SELECT last_name, job_id, salary FROM employees WHERE job_id = ( SELECT job_id FROM employees WHERE employee_id =141 ) AND salary > ( SELECT salary FROM employees WHERE employee_id =143 );
# 实现方式一:不成对比较 SELECT employee_id, manager_id, department_id FROM employees WHERE manager_id = ( SELECT manager_id FROM employees WHERE employee_id =141 ) AND department_id = ( SELECT department_id FROM employees WHERE employee_id =141 ) AND employee_id <>141;
# 实现方式二:成对比较 SELECT employee_id, manager_id, department_id FROM employees WHERE (manager_id, department_id) = ( SELECT manager_id, department_id FROM employees WHERE employee_id =141 ) AND employee_id <>141;
3.HAVING中的子查询
==题目:查询最低工资大于50号部门最低工资的部门id和其最低工资==
1 2 3 4 5 6 7 8
SELECT department_id, MIN(salary) FROM employees GROUPBY department_id HAVINGMIN(salary) > ( SELECTMIN(salary) FROM employees WHERE department_id =50 );
SELECT employee_id, last_name, (CASE department_id WHEN (SELECT department_id FROM departments WHERE location_id =1800) THEN'Canada' ELSE'USA'END) AS "location" FROM employees;
5.非法使用子查询
1 2 3 4 5 6 7 8
# 错误的原因:多行子查询使用单行比较符 SELECT employee_id, last_name FROM employees WHERE salary = ( SELECTMIN(salary) FROM employees GROUPBY department_id );
SELECT employee_id, last_name, job_id, salary FROM employees WHERE job_id <>'IT_PROG' AND salary <ANY( SELECT salary FROM employees WHERE job_id ='IT_PROG' ); # 等价的写法: SELECT employee_id, last_name, job_id, salary FROM employees WHERE job_id <>'IT_PROG' AND salary < ( SELECTMAX(salary) FROM employees WHERE job_id ='IT_PROG' );
# 方式一:在WHERE中声明子查询 SELECT e1.last_name, e1.salary, e1.department FROM employees e1 WHERE e1.salary > ( SELECTAVG(e2.salary) FROM employees e2 WHERE e2.department_id = e1.department_id ); # 方式二:在FROM中声明子查询 SELECT e.last_name, e.salary, e.department_id FROM employees e, ( SELECT department_id, AVG(salary) AS "avg_sal" FROM employees GROUPBY department_id) t_dept_avg_sal WHERE e.department_id = t_dept_avg_sal.department_id AND e.salary > t_dept_avg_salary.avg_sal;
==题目:查询员工的id,salary,按照department_name排序==
1 2 3 4 5 6 7
SELECT e.employee_id, e.salary FROM employees e ORDERBY ( SELECT d.department_name FROM departments d WHERE e.`department_id` = d.`department_id` );
# 方式一:右连接 SELECT d.department_id, d.department_name FROM departments e RIGHTJOIN departments d ON e.`department_id` = d.`department_id` WHERE e.`department_id` ISNULL;
# 方式二:NOTEXISTS SELECT d.department_id, d.department_name FROM departments d WHERENOTEXISTS ( SELECT* FROM employees e WHERE d.`department_id` = e.`department_id` );
# 方式一:相关子查询 SELECT d.*, (SELECTAVG(salary) FROM employees WHERE department_id = d.`department_id`) avg_sal FROM departments d, ( SELECT department_id, AVG(salary) avg_sal FROM employees GROUPBY department_id ORDERBY avg_sal ASC LIMIT 0,1 ) t_dept_avg_sal WHERE d.`department_id` = t_dept_avg_sal.`department_id`;
#方式二:不相关子查询 SELECT d.*, t_dept_avg_sal.avg_sal avg_sal FROM departments d, ( SELECT department_id, AVG(salary) avg_sal FROM employees GROUPBY department_id ORDERBY avg_sal ASC LIMIT 0,1 ) t_dept_avg_sal WHERE d.`department_id` = t_dept_avg_sal.`department_id`;
10.查询平均工资最高的job信息
1 2 3 4 5 6 7 8 9 10 11
SELECT* FROM jobs WHERE job_id = ( SELECT job_id FROM employees GROUPBY job_id HAVINGAVG(salary) >=ALL( SELECTAVG(salary) FROM employees GROUPBY job_id) );
11.查询平均工资高于公司平均工资的部门有哪些
1 2 3 4 5 6 7 8
SELECT depatment_id FROM employees WHERE department_id ISNOTNULL GROUPBY department_id HAVINGAVG(salary) > ( SELECTAVG(salary) FROM employees );
SELECT* FROM employees WHERE employee_id = (SELECT d.manager_id FROM departments d JOIN (SELECT department_id, AVG(salary) AS "avg_sal" FROM employees WHERE department_id ISNOTNULL GROUPBY department_id ORDERBY avg_sal LIMIT 1) t_dept_avg_sal ON d.department_id = t_dept_avg_sal.department_id);
15.查询部门的部门号,其中不包括job_id是”ST_CLERK”的部门号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SELECT department_id FROM departments WHERE department_id NOTIN ( SELECTDISTINCT department_id FROM employees WHERE job_id = `ST_CLERK` );
SELECT d.department_id FROM department d WHERENOTEXISTS ( SELECT* FROM employees e WHERE d.`department_id` = e.`department_id` AND e.`job_id` ='ST_CLERK' );
16.选择所有没有管理者的员工的last_name
1 2 3 4 5 6 7 8 9 10 11
SELECT last_name FROM employees WHERE manager_id ISNULL;
SELECT emp.last_name FROM employees emp WHERENOTEXISTS ( SELECT* FROM employees mgr WHERE emp.`manager_id` = mgr.`employee_id` );
17.查询员工号、姓名、雇用时间、工资,其中员工的管理者为 ‘De Haan’
1 2 3 4 5 6 7
SELECT employee_id, last_name, hire_date, salary FROM employee WHERE manager_id IN ( SELECT manager_id FROM employee WHERE last_name ='De Haan' );
18.查询各部门中工资比本部门平均工资高的员工的员工号,姓名和工资(相关子查询)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SELECT e1.department_id, e1.last_name, e1.salary FROM employees e1 WHERE e1.salary > ( SELECTAVG(salary) FROM employees e2 WHERE e2.`department_id` = e1.`department_id` );
SELECT e.last_name, e.salary, e.department_id FROM employees e, ( SELECT department_id, AVG(salary) avg_sal FROM employees GROUPBY department_id ) t_dept_avg_sal WHERE e.`department_id` = t_dept_avg_sal.`department_id` AND e.`salary` > t_dept_avg_sal.`avg_sal`;
19.查询每个部门下的部门人数大于5的部门名称(相关子查询)
1 2 3 4 5 6 7
SELECT d.department_name FROM departments d WHERE5< ( SELECTCOUNT(*) FROM employees e WHERE d.`department_id` = e.`department_id` );
20.查询每个国家下的部门个数大于2的国家编号(相关子查询)
1 2 3 4 5 6 7
SELECT country_id FROM locations l WHERE2< ( SELECTCOUNT(*) FROM department d WHERE l.`location_id` = d.`location_id` );