【sql实战2】SQL语法练习

DMS

select birth_date 生日, gender 性别 from employees;
select first_name, last_name, gender from employees where gender = 'F';
select birth_date from employees order by birth_date desc;
select * from employees limit 10 offset 20;
select max(birth_date) from employees;
select count(gender), gender from employees group by gender;
select count(first_name) c, first_name from employees group by first_name order by c ;
select * from employees order by birth_date limit 10 offset 0 ;

DDS

基本查询

聚合查询

连接查询

内连接

SELECT 
    birth_date, first_name, last_name, dept_emp.dept_no, dept_name
FROM
    employees
        JOIN
    dept_emp ON dept_emp.emp_no = employees.emp_no
        JOIN
    departments ON departments.dept_no = dept_emp.dept_no
WHERE
    employees.emp_no = 10001;

左连接

SELECT 
    birth_date,
    first_name,
    last_name,
    dept_emp.dept_no,
    dept_name
FROM
    employees
        LEFT JOIN
    dept_emp ON dept_emp.emp_no = employees.emp_no
        LEFT JOIN
    departments ON departments.dept_no = dept_emp.dept_no
WHERE
    employees.emp_no in (10001, 10002, 10003);

Done

  • MySQL目前还不支持FUll JOIN, 需要用对LEFT JOIN和RIGHT JOIN做UNION来实现
  • 另外是部分数据演示的时候数据库被我破坏了

可以可以先忽略全连接这个知识点了。

SELECT 
    birth_date,
    first_name,
    last_name,
    dept_emp.dept_no,
    dept_name
FROM
    employees FULL
        JOIN
    (dept_emp, departments) ON dept_emp.emp_no = employees.emp_no
        AND departments.dept_no = dept_emp.dept_no
WHERE
    employees.emp_no IN (10001 , 10002, 10003);

作业

  • 给出各部门历任领导的记录并附上部门名字
  • 计算各个部门的平均工资并排序,给出平均工资、部门名称

数据库导入报错,这个是服务器的原因吗

offset以谁为偏移基准,有什么规则吗?

查询姓氏的总数,只能通过翻看下面的信息吗?sql语句是怎么样的?

select * from employees order by birth_date limit 10 offset 0 ;

根据前面select的查询结果的记录顺序

可以修改默认展示的返回的记录数大小,默认是50,实现一页浏览更多。更多的数据,还是得靠limit offset组合

下面的result39什么意思,是返回的记录数么

having in 和group by 和where的执行顺序是什么样的呢?

在docker的mysql容器中导sql文件到远程数据库的问题,望解答:
方法一的问题:

方法二的问题:

SQL解析后的查询顺序:
image

老师,我们在实际项目中,会如何用到数据库,只是在mysql客户端用语句增删改查吗?实际数据库使用场景是什么样的?

假设服务器在远程

docker run -it -v /Users/seveniruby/projects/test_db:/data/ --rm mysql bash -c "cd /data; mysql -h sql.testing-studio.com -u root -p < /data/employees.sql"

假设数据库服务器你可以直接访问,可以执行

docker exec -i mysql mysql -h sql.testing-studio.com -uroot -phogwarts  < /tmp/demo.sql
  • 测试数据构造(假设你搞不定mock)
  • 清理和还原被测系统数据, 一键恢复原来的老数据
  • 测试数据的提取
  • 写数据的校验
  • 给出各部门历任领导的记录并附上部门名字
    SELECT
    e.first_name,
    e.last_name,
    d.dept_name
    FROM
    dept_manager dm
    JOIN departments d ON dm.dept_no = d.dept_no
    JOIN employees e ON dm.emp_no = e.emp_no;

*计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT
AVG( s.salary ) salary,
d.dept_name
FROM
departments d
JOIN dept_emp de ON d.dept_no = de.dept_no
JOIN salaries s ON s.emp_no = de.emp_no
GROUP BY
dept_name
ORDER BY
salary;

1.给出各部门历任领导的记录并附上部门名字:
A.
select es.*,ds.dept_name from employees es,dept_manager dr,departments ds where es.emp_no=dr.emp_no and dr.dept_no=ds.dept_no;

B.
select es.*,ds.dept_name from employees es inner join dept_manager dr on es.emp_no=dr.emp_no inner join departments ds on dr.dept_no=ds.dept_no;

2.计算各个部门的平均工资并排序,给出平均工资、部门名称:
A.
select avg(ss.salary)dept_salary_avg,ds.dept_name from salaries ss,dept_emp dp,departments ds where ss.emp_no= dp.emp_no and dp.dept_no=ds.dept_no group by ds.dept_no order by dept_salary_avg;
image

B.
select avg(ss.salary)dept_salary_avg,ds.dept_name from salaries ss join dept_emp dp on ss.emp_no=dp.emp_no join departments ds on dp.dept_no=ds.dept_no group by ds.dept_no order by dept_salary_avg desc;
image

给出各部门历任领导的记录并附上部门名字
SELECT
dept_manager.emp_no,
dept_manager.dept_no,
from_date,
to_date,
dept_name
FROM
dept_manager
JOIN departments ON dept_manager.dept_no = departments.dept_no;

±-------±--------±-----------±-----------±-------------------+
| emp_no | dept_no | from_date | to_date | dept_name |
±-------±--------±-----------±-----------±-------------------+
| 111692 | d009 | 1985-01-01 | 1988-10-17 | Customer Service |
| 111784 | d009 | 1988-10-17 | 1992-09-08 | Customer Service |
| 111877 | d009 | 1992-09-08 | 1996-01-03 | Customer Service |
| 111939 | d009 | 1996-01-03 | 9999-01-01 | Customer Service |
| 110511 | d005 | 1985-01-01 | 1992-04-25 | Development |
| 110567 | d005 | 1992-04-25 | 9999-01-01 | Development |
| 110085 | d002 | 1985-01-01 | 1989-12-17 | Finance |
| 110114 | d002 | 1989-12-17 | 9999-01-01 | Finance |
| 110183 | d003 | 1985-01-01 | 1992-03-21 | Human Resources |
| 110228 | d003 | 1992-03-21 | 9999-01-01 | Human Resources |
| 110022 | d001 | 1985-01-01 | 1991-10-01 | Marketing |
| 110039 | d001 | 1991-10-01 | 9999-01-01 | Marketing |
| 110303 | d004 | 1985-01-01 | 1988-09-09 | Production |
| 110344 | d004 | 1988-09-09 | 1992-08-02 | Production |
| 110386 | d004 | 1992-08-02 | 1996-08-30 | Production |
| 110420 | d004 | 1996-08-30 | 9999-01-01 | Production |
| 110725 | d006 | 1985-01-01 | 1989-05-06 | Quality Management |
| 110765 | d006 | 1989-05-06 | 1991-09-12 | Quality Management |
| 110800 | d006 | 1991-09-12 | 1994-06-28 | Quality Management |
| 110854 | d006 | 1994-06-28 | 9999-01-01 | Quality Management |
| 111400 | d008 | 1985-01-01 | 1991-04-08 | Research |
| 111534 | d008 | 1991-04-08 | 9999-01-01 | Research |
| 111035 | d007 | 1985-01-01 | 1991-03-07 | Sales |
| 111133 | d007 | 1991-03-07 | 9999-01-01 | Sales |
±-------±--------±-----------±-----------±-------------------+
24 rows in set (0.06 sec)

计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT
avg( salary ),
dept_name
FROM
salaries
JOIN dept_emp ON salaries.emp_no = dept_emp.emp_no
JOIN departments ON dept_emp.dept_no = departments.dept_no
GROUP BY
dept_emp.dept_no
ORDER BY
avg( salary ) DESC;

±--------------±-------------------+
| avg( salary ) | dept_name |
±--------------±-------------------+
| 80667.6058 | Sales |
| 71913.2000 | Marketing |
| 70489.3649 | Finance |
| 59665.1817 | Research |
| 59605.4825 | Production |
| 59478.9012 | Development |
| 58770.3665 | Customer Service |
| 57251.2719 | Quality Management |
| 55574.8794 | Human Resources |
±--------------±-------------------+
9 rows in set (2.70 sec)

1、给出各部门历任领导的记录并附上部门名字

select m.emp_no,e.first_name,e.last_name,d.dept_name from dept_manager m LEFT JOIN departments d ON m.dept_no = d.dept_no LEFT JOIN employees e ON m.emp_no = e.emp_no;

2、计算各个部门的平均工资并排序,给出平均工资、部门名称

select d.dept_name "部门名称",AVG(s.salary) "平均工资" from salaries s LEFT JOIN dept_emp e ON s.emp_no = e.emp_no LEFT JOIN departments d ON e.dept_no = d.dept_no GROUP BY d.dept_name;
  • 给出各部门历任领导的记录并附上部门名字
SELECT
  dept_manager.dept_no,
  dept_name,
  employees.emp_no,
  first_name,
  last_name,
  dept_manager.from_date,
  dept_manager.to_date
FROM
  departments
JOIN (dept_manager, employees) ON employees.emp_no = dept_manager.emp_no
AND departments.dept_no = dept_manager.dept_no
ORDER BY
  dept_manager.dept_no;
  • 计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT
 dept_name,
 AVG(salary)
FROM
 salaries
JOIN (dept_emp,departments) ON dept_emp.emp_no = salaries.emp_no
AND departments.dept_no = dept_emp.dept_no
GROUP BY
 dept_emp.dept_no
ORDER BY
 AVG(salary) DESC;
  • 给出各部门历任领导的记录并附上部门名字
SELECT
  dept_manager.dept_no,
  dept_name,
  employees.emp_no,
  first_name,
  last_name,
  dept_manager.from_date,
  dept_manager.to_date
FROM departments
JOIN (dept_manager, employees) ON employees.emp_no = dept_manager.emp_no
AND departments.dept_no = dept_manager.dept_no
ORDER BY dept_manager.dept_no;
  • 计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT AVG(salary), dept_name
FROM salaries
JOIN (dept_emp, departments) ON dept_emp.emp_no = salaries.emp_no
AND departments.dept_no = dept_emp.dept_no
GROUP BY dept_emp.dept_no
ORDER BY AVG(salary) DESC;

给出各部门历任领导的记录并附上部门名字
SELECT e.first_name,e.last_name ,d.dept_name from dept_manager as m
LEFT JOIN employees as e on m.emp_no = e.emp_no
LEFT JOIN departments as d on m.dept_no = d.dept_no
image
计算各个部门的平均工资并排序,给出平均工资、部门名称

SELECT AVG(s.salary) as salary ,de.dept_name FROM salaries as s
left JOIN dept_emp as d on s.emp_no = d.emp_no
LEFT JOIN departments as de on d.dept_no = de.dept_no GROUP BY de.dept_no ORDER BY salary
image