有错欢迎指出...
01.查询员工表所有数据,并说明使用*的缺点
SELECT * FROM emp;
使用*的缺点:把不必要的列也查询出来了,而且效率不如直接指定列名
02.查询职位(JOB)为'PRESIDENT'的员工的工资
SELECT sal FROM emp WHERE job='PRESIDENT';
03.查询佣金(COMM)为0或为NULL的员工信息
SELECT * FROM emp WHERE comm=0.00 OR comm IS NULL;
04.查询入职日期在1982-5-1到1981-12-31之间的所有员工信息
SELECT * FROM emp WHERE hiredate BETWEEN STR_TO_DATE('1981-12-31','%Y-%m-%d') AND STR_TO_DATE('1982-5-1','%Y-%m-%d');
05.查询所有名字长度为4的员工的员工编号,姓名
SELECT empno,ename FROM emp WHERE LENGTH(ename)=4;
06.显示10号部门的所有经理('MANAGER')和20号部门的所有职员('CLERK')的详细信息
SELECT * FROM emp
WHERE (job='MANAGER' AND deptno=10) OR (deptno=20 AND job='CLERK');
07.显示姓名中没有'L'字的员工的详细信息或含有'SM'字的员工信息
SELECT * FROM emp WHERE ename NOT LIKE '%L%' OR ename LIKE '%SM%';
08.显示各个部门经理('MANAGER')的工资
SELECT sal FROM emp WHERE job='MANAGER';
09.显示佣金(COMM)收入比工资(SAL)高的员工的详细信息
SELECT * FROM emp WHERE IFNULL(comm,0)>IFNULL(sal,0);
10.把hiredate列看做是员工的生日,求本月过生日的员工(考察知识点:单行函数)
SELECT * FROM emp WHERE MONTH(hiredate) = MONTH(SYSDATE());
11.把hiredate列看做是员工的生日,求下月过生日的员工(考察知识点:单行函数)
SELECT * FROM emp WHERE MONTH(hiredate)=MONTH(SYSDATE())+1;
12.求1982年入职的员工(考察知识点:单行函数)
SELECT * FROM emp WHERE YEAR(hiredate)=1982;
13.求1981年下半年入职的员工(考察知识点:单行函数)
SELECT * FROM emp WHERE hiredate BETWEEN STR_TO_DATE('1981-6-1','%Y-%m-%d') AND STR_TO_DATE('1981-12-31','%Y-%m-%d');
14.求1981年各个月入职的员工个数(考察知识点:组函数)
SELECT EXTRACT(MONTH FROM hiredate) AS months, COUNT(1) FROM emp
WHERE EXTRACT(YEAR FROM hiredate)='1981'
GROUP BY EXTRACT(MONTH FROM hiredate) ORDER BY months;
15.查询各个部门的平均工资
SELECT deptno,AVG(sal) FROM emp GROUP BY deptno;
16.显示各种职位的最低工资
SELECT job,MIN(sal) FROM emp GROUP BY job;
17.按照入职日期由新到旧排列员工信息
SELECT * FROM emp ORDER BY hiredate DESC;
18.查询员工的基本信息,附加其上级的姓名
SELECT t1.empno,t1.ename,t1.job,t1.hiredate,t1.sal,t1.comm,t1.deptno,t2.ename
FROM emp t1 JOIN emp t2 ON t1.mgr=t2.empno;
19.显示工资比'ALLEN'高的所有员工的姓名和工资
SELECT t1.ename,t1.sal FROM emp t1 JOIN emp t2 ON IFNULL(t1.sal,0)> IFNULL(t2.sal,0) WHERE t2.ename='ALLEN';
20.显示与'SCOTT'从事相同工作的员工的详细信息
SELECT t1.empno,t1.ename,t1.job,t1.mgr,t1.hiredate,t1.sal,t1.comm,t1.deptno FROM emp t1 JOIN
emp t2 ON (t2.ename='SCOTT' AND t1.job=t2.job )AND t1.ename!='SCOTT';
21.显示销售部('SALESMAN')员工的姓名
select ename from emp t1, dept t2 where t1.deptno = t2.deptno and t2.dname='SALESMAN';
22.显示与30号部门'MARTIN'员工工资相同的员工的姓名和工资
SELECT t1.ename,t1.sal FROM emp t1 JOIN emp t2 ON t1.sal=t2.sal WHERE t1.deptno=30 AND t2.ename='MARTIN';
23.查询所有工资高于平均工资的销售人员('SALESMAN')
SELECT * FROM emp WHERE job='SALESMAN' AND sal > (SELECT AVG(sal) FROM emp);
24.显示所有职员的姓名及其所在部门的名称和工资
SELECT t1.ename,t1.sal,t2.dname FROM emp t1 JOIN dept t2 ON t1.deptno=t2.deptno ;
25查询在研发部('RESEARCH')工作员工的编号,姓名,工作部门,工作所在地
SELECT empno,ename,dname,loc FROM emp t1, dept t2
WHERE t1.deptno = t2.deptno AND dname='RESEARCH';
26.查询各个部门的名称和员工人数
SELECT deptno,COUNT(*) FROM emp GROUP BY deptno;
27.查询各个职位员工工资大于平均工资(平均工资包括所有员工)的人数和员工职位
SELECT COUNT(*),job FROM emp WHERE sal>(SELECT AVG(IFNULL(sal,0)) FROM emp ) GROUP BY job;
28.查询工资相同的员工的工资和姓名
SELECT t1.empno,t1.ename,t1.job,t1.mgr,t1.hiredate,t1.sal,t1.comm,t1.deptno
FROM emp t1 JOIN emp t2 ON t1.sal=t2.sal WHERE t1.empno!=t2.empno ;
29.查询工资最高的3名员工信息
SELECT * FROM emp ORDER BY sal DESC LIMIT 0,3;
30.按工资进行排名,排名从1开始,工资相同排名相同(如果两人并列第1则没有第2名,从第3名继续排)
SELECT t1.*, (SELECT COUNT(*) FROM emp
WHERE IFNULL(sal,0)> IFNULL(t1.sal,0))+1 rank
FROM emp t1 ORDER BY rank;
31.求入职日期相同的(年月日相同)的员工
SELECT t1.* FROM emp t1 JOIN emp t2
ON t1.hiredate=t2.hiredate WHERE t1.empno!=t2.empno ;
32.查询每个部门的最高工资
SELECT MAX(IFNULL(sal,0)),deptno FROM emp GROUP BY deptno;
33.查询每个部门,每种职位的最高工资
SELECT deptno, job, MAX(IFNULL(sal,0)) FROM emp GROUP BY deptno,job;
34.查询每个部门有多少人
SELECT deptno,COUNT(t.empno) FROM emp t GROUP BY t.deptno;