AI智能
改变未来

MySQL子查询学习(练习)

CREATE TABLE emp(empno  INT,ename  VARCHAR(50),job  VARCHAR(50),mgr  INT,hiredate DATE,sal  DECIMAL(7,2),comm  DECIMAL(7,2),deptno  INT) ;CREATE TABLE dept(deptno  INT,dname  VARCHAR(14),locINSERT INTO emp VALUES(7369,\'SMITH\',\'CLERK\',7902,\'1980-12-17\',800,NULL,20);INSERT INTO emp VALUES(7499,\'ALLEN\',\'SALESMAN\',7698,\'1981-02-20\',1600,300,30);INSERT INTO emp VALUES(7521,\'WARD\',\'SALESMAN\',7698,\'1981-02-22\',1250,500,30);INSERT INTO emp VALUES(7566,\'JONES\',\'MANAGER\',7839,\'1981-04-02\',2975,NULL,20);INSERT INTO emp VALUES(7654,\'MARTIN\',\'SALESMAN\',7698,\'1981-09-28\',1250,1400,30);INSERT INTO emp VALUES(7698,\'BLAKE\',\'MANAGER\',7839,\'1981-05-01\',2850,NULL,30);INSERT INTO emp VALUES(7782,\'CLARK\',\'MANAGER\',7839,\'1981-06-09\',2450,NULL,10);INSERT INTO emp VALUES(7788,\'SCOTT\',\'ANALYST\',7566,\'1987-04-19\',3000,NULL,20);INSERT INTO emp VALUES(7839,\'KING\',\'PRESIDENT\',NULL,\'1981-11-17\',5000,NULL,10);INSERT INTO emp VALUES(7844,\'TURNER\',\'SALESMAN\',7698,\'1981-09-08\',1500,0,30);INSERT INTO emp VALUES(7876,\'ADAMS\',\'CLERK\',7788,\'1987-05-23\',1100,NULL,20);INSERT INTO emp VALUES(7900,\'JAMES\',\'CLERK\',7698,\'1981-12-03\',950,NULL,30);INSERT INTO emp VALUES(7902,\'FORD\',\'ANALYST\',7566,\'1981-12-03\',3000,NULL,20);INSERT INTO emp VALUES(7934,\'MILLER\',\'CLERK\',7782,\'1982-01-23\',1300,NULL,10);INSERT INTO dept VALUES(10, \'ACCOUNTING\', \'NEW YORK\');INSERT INTO dept VALUES(20, \'RESEARCH\', \'DALLAS\');INSERT INTO dept VALUES(30, \'SALES\', \'CHICAGO\');INSERT INTO dept VALUES(40, \'OPERATIONS\', \'BOSTON\');SELECT *FROM emp-- 查询出高于10号部门的平均工资的员工信息SELECT * FROM emp WHERE sal>(SELECT AVG(sal) FROM emp WHERE deptno=10)-- 查询出比10号部门任何员工薪资高的员工信息SELECT * FROM emp WHERE sal>(SELECT sal FROM emp WHERE deptno=10 ORDER BY sal LIMIT 1)-- 和10号部门同名同工作的员工信息SELECT * FROM emp WHERE deptno IN(20,30,40)AND (ename=(SELECT ename FROM emp)AND job=(SELECT job FROM emp))-- 获取员工的名字和部门的名字SELECT e.ename,d.dname FROM emp e INNER JOIN dept d ON e.deptno=d.deptno-- 查询emp表中经理信息SELECT a.* FROM (SELECT * FROM emp WHERE job=\'MANAGER\') a-- 薪资高于10号部门平均工资的所有员工信息SELECT * FROM emp WHERE sal>(SELECT AVG(sal) FROM emp GROUP BY deptno HAVING deptno=10)-- 有哪些部门的平均工资高于30号部门的平均工资SELECT deptno,AVG(sal) avgsal FROM emp GROUP BY deptno HAVING avgsal>(SELECT AVG(sal) FROM emp WHERE deptno=30)-- 工资>JONES工资SELECT * FROM emp WHERE sal>(SELECT sal FROM emp WHERE ename=\'JONES\')-- 查询与SCOTT同一个部门的员工SELECT * FROM emp WHERE deptno IN(SELECT deptno FROM emp WHERE ename=\'SCOTT\')-- 工资高于30号部门所有人的员工信息SELECT * FROM emp WHERE sal>(SELECT sal FROM emp WHERE deptno=30 ORDER BY sal DESC LIMIT 1)-- 查询工作和工资与MARTIN完全相同的员工信息SELECT * FROM emp WHERE sal=(SELECT sal FROM emp WHERE job=(SELECT job FROM emp WHERE ename=\'MARTIN\')AND ename=\'MARTIN\')-- 有两个以上直接下属的员工信息SELECT * FROM emp WHERE empno IN(SELECT mgr FROM emp GROUP BY mgr HAVING COUNT(mgr)>1)-- 查询员工编号为7788的员工名称,员工工资,部门名称,部门地址SELECT e.ename,e.sal,d.dname,d.loc FROM dept d INNER JOIN (SELECT * FROM emp) e ON d.deptno=e.deptno WHERE e.empno=\'7788\'--  查询出高于本部门平均工资的员工信息SELECT * FROM emp e WHERE sal>(SELECT AVG(sal) FROM emp GROUP BY deptno HAVING deptno=e.deptno)--. 列出达拉斯加工作的人中,比纽约平均工资高的人SELECT * FROM emp e INNER JOIN dept d ON e.deptno=d.deptno WHERE e.sal>(SELECT AVG(sal) FROM emp e INNER JOIN dept d ON e.deptno=d.deptno WHERE d.loc=\'NEW YORK\')AND d.loc=\'DALLAS\'-- 查询7369员工编号,姓名,经理编号和经理姓名SELECT e.empno 编号,e.ename 姓名,m.empno 经理编号,m.ename 经理姓名 FROM emp m INNER JOIN (SELECT * FROM emp WHERE empno=\'7369\') e ON m.deptno=e.deptno WHERE m.empno=e.mgr--  查询出各个部门薪水最高的员工所有信息SELECT * FROM emp e WHERE sal=(SELECT MAX(sal) FROM emp GROUP BY deptno HAVING deptno=e.deptno)-- 如果有需要解释的留下评论,代码太多无法都解释了,我还在上学没有时间,抱歉抱歉有时间解释我会慢慢更新的--评论带这题就行了
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » MySQL子查询学习(练习)