SQL实战(二)

标题

SQL实战(二)

课程价值

  • 掌握 SQL 单表和多表查询

大纲

  • 单表查询
  • 多表查询

时长

90分钟

PPT

https://pdf.ceshiren.com/lg2/SQL实战2

实战内容

单表查询

条件查询语法

  • 通配:WHERE <列名> LIKE <字符表达式>
  • 范围限定:WHERE <列名> BETWEEN <起始表达式> AND <结束表达式>
  • 子集:in
  • 为空:is/is not null

条件查询练习

– 查询出生日期晚于1965-01-01的员工编号、姓名和生日
SELECT
emp_no, first_name, last_name, birth_date
FROM
employees
WHERE
birth_date > ‘1965-01-01’;
– 查询名字中包含’fai’的员工的信息
SELECT
*
FROM
employees
WHERE
first_name LIKE ‘%fai%’;
– 查询名字中’fa’开头的名字长度为3位的员工信息
SELECT
*
FROM
employees
WHERE
first_name LIKE ‘fa_’;
– 查询年薪介于70000到70003之间的员工编号和年薪
SELECT
emp_no, salary
FROM
salaries
WHERE
salary BETWEEN 70000 AND 70003;
– 查询名字在Berni和Bezalel之间的员工编号和名字
SELECT
emp_no, first_name
FROM
employees
WHERE
first_name BETWEEN ‘Berni’ AND ‘Bezalel’;
– 查询入职日期为1995-01-27和1995-03-20日的员工信息
SELECT
*
FROM
employees
WHERE
hire_date IN (‘1995-01-27’ , ‘1995-03-20’);
– 查询学生表中姓名为空的学生信息
SELECT
*
FROM
hogwarts_demo.students
WHERE
name IS NULL;
– 查询名字为Lillian并且姓氏为Haddadi的员工信息
SELECT
*
FROM
employees
WHERE
first_name = ‘Lillian’
AND last_name = ‘Haddadi’;
– 查询名字为Lillian或者姓氏为Terkki的员工信息
SELECT
*
FROM
employees
WHERE
first_name = ‘Lillian’
OR last_name = ‘Terkki’;
– 查询名字不为Lillian并且性别不是女的员工信息
SELECT
*
FROM
employees
WHERE
first_name != ‘Lillian’
AND NOT gender = ‘F’;

聚合查询

聚合函数练习

– 查询职级名称为Senior Engineer的员工数量
select count(*) from titles where title = ‘Senior Engineer’;
– 查询员工编号为10002的员工的最高年薪
select max(salary) from salaries where emp_no = ‘10002’;
– 查询员工编号为10002的员工的最低年薪
select min(salary) from salaries where emp_no = ‘10002’;
– 查询员工编号为10002的员工的薪水总和
select sum(salary) from salaries where emp_no = ‘10002’;
– 查询员工编号为10002的员工的平均年薪
select avg(salary) from salaries where emp_no = ‘10002’;

聚合查询练习

– 查询每个员工的薪资和
select emp_no, sum(salary) from salaries group by emp_no;
– 查询员工编号小于10010的,薪资和小于400000的员工的薪资和
SELECT
emp_no, SUM(salary)
FROM
salaries
WHERE
emp_no < 10010
GROUP BY emp_no
HAVING SUM(salary) < 400000;

子句小结

  • WHERE子句:从数据源中去掉不符合其搜索条件的数据
  • GROUP BY子句:搜集数据行到各个组中,统计函数为各个组计算统计值
  • HAVING子句:去掉不符合其组搜索条件的各行数据行

基础查询语法总结

select <列名>
from <表名>
where <查询条件表达式>
group by <分组的列名>
having <分组后查询条件表达式>
order by <排序列名> [ASC或者DESC]
limte [开始的行数], <查询记录的条数>

多表查询

表连接

语法
SELECT * 
FROM  
	表名1  别名1
INNER/LEFT/RIGHT JOIN 
	表名2  别名2
ON 
	别名1.外键列 = 别名2.主键列;

表连接练习

– 查询员工编号为10001的员工的生日,姓名,部门编号和部门名称
SELECT
birth_date, first_name, last_name, d.dept_no, dept_name
FROM
employees e
INNER JOIN
dept_emp de ON e.emp_no = de.emp_no
INNER JOIN
departments d ON d.dept_no = de.dept_no
WHERE
e.emp_no = ‘10001’;
– 取出客服部门的历任管理者
SELECT
*
FROM
employees e
INNER JOIN
(dept_manager dm, departments d) ON e.emp_no = dm.emp_no
AND dm.dept_no = d.dept_no
WHERE
dept_name = ‘Customer Service’;
– 查询员工编号为10001、10002、10003的员工的生日,姓名,部门编号,部门名称
SELECT
birth_date, first_name, last_name, d.dept_no, dept_name
FROM
employees e
LEFT JOIN
(dept_emp de, departments d) ON e.emp_no = de.emp_no
AND de.dept_no = d.dept_no
WHERE
e.emp_no IN (10001 , 10002, 10003);

子查询练习

– 查询生日为1959-12-03,入职日期为1986-08-28的员工所在的部门编号
SELECT
dept_no
FROM
dept_emp
WHERE
emp_no = (SELECT
emp_no
FROM
employees
WHERE
birth_date = ‘1959-12-03’
AND hire_date = ‘1986-08-28’);
– 查询1988年的年薪为62102的员工的姓名
SELECT
first_name, last_name
FROM
employees
WHERE
emp_no IN (SELECT
emp_no
FROM
salaries
WHERE
salary = 62102
AND from_date LIKE ‘1988%’);

查询思路总结

  • 查询结果是什么,对应的是哪些表
  • 查询条件是什么,查询条件中的这些字段都在哪些表里面

课后作业

  • 计算各个部门的平均工资并排序,给出平均工资、部门名称
  • 查询developments部的工资最高的前十位员工信息
  • 查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)

课后调查表

-- 计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT 
    de.dept_name, AVG(s.salary)
FROM
    salaries s
        INNER JOIN
    (dept_emp d, departments de) ON d.emp_no = s.emp_no
        AND d.dept_no = de.dept_no
GROUP BY de.dept_name;
-- 查询developments部的工资最高的前十位员工信息
SELECT 
    e.*
FROM
    employees e
        INNER JOIN
    (dept_emp d, departments de, salaries s) ON d.emp_no = e.emp_no
        AND d.dept_no = de.dept_no and e.emp_no = s.emp_no
WHERE
    dept_name = 'Development' order by s.salary desc limit 10;
-- 查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
SELECT 
    e.emp_no, e.hire_date
FROM
    employees e
        INNER JOIN
    (dept_emp d, departments de) ON d.emp_no = e.emp_no
        AND d.dept_no = de.dept_no
WHERE
    de.dept_name = 'Development';
    
    
SELECT 
    emp_no, hire_date
FROM
    employees
WHERE
    emp_no IN (SELECT 
            emp_no
        FROM
            dept_emp
        WHERE
            dept_no = (SELECT 
                    dept_no
                FROM
                    departments
                WHERE
                    dept_name = 'Development'));
----计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT 
    departments.dept_name AS deptname,
    avg(salaries.salary) AS avg_salary
FROM
    salaries
        LEFT JOIN
    dept_emp ON salaries.emp_no = dept_emp.emp_no
        LEFT JOIN
    departments ON departments.dept_no = dept_emp.dept_no
group by deptname
order by avg_salary;
----查询developments部的工资最高的前十位员工信息
SELECT 
    MAX(salaries.salary) AS max_salary, employees.*
FROM
    salaries
        LEFT JOIN
    dept_emp ON salaries.emp_no = dept_emp.emp_no
        LEFT JOIN
    departments ON departments.dept_no = dept_emp.dept_no
        LEFT JOIN
    employees ON dept_emp.emp_no = employees.emp_no
WHERE
    departments.dept_name = 'Development'
GROUP BY salaries.emp_no
ORDER BY max_salary DESC
LIMIT 10;
----查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
----连接查询
SELECT 
    employees.emp_no, employees.hire_date
FROM
    employees
        LEFT JOIN
    dept_emp ON employees.emp_no = dept_emp.emp_no
        LEFT JOIN
    departments ON departments.dept_no = dept_emp.dept_no
WHERE
    departments.dept_name = 'Development';
----子查询
SELECT 
    employees.emp_no, employees.hire_date
FROM
    employees
WHERE
    employees.emp_no IN (SELECT 
            dept_emp.emp_no
        FROM
            dept_emp
                LEFT JOIN
            departments ON dept_emp.dept_no = departments.dept_no);
-- 计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT
	c.dept_name,
	AVG( a.salary ) 
FROM
	salaries a
	LEFT JOIN dept_emp b ON a.emp_no = b.emp_no
	LEFT JOIN departments c ON b.dept_no = c.dept_no 
GROUP BY
	c.dept_name 
ORDER BY
	AVG( a.salary );

-- 查询developments(Development)部的工资最高的前十位员工信息
SELECT
	e.* 
FROM
	employees e
	LEFT JOIN ( departments d, salaries s, dept_emp de ) ON e.emp_no = s.emp_no 
	AND d.dept_no = de.dept_no 
	AND e.emp_no = de.emp_no 
	AND d.dept_name = 'Development' 
ORDER BY
	s.salary DESC 
	LIMIT 10;

-- 查询developments(Development)部的所有员工的员工编号和入职日期
-- (使用表连接和子查询两种方式进行演练)
-- 表连接
SELECT
	e.emp_no,
	hire_date 
FROM
	employees e
	INNER JOIN dept_emp de ON e.emp_no = de.emp_no
	INNER JOIN departments d ON d.dept_no = de.dept_no 
	AND d.dept_name = 'Development';
-- 子查询
SELECT
	e.emp_no,
	hire_date 
FROM
	employees e 
WHERE
	emp_no IN ( SELECT de.emp_no FROM dept_emp de WHERE de.dept_no = 
( SELECT dept_no FROM departments d WHERE d.dept_name = 'Development' ) );

张志格_作业:

-- 计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT 
    dem.dept_name AS deptname,
    avg(s.salary) AS avg_salary
FROM salaries s
    JOIN  (dept_emp d, departments dem)
      ON s.emp_no = d.emp_no
     and dem.dept_no = d.dept_no
group by deptname
order by avg_salary desc;

--查询developments部的工资最高的前十位员工信息
SELECT e.*
FROM employees e
    JOIN (dept_emp d, departments dem, salaries s)
	ON e.emp_no = d.emp_no
    and dem.dept_no = d.dept_no
    and e.emp_no = s.emp_no
where dept_name='Development'
order by s.salary desc
limit 10

-- 查询developments部的所有员工的员工编号和入职日期
-- 表连接
SELECT e.emp_no, e.hire_date
FROM employees e
    JOIN (dept_emp d, departments dem) 
     ON d.emp_no = e.emp_no
    AND d.dept_no = dem.dept_no
WHERE
    dem.dept_name='Development'
-- 子查询
SELECT emp_no, hire_date
FROM employees
WHERE emp_no IN (SELECT emp_no 
				 FROM dept_emp
				WHERE dept_no = (SELECT dept_no
								FROM departments
								WHERE dept_name = 'Development'))

– 课后作业
– 计算各个部门的平均工资并排序,给出平均工资、部门名称

SELECT
	d.dept_name,
	avg( s.salary ) 
FROM
	salaries s LEFT join dept_emp de ON s.emp_no = de.emp_no
	LEFT JOIN departments d  
on 
	de.dept_no = d.dept_no 
GROUP BY
	d.dept_no 
ORDER BY
	avg( s.salary ) DESC

– 查询developments部的工资最高的前十位员工信息

SELECT
	* 
FROM
	employees e
	LEFT JOIN salaries s ON e.emp_no = s.emp_no 
	where e.emp_no IN ( SELECT emp_no FROM dept_emp WHERE dept_no IN ( SELECT dept_no FROM departments WHERE dept_name = 'Development' ) ) 
ORDER BY
	s.salary desc
	LIMIT 10

– 查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
– 表连接

SELECT
	e.emp_no,hire_date 
FROM
	employees e
	LEFT JOIN ( dept_emp de, departments d ) ON e.emp_no = de.emp_no 
	AND de.dept_no = d.dept_no 
WHERE
	d.dept_name = 'Development'

– 子查询

SELECT
	emp_no,hire_date 
FROM
	employees 
WHERE
	emp_no IN ( SELECT emp_no FROM dept_emp WHERE dept_no IN ( SELECT dept_no FROM departments WHERE dept_name = 'Development' ) )

郑向红作业:
1.计算各个部门的平均工资并排序,给出平均工资、部门名称
image
2.查询developments部的工资最高的前十位员工信息


3.查询developments部的所有员工的员工编号和入职日期(使用表连接演练)

4.查询developments部的所有员工的员工编号和入职日期(使用子查询演练)

陈智斌 SQL实战(二)作业提交

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

image

查询developments部的工资最高的前十位员工信息
SELECT
salary,
dept_name
FROM
salaries s
LEFT JOIN ( dept_emp de, departments dep ) ON s.emp_no = de.emp_no
AND de.dept_no = dep.dept_no
WHERE
dep.dept_name = “Development”
ORDER BY
salary DESC
LIMIT 10;
image

查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)

连接查询
SELECT
emp_no,
from_date,
dept_name
FROM
dept_emp de
LEFT JOIN departments dep ON de.dept_no = dep.dept_no
WHERE
dept_name = “Development”;

子查询
SELECT
emp_no,
from_date
FROM
dept_emp
WHERE
dept_no = ( SELECT dept_no FROM departments WHERE dept_name = “Development” );

张立雨7646 作业

SELECT de.dept_name, AVG(s.salary) FROM salaries s INNER JOIN (dept_emp d, departments de) ON d.emp_no=s.emp_no AND de.dept_no=d.dept_no GROUP BY de.dept_name ;

SELECT e.* FROM employees e INNER JOIN (dept_emp d, departments de, salaries s) ON d.emp_no = e.emp_no AND d.dept_no = de.dept_no and e.emp_no = s.emp_no
WHERE dept_name = ‘Development’ order by s.salary desc limit 10;

SELECT e.emp_no, e.hire_date FROM employees e INNER JOIN (dept_emp d, departments de) ON d.emp_no = e.emp_no AND d.dept_no = de.dept_no
WHERE de.dept_name = ‘Development’;

/计算各个部门的平均工资并排序,给出平均工资、部门名称/
SELECT de.dept_name, AVG(s.salary) FROM salaries s INNER JOIN (dept_emp d, departments de) ON d.emp_no = s.emp_no AND d.dept_no = de.dept_no GROUP BY de.dept_name;
/查询developments部的工资最高的前十位员工信息/
SELECT e.* FROM employees e LEFT JOIN ( departments d, salaries s, dept_emp de ) ON e.emp_no = s.emp_no AND d.dept_no = de.dept_no AND e.emp_no = de.emp_no AND d.dept_name = ‘Development’ ORDER BY s.salary DESC LIMIT 10;

/查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)/
/表链接/
SELECT e.emp_no, e.hire_date FROM employees e INNER JOIN (dept_emp d, departments de) ON d.emp_no = e.emp_no AND d.dept_no = de.dept_no WHERE de.dept_name = ‘Development’;
/子查询/
SELECT emp_no, hire_date FROM employees WHERE emp_no IN (SELECT emp_no FROM dept_emp WHERE dept_no = (SELECT dept_no FROM departments WHERE dept_name = ‘Development’));

贾潇冰——SQL实战(二)作业
1、计算各个部门的平均工资并排序,给出平均工资、部门名称


2、查询developments部的工资最高的前十位员工信息

3、查询developments部的所有员工的员工编号和入职日期
(1)表连接

(2)子查询

#第一题计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT
departments.dept_name AS deptname,
avg(salaries.salary) AS avg_salary
FROM
salaries
LEFT JOIN
dept_emp ON salaries.emp_no = dept_emp.emp_no
LEFT JOIN
departments ON departments.dept_no = dept_emp.dept_no
group by deptname
order by avg_salary;
#第二题查询developments部的工资最高的前十位员工信息
SELECT
MAX(salaries.salary) AS max_salary, employees.*
FROM
salaries
LEFT JOIN
dept_emp ON salaries.emp_no = dept_emp.emp_no
LEFT JOIN
departments ON departments.dept_no = dept_emp.dept_no
LEFT JOIN
employees ON dept_emp.emp_no = employees.emp_no
WHERE
departments.dept_name = ‘Development’
GROUP BY salaries.emp_no
ORDER BY max_salary DESC
LIMIT 10;
#第三题查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
SELECT
employees.emp_no, employees.hire_date
FROM
employees
LEFT JOIN
dept_emp ON employees.emp_no = dept_emp.emp_no
LEFT JOIN
departments ON departments.dept_no = dept_emp.dept_no
WHERE
departments.dept_name = ‘Development’;

SELECT
employees.emp_no, employees.hire_date
FROM
employees
WHERE
employees.emp_no IN (SELECT
dept_emp.emp_no
FROM
dept_emp
LEFT JOIN
departments ON dept_emp.dept_no = departments.dept_no);

  • 计算各个部门的平均工资并排序,给出平均工资、部门名称
    image
  • 查询developments部的工资最高的前十位员工信息
  • 查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
    image
    image

#计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT AVG(s.salary) as avg_salary, dept.dept_name
FROM salaries s
INNER JOIN (departments dept ,dept_emp emp)
ON (s.emp_no = emp.emp_no AND emp.dept_no = dept.dept_no)
GROUP BY dept.dept_name;

#查询developments部的工资最高的前十位员工信息
select
MAX(salaries.salary) as ma,employees.*
from salaries
LEFT JOIN employees
ON employees.emp_no = salaries.emp_no
INNER JOIN dept_emp
ON dept_emp.emp_no = employees.emp_no
INNER JOIN departments
ON departments.dept_name= ‘Development’ AND departments.dept_no = dept_emp.dept_no
GROUP BY salaries.emp_no
ORDER BY ma DESC limit 10;
#查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
#子查询
select
employees.emp_no,employees.hire_date
from
employees
where employees.emp_no IN
(SELECT dept_emp.emp_no from dept_emp where dept_emp.dept_no IN
(SELECT departments.dept_no from departments where departments.dept_name = ‘Development’));
#表连接
select
employees.emp_no,employees.hire_date
from
employees
INNER JOIN (dept_emp,departments)
ON (employees.emp_no = dept_emp.emp_no
AND dept_emp.dept_no = departments.dept_no
AND departments.dept_name = ‘Development’);

#计算各个部门的平均工资并排序,给出平均工资、部门名称
image
#查询developments部的工资最高的前十位员工信息
image
#查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
image

窦晖_SQL2作业_测开2期

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

SELECT 
    AVG(ss.salary) AS deptAvgSalary, dd.dept_name AS deptName
FROM
    departments AS dd
        LEFT JOIN
    (dept_emp AS de, salaries AS ss) ON dd.dept_no = de.dept_no
        AND de.emp_no = ss.emp_no
GROUP BY dd.dept_name
ORDER BY deptAvgSalary DESC;

查询developments部的工资最高的前十位员工信息

SELECT 
    MAX(ss.salary) AS developmentsSalaryTop10, ee.*
FROM
    employees AS ee
        LEFT JOIN
    salaries AS ss ON ee.emp_no = ss.emp_no
        LEFT JOIN
    dept_emp AS de ON ee.emp_no = de.emp_no
WHERE
    de.dept_no IN (SELECT 
            dept_no
        FROM
            departments
        WHERE
            dept_name = 'Development')
GROUP BY ee.emp_no
ORDER BY MAX(ss.salary) DESC
LIMIT 10;

查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)

-- 表连接
SELECT 
		ee.emp_no AS developmentsEmpNo, ee.hire_date AS hireDate
	FROM
		employees AS ee
			LEFT JOIN
		dept_emp AS de ON ee.emp_no = de.emp_no
	WHERE
		de.dept_no IN (SELECT 
				dept_no
			FROM
				departments
			WHERE
				dept_name = 'Development');

-- 子查询
	SELECT 
		ee.emp_no AS developmentsEmpNo, ee.hire_date AS hireDate
	FROM
		employees AS ee
	WHERE
		ee.emp_no IN (SELECT 
				de.emp_no
			FROM
				dept_emp AS de
			WHERE
				de.dept_no IN (SELECT 
						dept_no
					FROM
						departments AS dd
					WHERE
						dd.dept_name = 'Development'));

刘羽 SQL实战(二)–作业

– 计算各个部门的平均工资并排序,给出平均工资、部门名称
SELECT
AVG(salary), dept_name
FROM
salaries s
INNER JOIN
dept_emp de ON s.emp_no = de.emp_no
INNER JOIN
departments d ON de.dept_no = d.dept_no
GROUP BY de.dept_no
ORDER BY AVG(salary);
– 查询developments部的工资最高的前十位员工信息
SELECT
*
FROM
employees e
left JOIN
dept_emp de ON e.emp_no = de.emp_no
INNER JOIN
departments d ON de.dept_no = d.dept_no
INNER JOIN
salaries s ON e.emp_no = s.emp_no
WHERE
dept_name = ‘Development’
ORDER BY s.salary DESC
LIMIT 10;
– 查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
– 表连接如下:
SELECT
e.emp_no, hire_date
FROM
employees e
INNER JOIN
(dept_emp de, departments d) ON e.emp_no = de.emp_no
AND de.dept_no = d.dept_no
WHERE
dept_name = ‘Development’;
– 子查询方式如下:
SELECT
hire_date, emp_no
FROM
employees
WHERE
emp_no IN (SELECT
emp_no
FROM
dept_emp
WHERE
dept_no = (SELECT
dept_no
FROM
departments
WHERE
dept_name = ‘Development’));

匡志荣 SQL实战(二) 作业

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

– 查询developments部的工资最高的前十位员工信息
SELECT
e.*, s.salary, d1.dept_no
FROM
employees e
LEFT JOIN
(salaries s, dept_emp d1, departments d2) ON s.emp_no = d1.emp_no
AND d1.dept_no = d2.dept_no
AND e.emp_no = s.emp_no
WHERE
d2.dept_name = ‘Development’
ORDER BY s.salary DESC
LIMIT 10;

– 查询developments部的所有员工的员工编号和入职日期
SELECT
e.emp_no, e.hire_date
FROM
employees e
INNER JOIN
(departments d1, dept_emp d2) ON d1.dept_no = d2.dept_no
AND e.emp_no = d2.emp_no
WHERE
d1.dept_name = ‘Development’;

SELECT
emp_no, hire_date
FROM
employees
WHERE
emp_no IN (SELECT
emp_no
FROM
dept_emp
WHERE
dept_emp.dept_no = (SELECT
dept_no
FROM
departments
WHERE
dept_name = ‘Development’));

王小刚作业
– 课后作业
– 计算各个部门的平均工资并排序,给出平均工资、部门名称
select
avg(salary),dept_name
from
employees.salaries s
inner join
(employees.dept_emp de,employees.departments d)
on
s.emp_no = de.emp_no and de.dept_no = d.dept_no
group by
d.dept_name;

– 查询developments部的工资最高的前十位员工信息
select
e.*
from
employees.employees e
inner join
(employees.dept_emp de,employees.departments d,employees.salaries s)
on
s.emp_no = de.emp_no and de.dept_no = d.dept_no and e.emp_no = de.emp_no
order by
s.salary desc
limit 10 ;
– 查询developments部的所有员工的员工编号和入职日期(使用表连接和子查询两种方式进行演练)
– 表连接
select
e.emp_no,e.hire_date
from
employees.employees e
inner join
(employees.dept_emp de,employees.departments d)
on
e.emp_no = de.emp_no and de.dept_no = d.dept_no
where
d.dept_name = ‘Development’;
– 子查询
select
emp_no,hire_date
from
employees.employees
where
emp_no in ( select
emp_no
from
employees.dept_emp
where
dept_no in (select
dept_no
from
employees.departments
where
dept_name = ‘Development’

         )

)