时间:2021-07-01 10:21:17 帮助过:3人阅读
HAVING子句是对分组统计函数进行过滤的子句,也可以在HAVING子句中使用子查询
SELECT JOB,AVG(SAL)
FROM EMP
GROUP BY JOB
HAVING AVG(SAL) = (SELECT MAX(AVG(SAL)) FROM EMP GROUP BY JOB);
FROM子查询就是将一个查询结构(一般多行多列)作为主查询的数据源
SELECT JOB,AVG(SAL)
FROM (SELECT JOB,AVG(SAL) AS AVGSAL FROM EMP GROUP BY JOB)TEMP
WHERE TEMP.AVGSAL>2000;
SELECT子查询在SELECT子句中使用查询的结果(一般会和dual空表一起使用)
SELECT (SELECT COUNT(*) FROM EMP WHERE JOB = ‘SALESMAN‘)/(SELECT COUNT(*) FROM EMP)
FROM DUAL;
将主查询的数据带到子查询中验证,如果成功则返回true,否则发水false。主查询接收true是就会显示这条数据,flase就不会显示。
SELECT *
FROM EMP E
WHERE EXISIT (
SELECT *
FROM DEPT D
WHERE E.DEPTNO = D.DEPTNO);
SELECT *
FROM EMP
WHERE SAL = (SELECT MIN(SAL)
FROM (SELECT ROWNUM,SAL
FROM (SELECT SAL FROM EMP GROUP BY SAL ORDER BY SAL DESC)
WHERE ROWNUM<=n));
思路:
1.先按薪资降序分组
2.再取前n名薪资中最低的薪资,即第n名的薪资。
3.最后在原表中找出薪资与最低薪资相同的员工信息。
SQl子查询
标签:key 一个 条件 min 另一个 sele order 成功 temp