SQL练习40题(oracle)

使用ORACLE中scott自带的emp表做练习

1.查询所有的部门编号:
select distinct(deptno) from emp;

2.查询所有有人员的部门编号:
select ename,deptno from emp;

3.查询所有岗位名称:
select job from emp group by job;

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;

7.查询所有有奖金的员工信息
select * from emp where comm is not null;

8.查询最高领导的员工信息
select * from emp where mgr is null;

9.查询所有81年之后入职的员工信息
select * from emp where hiredate>to_date(‘1982’,‘yyyy’)

10.查询所有薪水在2000-4000范围内的员工信息
select * from emp where sal between 2000 and 4000;

11.查询所有部门编号是10或30的员工信息
select * from emp where deptno=10 or deptno=30;

12.查询所有20部门并且薪水超过2000的员工信息:
select * from emp where deptno=20 and sal>2000;

13.查询所有薪水不在2000-4000范围内的员工信息
select * from emp where sal not between 2000 and 4000;

14.查询所有部门编号不是10,30的员工信息
select * from emp where deptno!=10 and deptno!=30;

15.查询用户名为scott的员工信息:注意区分大小写
select * from emp where ename=‘SCOTT’;

16.查询姓名里面包含ALL的员工姓名
select * from emp where ename like ‘%ALL%’;

17.查询所有以”S”开头的同学
select * from emp where ename like ‘S%’;

18.查询第二个字母为A的员工姓名
select * 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,sal12+nvl(comm,0) total from emp where sal12+nvl(comm,0)>10000 order by total desc;

23.查询年薪超过10000的员工的姓名,编号,薪水,年收入,按照年薪降序排列
select ename,empno,sal,sal12 from emp where sal12>10000 order by sal*12 desc;

24.查询雇员表中,姓名为SMITH的雇员,截止到今天共工作了多少周,则可以使用如下的SQL语句(选做)
select (sysdate-hiredate)/7 from emp where ename=‘SMITH’;

25.查询各部门的最高薪水、最低薪水、平均薪水….
select deptno,max(sal),min(sal),avg(sal) from emp group by deptno;

26.查询‘SMITH’的领导姓名
select ename from emp where empno=(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(nvl(comm,0)) from emp;

32.查询公司中最晚入职的时间
select max(hiredate) from emp;

33.查询公司中有奖金的人数
select count(comm) from emp;

34.查询20部门的最高薪水是多少
select max(sal) from emp where deptno=20;

35.查询各部门的平均薪水及部门编号,部门名称。
select d.deptno,d.dname,avg(sal) from emp e join dept d on e.deptno=d.deptno
group by d.deptno,d.dname;

36.查询各部门中最高薪水的员工编号,姓名…
select e.empno,e.ename,e.deptno,e.sal from emp e
join (select deptno,max(sal) max_sal from emp group by deptno) t
on e.deptno=t.deptno and e.sal=t.max_sal;

37.查询所有员工姓名中包含‘A’的最高薪水
select max(sal) from emp where ename like ‘%A%’;

38.查询各岗位的最高薪水,最低薪水。要求只统计薪水>1000的
select job,max(sal),min(sal) from emp where sal>1000 group by job;

39.查询各部门的平均薪水及部门编号,要求只列出平均薪水>2000
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;

40.查询各部门的平均薪水及部门编号,要求只有员工姓名中包含
‘A’才参与统计,只列出平均薪水>1500的,按照平均薪水降序排列
select deptno,avg(sal) from emp where ename like ‘%A%’
group by deptno having avg(sal)>1500 order by avg(sal) desc;

  • 5
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值