1.查询所有的部门编号:
select deptno
from emp
select deptno
from dept
2.查询所有有人的部门编号:
select ename,deptno
from emp
3.查询所有岗位名称:
select job
from emp
4.查询所有薪水超过两千的员工信息
select *
from emp
where sal >2000
5.查询所有20部门的员工姓名,编号及薪水
select ename,empno,sal
from emp
where deptno = 20
6.查询所有没有奖金的员工信息
select *
from emp
where comm is null or comm = 0
7.查询所有有奖金的员工信息
select *
from emp
where comm is not null and comm != 0
8.查询最高领导的员工信息
select *
from emp
where mgr is null
9.查询所有81年之后入职的员工信息
select *
from emp
where hiredate > '1981-12-31'
10.查询所有薪水在2000-4000范围内的员工信息
select *
from emp
where sal between 2000 and 4000
11.查询所有部门编号是10或30的员工信息
select *
from emp
where deptno in (10,20)
12.查询所有20部门并且薪水超过2000的员工信息:
select *
from emp
where deptno = 20 and sal > 2000
13.查询所有薪水不在2000-4000范围内的员工信息
select *
from emp
where sal <2000 or sal >4000
14.查询所有部门编号不是10,30的员工信息
select *
from emp
where deptno not in (10,30)
15.查询用户名为scott的员工信息:注意区分大小写
select *
from emp
where binary ename = 'scott'
16.查询姓名里面包含ALL的员工姓名
select ename
from emp
where ename like '%ALL%'
17.查询所有以”S”开头的同学
select *
from emp
where ename like 'S%'
18.查询第二个字母为A的员工姓名
select ename
from emp
where ename like '_A%'
19.查询所有员工的编号、姓名、部门编号、职位、薪水,按照薪水降序排列
select empno,ename,deptno,job,sal
from emp
order by sal desc
20.查询所有员工信息,按照部门降序排列,部门内按照薪水升序排列
select *
from emp
order by deptno desc,sal
21.查询姓名中包含‘A’员工的姓名,编号,薪水,按照薪水降序排列
select ename,empno,sal
from emp
where ename like '%A%'
order by sal desc
22.查询年收入超过10000的员工的姓名,编号,薪水,年收入,按照年收入降序排列
select ename,empno,sal,sal*12 yearsal
from emp
where sal*12>10000
order by yearsal desc
23.查询年薪超过10000的员工的姓名,编号,薪水,年收入,按照年薪降序排列
select ename,empno,sal,sal*12 yearsal
from emp
where sal*12>10000
order by yearsal desc
后续练习题:
24.查询雇员表中,姓名为SMITH的雇员,截止到今天共工作了多少周,则可以使用如下的SQL语句
select timestampdiff(week,hiredate,now())
from emp
where ename = 'SMITH'
25.查询各部门的最高薪水、最低薪水、平均薪水…..
select max(sal),min(sal),avg(sal)
from emp
group by deptno
26.查询‘SMITH’的领导姓名
select ename
from emp
where ename = (select mgr from emp where ename ='SMITH')
27.查询部门名称是‘SALES’的员工信息
select *
from emp
where deptno = (select deptno from dept where dname='SALES')
28.查询公司中薪水最高的员工信息
select *
from emp
where sal = (select max(sal) from emp)
29.查询公司所有员工的个数
select count(empno)
from emp
30.查询公司中最高薪水是多少
select max(sal)
from emp
31.查询公司中平均奖金是多少
select avg(ifnull(sal,0))
from emp
32.查询公司中最晚入职的时间
select max(hiredate)
from emp
33.查询公司中有奖金的人数.
select count(empno)
from emp
where comm is not null
34.查询20部门的最高薪水是多少.
select max(sal)
from emp
where deptno = 20
35.查询各部门的平均薪水及部门编号,部门名称。
select deptno,dname,avg(sal)
from emp e join dept d
on e.deptno = d.deptno
group by deptno,dname
36.查询各部门中最高薪水的员工编号,姓名…
select empno,ename
from emp e
where sal = (select max(sal) from emp where deptno = e.deptno)
37.查询所有员工姓名中包含‘A’的最高薪水
select max(sal)
from emp
where ename like '%A%'
38.查询各岗位的最高薪水,最低薪水。要求只统计薪水>1000的
select max(sal), min(sal)
from emp
where sal > 1000
group by job
39.查询各部门的平均薪水及部门编号,要求只列出平均薪水>2000
select deptno,avg(sal) avgsal
from emp
group by deptno
having avgsal > 2000
40.查询各部门的平均薪水及部门编号,要求只有员工姓名中包含‘A’才参与统计,只列出平均薪水>1500的,按照平均薪水降序排列
select deptno,avg(sal) avgsal
from emp
where ename like '%A%'
GROUP BY deptno
having avgsal > 1500
order by avgsal desc
41.查询各部门最高薪水的员工信息
select *
from emp e
where sal =(select max(sal) from emp where deptno = e.deptno)
42.查询最高薪水的员工信息
select *
from emp e
where sal =(select max(sal) from emp where deptno = e.deptno)
43.查询薪水大于该部门平均薪水的员工信息
select *
from emp e
where sal > (select avg(sal) from emp where deptno = e.deptno)
44.查询最高薪水的员工信息
select *
from emp e
where sal =(select max(sal) from emp where deptno = e.deptno)
45.查询各部门最高薪水的员工信息
select *
from emp e
where sal =(select max(sal) from emp where deptno = e.deptno)
46.查询‘SMITH’的领导姓名
select ename
from emp
where ename = (select mgr from emp where ename ='SMITH')
47.查询部门名称是‘SALES’的员工信息
select *
from emp
where deptno = (select deptno from dept where dname='SALES')
48.查询公司中薪水最高的员工信息
select *
from emp e
where sal =(select max(sal) from emp where deptno = e.deptno)
49.查询薪水等级为4的员工信息
select *
from emp e join salgrade s
on sal between losal and hisal
where grade = 4
50.查询领导者是‘BLAKE’的员工信息
select *
from emp
where mgr = (select empno from emp where ename = 'BLAKE')
51.查询最高领导者的薪水等级
select grade
from emp e join salgrade s
on sal between losal and hisal
where mgr is null
52.查询薪水最低的员工信息
select *
from emp e
where sal =(select min(sal) from emp where deptno = e.deptno)
53.查询和SMITH工作相同的员工信息
select *
from emp
where job in (select job from emp where ename = 'SMITH' )
54.查询不是领导的员工信息
select *
from emp
where empno not in (select mgr from emp)
55.查询平均工资比10部门低的部门编号
select deptno
from emp
group by deptno
having avg(sal) < (select avg(sal) from emp where deptno = 10)
56.查询在纽约工作的所有员工
select *
from emp
where deptno in (select deptno from dept where loc = 'NEW YORK')
57.查询‘SALES’部门平均薪水的等级
select avg(grade)
from emp e join salgrade s
on sal between losal and hisal
where deptno = (select deptno from dept where dname = 'SALES')
58.查询10号部门的员工在整个公司中所占的比例:
select count(empno)/(select count(empno) from emp)
from emp
where deptno =10
59.emp显示前5条。
select *
from emp
limit 0,5
60.查询各部门工资大于该部门平均工资的员工信息:
select *
from emp e
where sal >(select avg(sal) from emp where deptno = e.deptno)
61.查询各岗位工资小于该岗位平均工资的员工信息;
select *
from emp e
where sal >(select avg(sal) from emp where job = e.job)
62.查询所有领导的信息:要求使用exists关键字
select *
from emp e
where exists (select 1 from emp where mgr = e.empno)
63.查询所有员工的姓名,薪水,部门名称
select ename,sal,dname
from emp e join dept d
on e.deptno = d.deptno
64.查询所有员工的姓名,薪水,部门名称,薪水等级
select ename,sal,dname,grade
from emp e join dept d
on e.deptno = d.deptno
join salgrade s
on sal between losal and hisal
65.查询员工姓名及领导者姓名
select e.ename,m.ename
from emp e join emp m
on e.mgr = m.empno
66.查询所有员工的姓名,部门名称
select ename,dname
from emp e join dept d
on e.deptno = d.deptno
练习题:
1.查询员工表中工资大于1600的员工的姓名和工资
select ename,sal
from emp
where sal >1600
2.查询员工表中员工号是17的员工的姓名和部门编号
select ename,deptno
from emp
where empno = 17
3.选择员工表中工资不在4000到5000内的员工的姓名和工资.
select ename,sal
from emp
where sal < 4000 or sal >5000
4.选择员工表中在20和30部门工作的员工的姓名和部门号
select ename,deptno
from emp
where deptno in (20,30)
5.选择员工表中没有管理者的员工姓名及职位,按职位排序.
select ename,job
from emp
where mgr is null
order by job
6.选择员工表中有奖金的员工姓名,工资和奖金,按工资倒序排列
select ename,sal,comm
from emp
where comm is not null
order by sal desc
7.选择员工表中员工姓名的第三个字母是A的员工姓名
select ename
from emp
where ename like '__A%'
8.列出部门表中的部门名称和所在城市
select dname,loc
from dept
9.显示员工表中的不重复的岗位job
select distinct job
from emp
10.连接员工表中的员工姓名、职位、薪水,列之间用逗号连接,列头显示成out_put
select concate(ename,',',job,',',sal) out_put
from emp
11.查询员工表中员工号,姓名,工资,以及工资提高百分之20之后的结果
select empno,ename,sal,sal*1.2
from emp
12.查询员工的姓名和工资数,条件限定为工资数必须大于1200,并且查询结果按入职时间进行排序。早入职的员工排在前面
select ename,sal
from emp
where sal > 1200
order by hiredate
13.列出除了'ACCOUNTING'部门之外还有什么部门
select dname
from dept
where dname != 'ACCOUNTING'
14.把雇员按部门分组,求最高薪水,部门号 要求过滤掉名字中第二个字母是’A’的员工, 并且部门的平均薪水 > 3000,按照部门编号倒序排列
select max(sal),deptno
from emp
where ename not like '_A%'
group by deptno
having avg(sal) >3000
order by deptno desc
15.求工作职位是’manager’的员工姓名,部门名称和薪水等级
select ename,dname,grade
from emp e join dept d
on e.deptno = d.deptno
join salgrade s
on sal between losal and hisal
where job = 'manager'
16.按照部门分组统计,求最高薪水,平均薪水,最低薪水,只有薪水是1200以上的员工才参与统计,并且分组结果中只包含平均薪水在1500以上的部门,并且按照平均薪水倒序排列
select max(sal),avg(sal),min(sal)
from emp
where sal > 1200
group by deptno
having avg(sal) > 1500
order by avg(sal) desc
17.求薪水最高的员工姓名
select ename
from emp
where sal = (select max(sal) from emp)
18.查询各部门平均薪水等级,并且按平均薪水等级的降序排列
select avg(grade)
from emp e join salgrade s
on sal between losal and hisal
group by deptno
order by avg(grade)
19.查询所有员工姓名以S或s开头的所有员工信息
select *
from emp
where binary ename like 'S%' or binary ename like 's%'
20.查询所有工作时间超过一年的员工编号,姓名及入职时间,要求雇用时间的格式为’yyyy年mm月dd日’
select empno,ename,date_format(hiredate,'%Y年%月m%d日')
from emp
where timestampdiff(year,hiredate,now())>1
21.查询20部门的所有员工的员工姓名,实际收入
select ename,sal
from emp
where deptno = 20
22.查询10部门工资大于3000的员工信息,要求按员工的入职时间由前到后排序
select *
from emp
where sal > 3000 and deptno = 10
order by hiredate
23.查询10部门或20部门的所有员工的姓名,并截取前三位,按员工姓名升序排列
select ename
from emp
where deptno in (10,20)
order by ename
limit 0,3
24、查询所有员工的姓名,要求所有员工的姓名显示成小写,雇用日期显示为”yyyy-mm-dd”这种格式
select lcase(ename),date_format(hiredate,'%Y-%m-%d')
from emp
25、查询所有员工的姓名,所在部门名称,薪水,薪水等级、直接领导的姓名 (有问题,不显示最高领导)
select ename,dname,sal,grade,m.ename
from emp e join dept d
on e.deptno = d.deptno
join salgrade s
on sal between losal and hisal
join emp m
on e.mgr = m.empno
26、查询部门名称是’ACCOUNTING’的员工姓名及薪水等级
select ename,grade
from emp e join salgrade s
on sal between losal and hisal
where deptno = (select deptno from dept where dname = 'ACCOUNTING')
27、不能使用组函数,查询薪水的最高值.
select sal
from emp
order by sal desc
limit 0,1
28、统计平均薪水最高的部门名称
select dname
from (select dname,avgsal from(select dname,avg(sal) avgsal
from emp e join dept d
on deptno,dname)) f
where avgsal = (select max(avgsal) from (select dname,avg(sal) avgsal
from emp e join dept d
on e.deptno = d.deptno
group by deptno,dname) where dname = f.dname)
29、查询平均薪水等级最低的部门名称
select dname
from (select dname,avggrade from(select dname,avg(grade) avggrade
from emp e join dept d
on e.deptno = d.deptno
join salgrade s
on sal between losal and hisal
group by deptno,dname)) f
where avggrade = (select min(avggrade) from (select dname,avg(grade) avggrade
from emp e join dept d
on e.deptno = d.deptno
join salgrade s
on sal between losal and hisal
group by deptno,dname) where dname = f.dname)
选做
1、查询平均薪水最低的部门名称,要求:只有领导才参加统计
select dname
from (select dname,avgsal from(select dname,avg(sal) avgsal
from emp e join dept d
on deptno,dname)) f
where avgsal = (select max(avgsal) from (select dname,avg(sal) avgsal
from emp e join dept d
on e.deptno = d.deptno
group by deptno,dname where empno in (select distinct mgr from emp)) where dname = f.dname)
2、查询比普通员工的最高薪水还要高的领导者姓名
//领导的员工编号及工资
(select ename,empno,sal
from emp
where empno in (select mgr from emp)) c
//普通员工的编号及工资
select empno,sal
from emp
where empno not in (select mgr from emp)
//普通员工的最高薪水
select max(sal)
from (select sal from(select empno,sal
from emp
where empno not in (select mgr from emp)
) a) b
//求领导姓名
select ename
from (select ename,empno,sal from(select ename,empno,sal
from emp
where empno in (select mgr from emp)) c) d
where sal >(select max(sal)
from (select sal from(select empno,sal
from emp
where empno not in (select mgr from emp)
) a) b)
3、找出薪水最高的五个人
select *
from emp
order by sal desc
limit 0,5
4、查询第2到第7名的员工,按薪水降序排列
select *
from emp
order by sal desc
limit 1,6
5、查询最后入职的5名员工
select *
from emp
order by hiredate desc
limit 0,5