mysql 综合练习(二) 续练习(一)

荒凉一梦 提交于 2020-08-17 17:58:49

…………续练习(一)
“*”表示难易程度
– 18、查询员工和及其所属经理的姓名 *****

SELECT e.ename , m.ename AS mgrname FROM emp e LEFT OUTER JOIN emp m
ON e.mgr = m.empno; SELECT e.ename , m.ename AS mgrname FROM emp e ,
emp m WHERE e.mgr = m.empno;

– 19、查询雇佣日期早于其经理雇佣日期的员工及其经理姓名 *****

SELECT e.ename,m.ename AS mgrname FROM emp e,emp m WHERE e.mgr=m.empno
AND e.hiredate < m.hiredate; SELECT e.ename,m.ename AS mgrname FROM
emp e INNER JOIN emp m ON e.mgr = m.empno WHERE e.hiredate <
m.hiredate;


– 20、查询员工姓名及其所在的部门名称

SELECT e.ename,d.dname FROM emp e , dept d WHERE e.deptno = d.deptno;

– 21、查询工作为clerk的员工姓名及其所在部门名称

SELECT e.ename,d.dname FROM emp e , dept d WHERE e.deptno = d.deptno
AND e.job = ‘CLERK’;

– 22、查询各部门号及其部门中的最低工资 ***

SELECT deptno ,MIN(sal) AS minsal FROM emp GROUP BY deptno;

– 23、查询销售部sales的所有员工的姓名 *****

SELECT ename FROM emp WHERE deptno=(SELECT deptno FROM dept WHERE
dname=‘sales’); SELECT e.ename FROM emp e,dept d WHERE e.deptno =
d.deptno AND d.dname = ‘sales’;

– 24、查询工资水平大于平均工资的员工 *****

SELECT * FROM emp e WHERE e.sal > (SELECT AVG(sal) FROM emp);

– 25、查询与SCOTT从事相同工作的员工。 *****

SELECT * FROM emp WHERE job = (SELECT job FROM emp WHERE ename =
‘scott’);

– 26、查询与部门编号为30的员工工资水平相同的员工信息

SELECT * FROM emp WHERE sal IN (SELECT sal FROM emp WHERE deptno =
30);

– 27、查询工资高于部门编号为30的部门所有员工工资水平的员工信息 *****

SELECT * FROM emp WHERE sal > ALL(SELECT sal FROM emp WHERE deptno =
30);

SELECT * FROM emp WHERE sal > (SELECT MAX(sal) FROM emp WHERE
deptno = 30);

– 28、查询部门号、部门名、部门所在位置及其每个部门的员工总数 *****

SELECT d.deptno,d.dname,d.loc,COUNT(e.deptno) AS number FROM dept
d,emp e WHERE d.deptno = e.deptno GROUP BY e.deptno;

– 29、查询员工的姓名、工资及其所属部门

SELECT ename,dname,sal FROM emp ,dept WHERE emp.deptno = dept.deptno;

– 30、查询员工的详细信息(含部门名)

SELECT e.* , d.dname FROM emp e, dept d WHERE e.deptno = d.deptno;

– 31、查询各工作名称以及从事此工作的最低工资 *****

SELECT job , MIN(sal) AS minsal FROM emp GROUP BY job;

– 32、计算员工的年薪并且以年薪排序 ***

SELECT ename, sal * 12 AS yearSalary FROM emp ORDER BY yearSalary;

– 33、查询工资为第4级别的员工的姓名及其工资 *****

SELECT ename,sal FROM emp e ,salgrade s WHERE e.sal >= s.losal AND
e.sal <= s.hisal AND s.grade = 4; SELECT emp.ename,emp.sal FROM emp
,(SELECT losal,hisal FROM salgrade WHERE grade=4) g WHERE emp.sal
BETWEEN g.losal AND g.hisal;


– 34、查询工资为第3等级的职员名字、工资、所属部门以及部门所在地 *****

SELECT ename ,sal ,dname ,loc FROM emp,dept,salgrade WHERE emp.deptno
= dept.deptno AND grade = 3 AND sal >= losal AND sal < hisal;

– 35、查询工资等级大于smith的员工的信息 *****

SELECT e.* FROM emp e, salgrade s WHERE s.hisal < e.sal AND s.grade =
(SELECT grade FROM salgrade s ,emp e WHERE s.losal < e.sal AND
s.hisal > e.sal AND e.ename = ‘smith’);

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!