动力节点经典34道MySQL面试题(中)

11~取得最后入职的5名员工
SELECT ENAME,HIREDATE
FROM emp
ORDER BY HIREDATE DESC
LIMIT 5
12~取得每个薪水等级有多少员工
SELECT t.GRADE,count(GRADE)
FROM (SELECT e.ENAME,e.SAL,s.GRADE
FROM emp e
JOIN salgrade s
ON e.SAL BETWEEN s.LOSAL AND s.HISAL

) t
GROUP BY t.GRADE
ORDER BY GRADE
13~列出所有员工以及领导的名字
SELECT e1.ENAME employname,e2.ENAME Leadername
FROM emp e1
LEFT JOIN emp e2
ON e2.EMPNO=e1.MGR
14~列出受雇日期早于其直接上级的所有员工编号、姓名、部门名称
SELECT t.EMPNO,t.ENAME,d.DNAME
FROM (SELECT e1.EMPNO,e1.ENAME,e1.DEPTNO
FROM emp e1
JOIN emp e2
ON e2.EMPNO=e1.MGR AND e1.HIREDATE < e2.HIREDATE
) t
JOIN dept d
ON t.DEPTNO=d.DEPTNO
15~列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
SELECT d.DNAME,e.*
FROM dept d
LEFT JOIN emp e
ON e.DEPTNO=d.DEPTNO
ORDER BY d.DNAME
16~列出至少有5个员工的所有部门
SELECT d.DEPTNO,d.DNAME,t.total
FROM(SELECT DEPTNO,count(ENAME) total 
FROM emp
GROUP BY DEPTNO
HAVING count(ENAME)>=5
ORDER BY DEPTNO
) t
JOIN dept d
ON d.DEPTNO=t.DEPTNO
17~列出薪水比"SMITH"多的所有员工信息
SELECT *
FROM emp 
WHERE SAL>(SELECT SAL
FROM emp
WHERE ENAME ="SMITH")
18~列出所有"CLERK”(办事员)的姓名及其部门名称,部门人数
SELECT t1.*,t2.total
FROM (SELECT d.DEPTNO,e.ENAME,d.DNAME
FROM dept d
JOIN emp e
ON e.DEPTNO=d.DEPTNO
WHERE JOB="CLERK"
) t1
JOIN (SELECT DEPTNO,count(ENAME) total 
FROM emp
GROUP BY DEPTNO
ORDER BY DEPTNO
) t2
ON t1.DEPTNO=t2.DEPTNO
ORDER BY DEPTNO
19~列出最低薪水大于1500的各种工作及从事此工作的全部雇员人数
SELECT t2.*
FROM (SELECT DISTINCT job,min(SAL) minsal
FROM emp
GROUP BY JOB
HAVING minsal>=1500
) t1
JOIN (SELECT JOB,count(JOB)
FROM emp
GROUP BY JOB
) t2
ON t1.JOB=t2.JOB
20~列出在部门"SALES"<销售部>工作的员工的姓名,假定不知道销售部门的部门编号
SELECT e.ENAME,t.DNAME
FROM emp e
JOIN (SELECT DEPTNO,DNAME 
FROM dept
WHERE DNAME="SALES"
) t
ON e.DEPTNO=t.DEPTNO
 

你可能感兴趣的:(MySQL)