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);
题目:返回公司工资最少的员工的last_name
1 2 3 4 5 6
SELECT last_name,job_id,salary FROM employees WHERE salary = ( SELECTMIN(salary) FROM employees );
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;
1 2 3 4 5 6 7 8
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;
HAVING 中的子查询
首先执行子查询。
向主查询中的HAVING 子句返回结果。
题目:查询最低工资大于50号部门最低工资的部门id和其最低工资
1 2 3 4 5 6 7 8 9
SELECT department_id,MIN(salary) "min_sal" FROM employees WHERE department_id ISNOTNULL GROUPBY department_id HAVING min_sal > ( 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;
子查询中的空值问题
1 2 3 4 5 6
SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name ='Haas');
没有叫Haas的人,子查询不返回任何行
非法使用子查询
1 2 3 4 5 6
SELECT employee_id, last_name FROM employees WHERE salary = (SELECTMIN(salary) FROM employees GROUPBY department_id);
错误信息:Subquery returns more than 1 row
说明:括号中的子查询语句会返回每个部门的最低薪资,远不止一行
SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary <ANY ( SELECT salary FROM employees WHERE job_id ='IT_PROG' ) AND job_id !='IT_PROG';
SELECT employee_id,last_name,job_id,salary FROM employees WHERE salary <ALL ( SELECT salary FROM employees WHERE job_id ='IT_PROG' ) AND job_id !='IT_PROG';
题目:查询平均工资最低的部门id
在MySQL中,聚合函数不支持嵌套,但是在Oracle可以嵌套
1 2 3 4 5 6
SELECT department_id FROM employees GROUPBY department_id HAVINGAVG(salary) <=ALL (SELECTAVG(salary) FROM employees GROUPBY department_id);
空值问题
内查询中出现NULL,会影响查询结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SELECT last_name FROM employees WHERE employee_id NOTIN ( SELECT manager_id FROM employees );
SELECT last_name FROM employees WHERE employee_id NOTIN ( SELECT manager_id FROM employees WHERE manager_id ISNOTNULL-- 加一行判断,排除NULL的情况 );
SELECT last_name,salary,department_id FROM employees WHERE salary > ( SELECTAVG(salary) FROM employees );
这里仍然是不相关子查询,因为内查询只查询了一次,即找到公司平均工资即可
那我们现在再来看看原题该怎么做
1 2 3 4 5 6 7
SELECT last_name,salary,department_id FROM employees e1 WHERE salary > ( SELECTAVG(salary) FROM employees e2 WHERE e1.`department_id`=e2.`department_id` );
在 FROM 中使用子查询
查询各部门id的平均工资,当做一张表,然后再多表查询
1 2 3 4 5 6 7 8
SELECT e.last_name,e.salary,e.department_id FROM employees e JOIN (SELECT department_id,AVG(salary) avg_sal FROM employees GROUPBY department_id ) "t_dept_avg_sal" ON e.`department_id` = t_dept_avg_sal.department_id WHERE e.`salary` > t_dept_avg_sal.avg_sal;
题目:查询员工的id,salary,按照department_name 排序
1 2 3 4 5 6 7
SELECT employee_id,salary FROM employees e ORDERBY ( SELECT department_name FROM departments d WHERE e.`department_id` = d.`department_id` )ASC;
左外连接也可以做
1 2 3 4 5
SELECT employee_id,salary FROM employees e LEFTJOIN departments d ON e.`department_id` = d.`department_id` ORDERBY d.department_name;
-- 每次读题都得读个半天才知道啥意思 SELECT employee_id,last_name,job_id FROM employees e WHERE2<= (SELECTCOUNT(*) FROM job_history j WHERE e.`employee_id` = j.`employee_id` );
SELECT d.department_id,d.department_name FROM departments d LEFTJOIN employees e ON d.`department_id` = e.`department_id` WHERE e.`department_id` ISNULL;
SELECT d.department_id,d.department_name FROM departments d WHERENOTEXISTS (SELECT department_id FROM employees e WHERE e.`department_id` = d.`department_id` );
相关更新
题目:在employees中增加一个department_name字段,数据为员工对应的部门名称
1 2 3 4
UPDATE employees e SET department_name = (SELECT department_name FROM departments d WHERE e.department_id = d.department_id)
相关删除
删除表employees中,其与emp_history表皆有的数据
1 2 3 4 5
DELETEFROM employees e WHERE employee_id IN (SELECT employee_id FROM emp_history WHERE employee_id = e.employee_id);
思考题
问题:谁的工资比Abel的高?
1 2 3 4
SELECT e2.last_name,e2.salary FROM employees e1,employees e2 WHERE e1.last_name ='Abel' AND e1.`salary` < e2.`salary`;
1 2 3 4 5 6 7
SELECT last_name,salary FROM employees WHERE salary > ( SELECT salary FROM employees WHERE last_name ='Abel' );
SELECT last_name,job_id,salary FROM employees WHERE salary > ( SELECTMAX(salary) FROM employees WHERE job_id ='SA_MAN' );
1 2 3 4 5 6 7
SELECT last_name,job_id,salary FROM employees WHERE salary >ALL ( SELECT salary FROM employees WHERE job_id ='SA_MAN' );
查询和姓名中包含字母u的员工在相同部门的员工的员工号和姓名
1 2 3 4 5 6
SELECT employee_id,last_name FROM employees a WHERE department_id IN (SELECTDISTINCT department_id FROM employees b WHERE b.`last_name` LIKE "%u%" );
1 2 3 4 5 6
SELECT employee_id,last_name FROM employees a WHERE department_id =ANY (SELECTDISTINCT department_id FROM employees b WHERE b.`last_name` LIKE "%u%" );
查询在部门的location_id为1700的部门工作的员工的员工号
1 2 3 4 5 6
SELECT employee_id,last_name FROM employees e WHERE department_id IN (SELECT department_id FROM departments WHERE location_id =1700 );
查询管理者是King的员工姓名和工资
1 2 3 4 5 6
SELECT last_name,salary FROM employees WHERE manager_id IN (SELECT employee_id FROM employees WHERE last_name ='King' );
查询工资最低的员工信息: last_name, salary
1 2 3 4 5
SELECT last_name,salary FROM employees WHERE salary = (SELECTMIN(salary) FROM employees );
查询平均工资最低的部门信息
1 2 3 4 5 6 7 8 9
SELECT* FROM departments WHERE department_id = (SELECT department_id FROM employees GROUPBY department_id HAVINGAVG(salary) <=ALL (SELECTAVG(salary) FROM employees GROUPBY department_id ));
1 2 3 4 5 6 7 8
SELECT d.* FROM departments d,( SELECT department_id,AVG(salary) avg_sal FROM employees GROUPBY department_id ORDERBY avg_sal LIMIT 0,1) e WHERE d.`department_id` = e.department_id;
查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
1 2 3 4 5 6 7 8
SELECT* FROM departments d,( SELECT department_id,AVG(salary) avg_sal FROM employees GROUPBY department_id ORDERBY avg_sal LIMIT 1) t_dept_avg_sal WHERE d.`department_id` = t_dept_avg_sal.department_id;
查询平均工资最高的 job 信息
1 2 3 4 5 6 7 8 9
SELECT j.* FROM jobs j,( SELECTAVG(salary) avg_sal,job_id FROM employees GROUPBY job_id ORDERBY avg_sal DESC LIMIT 1 ) t_job_id_avg_sal WHERE j.`job_id` = t_job_id_avg_sal.job_id;
1 2 3 4 5 6 7 8 9 10 11 12
SELECT* FROM jobs j WHERE j.`job_id` = ( SELECT job_id FROM employees GROUPBY job_id HAVINGAVG(salary) >=ALL( SELECTAVG(salary) avg_sal FROM employees GROUPBY job_id ) );
查询平均工资高于公司平均工资的部门有哪些?
1 2 3 4 5 6 7
SELECT department_id FROM employees WHERE department_id ISNOTNULL GROUPBY department_id HAVINGAVG(salary) > (SELECTAVG(salary) FROM employees );
查询出公司中所有 manager 的详细信息
1 2 3
SELECTDISTINCT mng.* FROM employees emp , employees mng WHERE mng.`employee_id` = emp.`manager_id`;
1 2 3 4 5
SELECT* FROM employees WHERE employee_id IN (SELECTDISTINCT manager_id FROM employees );
1 2 3 4 5 6
SELECT* FROM employees m WHEREEXISTS (SELECTDISTINCT* FROM employees e WHERE m.`employee_id` = e.`manager_id` );
各个部门中 最高工资中最低的那个部门的 最低工资是多少?
1 2 3 4 5 6 7 8 9
SELECT employee_id,MIN(salary) FROM employees e,(SELECT department_id FROM employees WHERE department_id ISNOTNULL GROUPBY department_id ORDERBYMAX(salary) LIMIT 1 ) t_max_sal WHERE e.`department_id` = t_max_sal.department_id;
1 2 3 4 5 6 7 8 9
SELECTMIN(salary) FROM employees WHERE department_id = (SELECT department_id FROM employees WHERE department_id ISNOTNULL GROUPBY department_id ORDERBYMAX(salary) LIMIT 1 );
SELECT* FROM employees WHERE employee_id = (SELECT manager_id FROM departments WHERE department_id = (SELECT department_id FROM employees GROUPBY department_id ORDERBYAVG(salary) DESC LIMIT 1));
1 2 3 4 5 6 7 8 9 10 11 12
SELECT* FROM employees WHERE employee_id IN (SELECT manager_id FROM employees WHERE department_id = (SELECT department_id FROM employees GROUPBY department_id HAVINGAVG(salary) >=ALL ( SELECTAVG(salary) FROM employees GROUPBY department_id ))
查询部门的部门号,其中不包括job_id是"ST_CLERK"的部门号
1 2 3 4 5 6
SELECT department_id FROM departments WHERE department_id NOTIN (SELECTDISTINCT department_id FROM employees WHERE job_id ='ST_CLERK')
选择所有没有管理者的员工的last_name
1 2 3
SELECT last_name FROM employees WHERE manager_id ISNULL;
1 2 3 4 5 6
SELECT last_name FROM employees e1 WHERENOTEXISTS ( SELECT* FROM employees e2 WHERE e1.`manager_id` = e2.`employee_id`);
查询员工号、姓名、雇用时间、工资,其中员工的管理者为 ‘De Haan’
1 2 3 4 5
SELECT employee_id,last_name,hire_date,salary FROM employees WHERE manager_id = (SELECT employee_id FROM employees WHERE last_name ='De Haan');
查询各部门中工资比本部门平均工资高的员工的员工号, 姓名和工资(相关子查询)
1 2 3 4 5
SELECT employee_id,last_name,salary FROM employees e1 WHERE salary > (SELECTAVG(salary) FROM employees e2 WHERE e1.`department_id` = e2.`department_id`);
查询每个部门下的部门人数大于 5 的部门名称(相关子查询)
1 2 3 4 5
SELECT department_id,department_name FROM departments d WHERE5< (SELECTCOUNT(*) FROM employees e WHERE e.`department_id` = d.`department_id`);
查询每个国家下的部门个数大于 2 的国家编号(相关子查询)
1 2 3 4 5 6 7
SELECT country_id FROM locations l WHERE2< ( SELECTCOUNT(*) FROM departments d WHERE d.`location_id` = l.`location_id` );