一、60题
1. 案例:查询没有上级领导的员工的编号,姓名,工资
select empno,ename,sal from emp where mgr is null;
2. 案例:查询emp表中没有奖金的员工的姓名,职位,工资,以及奖金
select ename,job,sal,comm from emp where comm is null;3. 案例:查询emp表中含有奖金的员工的编号,姓名,职位,以及奖金
select empno,ename,job,comm from emp where comm is not null;
4. 案例:查询含有上级领导的员工的姓名,工资以及上级领导的编号
select ename,sal,mgr from emp where mgr is not null;
5. 案例:查询emp表中名字以‘S’开头的所有员工的姓名
select ename from emp where ename like "S%";6. 案例:查询emp表中名字的最后一个字符是'S'的员工的姓名
select ename from emp where ename like "%S";
7. 案例:查询倒数的第2个字符是‘E’的员工的姓名
select ename from emp where ename like '%E_';
8. 案例:查询emp表中员工的倒数第3个字符是‘N’的员工姓名
select ename from emp where ename like "%N_ _ _";
9. 案例:查询emp表中员工的名字中包含‘A’的员工的姓名 select ename from emp where ename like '%A%';
10. 案例:查询emp表中名字不是以'K'开头的员工的所有信息
select * from emp where ename not like 'K%';11. 案例:查询emp表中名字中不包含‘A’的所有员工的信息
select * from emp where ename not like '%A%';12. 案例:做文员的员工人数(job\_id 中 含有 CLERK 的)
select count(*) from emp where job like '%CLERK%';
13. 案例:销售人员 job: SALESMAN 的最高薪水
select max(sal) from emp where job = 'SALESMAN' ;
14. 案例:最早和最晚入职时间
select max(Hiredate),min(hiredate) from emp ;
15. 案例:查询类别 163的商品总库存量
select sum(num) from t_item where category_id = 163;
16. 案例:查询 类别 163 的商品
select * from t_item where category_id = 163;17. 案例:查询商品价格不大于100的商品名称列表
select * from t_item where price <= 100;18. 案例:查询品牌是联想,且价格在40000以上的商品名称和价格
select * from t_item where title = '联想' and price >= 40000;19. 案例:查询品牌是三木,或价格在50以下的商品名称和价格
select * from t_item where title = '三木' or price <= 50;
20. 案例:查询品牌是三木、广博、齐心的商品名称和价格
select title,price from t_item where title like in ('%联想%','%三木%','%齐心%');
where title like '%联想%' or title like "%三木%";21. 案例:查询品牌不是联想、戴尔的商品名称和价格
select title,price from t_item where title like '%联想%' or title like '%戴尔%';22. 案例:查找品牌是联想且价格大于10000的电脑名称
select title from t_item where title like '联想' and price >10000;
23. 案例:查询联想或戴尔的电脑名称列表
select title from t_item where title like '联想' and title like '戴尔';24. 案例:查询联想、戴尔、三木的商品名称列表
select title from t_item where title like "%戴尔%"
or title like '%联想%' or title like "%三木%";25. 案例:查询不是戴尔的电脑名称列表
select title from t_item where title not like '%戴尔%';26. 案例:查询所有是记事本的商品品牌、名称和价格
select title,left(2,title),price from t_item where title like '%记事本%';27. 案例:查询品牌是末尾字符是'力'的商品的品牌、名称和价格
select title from t_item where title like '%力';28. 案例:名称中有联想字样的商品名称
select title from t_item where title like '%联想%';29. 案例:查询卖点含有'赠'产品名称
select title from t_item where sell_point like "%赠%";30. 案例:查询emp表中员工的编号,姓名,职位,工资,并且工资在1000\~2000之间。
select EMPNO,ename,job,sal from emp where sal between 1000 and 2000;31. 案例:查询emp表中员工在10号部门,并且含有上级领导的员工的姓名,职位,上级领导编号以及所属部门的编号
select ename, job,mgr ,deptno from emp where deptno = 10 and mgr is not null;
32. 案例:查询emp表中名字中包含'E',并且职位不是MANAGER的员工的编号,姓名,职位,以及工资。
select deptno,ename,job,sal from emp where job != "MANAGER" and ename like "%E%";33. 案例:查询emp表中10号部门或者20号部门中员工的编号,姓名,所属部门的编号
select ename,id,deptno from emp where deptno in(10,20);
34. 案例:查询emp表中没有奖金或者名字的倒数第2个字母不是T的员工的编号,姓名,职位以及奖金
select deptno,ename,job from emp where comm is null or ename not like
'%T_';
35. 案例:查询工资高于3000或者部门编号是30的员工的姓名,职位,工资,入职时间以及所属部门的编号select ename,job,sal,hiredate ,deptno from emp where sal >3000 or deptno =30;
36. 案例:查询不是30号部门的员工的所有信息
select * from emp where deptno !=30;37. 案例:查询奖金不为空的员工的所有信息
select * from emp where comm is not null;38. 案例:查询emp表中所有员工的编号,姓名,职位,根据员工的编号进行降序排列
select id, ename ,job from emp group by id desc;
39. 案例:查询emp表中部门编号是10号或者30号中,所有员工姓名,职务,工资,根据工资进行升序排列
select ename,job,sal from emp where deptno in (10,30) GROUP BY sal;
40. 案例:查询emp表中所有的数据,然后根据部门的编号进行升序排列,如果部门编号一致,根据员工的编号进行降序排列
select * from emp ORDER BY deptno ,empno desc;41. 案例:查询emp表中工资高于1000或者没有上级领导的员工的编号,姓名,工资,所属部门的编号,以及上级领导的编号,根据部门编号进行降序排列,如果部门编号一致根据工资进行升序排列。
select empno , enmae ,sal , deptno ,mgr where sal>1000 or mgr is null ORDER BY deptno ,sal ;42. 案例:查询emp表中名字中不包含S的员工的编号,姓名,工资,奖金,根据工资进行升序排列,如果工资一致,根据编号进行降序排列
select deptno,ename ,sal ,comm where ename not like "%S%" order by sal ,empno;43. 案例:统计emp表中员工的总数量
select count(*) from emp;
44. 案例:统计emp表中获得奖金的员工的数量
select count(*) from emp where comm is not null;45. 案例:求出emp表中所有的工资累加之和
select sum(sal) from emp;46. 案例:求出emp表中所有的奖金累加之和
select sum(comm) from emp;47. 案例:求出emp表中员工的平均工资
select avg(sal) from emp;
48. 案例:求出emp表中员工的平均奖金
select avg(comm) from emp;
49. 案例:求出emp表中员工的最高工资
select max(sal) from emp;
50. 案例:求出emp表中员工编号的最大值
select max(empno) from emp;
51. 案例:查询emp表中员工的最低工资。
select min(sal) from emp;
52. 案例:查询emp表中员工的人数,工资的总和,平均工资,奖金的最大值,奖金的最小值,并且对返回的列起别名。
select count(*) c,sum(sal) s,avg(sal) a,max(comm) ,min(comm) from emp;
53. 案例:查询emp表中每个部门的编号,人数,工资总和,最后根据人数进行升序排列,如果人数一致,根据工资总和降序排列。
select deptno,count(*) c,sum(sal) s from emp GROUP BY deptno
order by c,s ;
54. 案例:查询工资在1000\~3000之间的员工信息,每个部门的编号,平均工资,最低工资,最高工资,根据平均工资进行升序排列。
select deptno,avg(sal) a from emp where sal between 1000 and 3000 GROUP BY deptno order by a ;
55. 案例:查询含有上级领导的员工,每个职业的人数,工资的总和,平均工资,最低工资,最后根据人数进行降序排列,如果人数一致,根据平均工资进行升序排列
select count(*) c,avg(sal) a from emp where mgr is not null GROUP BY job
ORDER BY c desc , a;56. 案例:查询工资在1000\~3000之间每一个员工的编号,姓名,职位,工资
select empno , ename, job, sal from emp where sal between 1000 and 3000;57. 案例:查询emp表中奖金在500\~2000之间所有员工的编号,姓名,工资以及奖金
select * from emp where sal between 500 and 3000;
58. 案例:查询员工的编号是7369,7521,
select * from emp where empno in(7369,7521);59. 案例:查询emp表中,职位是ANALYST,
select * from emp where job = "ANALYST" ;
60. 案例:查询emp表中职位不是ANALYST
select * from emp where job != "ANALYST" ;
二、21题多表查询
1. 每个部门的人数,根据人数降序排序
select count(*) from emp GROUP BY deptno order by count(*) desc;
2. 每个部门中,每个主管的手下人数
select count(*) from emp where mgr is not null GROUP BY deptno,mgr ;
3. 每种工作的平均工资
select avg(sal) from emp group by job;
4. 每年的入职人数
select count(*) from emp group by EXTRACT(year by hiredate);
5. 少于等于3个人的部门信息
select deptno from emp GROUP BY deptno HAVING count(*) <= 3;
select * from dept where deptno in (select deptno from emp GROUP BY deptno HAVING count(*) <= 3);
6. 拿最低工资的员工信息
select * from emp having min(sal);select min(sal) from emp;
select * from emp where sal =(select min(sal) from emp);
7. 只有一个下属的主管信息
select mgr from emp GROUP BY mgr having count(*) = 1;
select * from emp where EMPNO in(select mgr from emp GROUP BY mgr having count(*) = 1);
8. 每月发工资最多的部门信息
select sum(sal) from emp GROUP BY deptno order by sum(sal) desc limit 1;select deptno from emp GROUP BY DEPTNO HAVING sum(sal)= (select sum(sal) from emp GROUP BY deptno order by sum(sal) desc limit 1
)select * from dept where deptno in (select deptno from emp GROUP BY DEPTNO HAVING sum(sal)= (select sum(sal) from emp GROUP BY deptno order by sum(sal) desc limit 1
));
9. 下属最多的人,查询其个人信息
select mgr from emp GROUP BY mgr HAVING count(*) ORDER BY mgr desc limit 1;
select * from emp where EMPNO = (select mgr from emp GROUP BY mgr HAVING count(*) ORDER BY mgr desc limit 1);
--- 9
最多的人数根据人数找领导编号
找领导信息
10. 拿最高工资员工的同事信息
select max(sal) from empselect deptno from emp where sal = (select max(sal) from emp);
select * from emp where DEPTNO=(select deptno from emp where sal = (select max(sal) from emp)) and sal !=(select max(sal) from emp
);11. 和最后入职的员工在同一部门的员工信息
select max(hiredate) from emp ;
select deptno from emp where HIREdate =(select max(hiredate) from emp)select * from emp where DEPTNO=(select deptno from emp where HIREdate =(select max(hiredate) from emp) and hiredate = (select max(hiredate) from emp )
)
12. 查询平均工资高于20号平均工资的部门信息
select avg(sal) from emp where DEPTNO =20;
select DEPTNO from emp GROUP BY DEPTNO HAVING avg(sal) > (select avg(sal) from emp where DEPTNO =20);
select * from dept where DEPTNO =(select DEPTNO from emp GROUP BY DEPTNO HAVING avg(sal) > (select avg(sal) from emp where DEPTNO =20));
13. 查询员工信息和员工对应的部门名称
select e.*,d.dname from emp e, dept d where d.deptno = e.deptno;
14. 查询员工信息,部门名称,所在城市
select e.*,d.dname , d.loc from emp e,dept d where d.DEPTNO = e.DEPTNO;
15. 查询Dallas市所有的员工信息
select e.*
from emp e
left join dept d
on e.deptno = d.deptno
where d.loc = 'Dallas';
16. 查询每个城市的员工人数(需要查询出 波士顿0人)
select count(job),d.loc
from emp e
right join dept d
on e.DEPTNO = d.DEPTNO
GROUP BY d.loc
17. 查询每个员工的名字和对应的主管名字
select mgr from emp e
select ename from emp where mgr in (select mgr from emp e)select e1.ename ,e2.ename
from emp e1
left join emp e2
on e1.mgr = e2.empno;
18. 查询每个员工的名字、对应的主管名字和对应的部门名字
select e1.ename,e2.ename , d.dname
from emp e1
left join emp e2
on e1.mgr = e2.empno
left join dept d
on e1.deptno =d.deptno
19. 查询emp表中所有员工的姓名以及该员工的领导编号,领导姓名,领导职位
select
e1.ename ,e1.mgr,e2.ename,e2.job
from
emp e1left join
emp e2
on
e1.mgr = e2.empno
20. 查询emp表名字中没有字母k的所有员工的编号,姓名,职位,工资,部门名
select
e.empno,e.ename,e.job,e.sal,d.dname
from
emp e
left join
dept d
on e.deptno = d.deptno
where e.ename not like '%k%';
21. 查询dept表中所有部门的信息和与之关联的员工名字和工资,排除掉10号部门,根据部门编号降序排序,如果部门编号一致则按照工资升序排序
select
d.*,e.ename,e.sal
from
emp e
left join
dept d
on
e.deptno = d.deptno
where d.deptno != 10
order by deptno desc, sal ;
三、条件判断函数
-- if(value,t,f)
select ename, salary,if(salary>10000,'高薪','正常') from t_employee;-- 计算实发工资
# 实发工资 = salary + salary * commission_pct
select ename,salary 工资, salary+salary*commission_pct 实发工资 from t_employee #出错select ename,salary 工资, salary+ salary*ifnull(commission_pct,0) 实发工资 from t_employee
# 查询员工的编号,姓名,薪资, 薪资等级,等级根据薪资判断
# 如果薪资 > 20000 显示 十分羡慕
# 如果薪资 15000-20000 有点嫉妒
# 如果 10000-15000 努力级别
# 5000-10000 平均级别
# 小于5000 低保级别
select eid,ename,salary,
case when salary>20000 then '十分羡慕'
when salary> 15000 then '有点嫉妒'
when salary>10000 then '努力级别'
when salary>5000 then '平均级别'
else '低保级别'
end as '工资等级'
from t_employee;
#查询一个员工在多少个地方工作过(计算工作地点的数量)
/*
统计 worke_place字段中的","个数+1
*/
#统计出了逗号的数量,
select char_length('北京,深圳,上海') - char_length(replace('北京,深圳,上海',',',''))
select work_place, (char_length(work_place) - char_length(replace(work_place,',',''))+1) 城市数量 from t_employee;
select ename, (char_length(work_place) - char_length(replace(work_place,',',''))+1) 城市数量 from t_employee;
select ename, work_place,
case (char_length(work_place) - char_length(replace(work_place,',',''))+1)
when 1 then '固定在一个地方工作'
when 2 then '这哥们两地奔波'
when 3 then '这哥们属于频繁流动人口'
else '频繁出差'
end as '工作状态'
from t_employee;