1.问题:查询每个员工的部门名称,列出员工姓名和部门名称

select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno

2. 问题:查询员工表中,每个员工的工资等级,列出员工姓名,工资和工资等级

select e.ename,e.sal,s.grade from emp e,salgrade s
where e.sal between s.losal and s.hisal;

3. 查询所有比自己领导入职早的员工的姓名和上级领导的姓名

select w.ename as 员工姓名,m.ename as 上级领导姓名
from emp w,emp m where w.mgr=m.empno and w.hiredate<m.hiredate;

4.问题:查询每个员工的部门名称,列出员工姓名和部门名称

select e.ename,d.dname from emp e inner join dept d on e.deptno=d.deptno;

5.问题:查询部门10中每个员工的工资等级

select e.ename,s.grade from emp e
inner join salgrade s
on e.sal between s.losal and s.hisal --连接条件
where e.deptno=10; --查询条件

6.问题:查询emp表中部门名称为ACCOUNTING的员工的姓名,和部门位置

1
select e.ename,d.loc from emp e,dept d
where e.deptno=d.deptno and d.dname='ACCOUNTING';
2
select e.ename,d.loc from emp e inner join dept d
on e.deptno=d.deptno
where d.dname='ACCOUNTING';
3
select e.empno,e.ename from emp e
where deptno=(select deptno from dept where dept.dname = 'ACCOUNTING')
order by ename ;

7.查询高于自己部门平均工资的员工的信息,列出部门平均工资

--(1)获得每个部门的平均工资
select deptno,avg(sal) avg_sal from emp group by deptno; --x
--(2) 将x表和emp表做表连接
select e.*,x.* from emp e,x where e.deptno=x.deptno and e.sal>x.avg_sal;
--(3)替换x
select e.*,x.* from
emp e,(select deptno,avg(sal) avg_sal from emp group by deptno)x
where e.deptno=x.deptno and e.sal>x.avg_sal;

8.问题:查询没有没有员工的部门信息

select d.* from dept d left join emp e on e.deptno=d.deptno where e.empno is null;

9. 问题:查询和scott工作相同的员工姓名,不包含scott在内

select emp.ename
from emp
where job= (select job from emp where ename='SCOTT') and ename <> 'SCOTT'

10.查询blake的上级领导的姓名和工资

select ename ,sal
from emp
where empno =(select mgr from emp where ename='BLAKE' )

11. 查询薪水高于部门30的最低薪水的员工信息

select *
from emp
where sal < (select min(sal) from emp where deptno=30)

12.查询哪些部门最低薪水高于部门30的最低薪水,列出这些部门薪水最低的员工信息

--(1)查询哪些部门最低薪水高于部门30的最低薪水  --x
select deptno,min(sal) from emp
group by deptno
having min(sal)>(select min(sal) from emp where deptno=30);
--(2)将emp表与x做表连接
select e.* from emp e inner join x on e.sal=x.min_sal;
--(3)替换:
select e.*,x.* from
(select deptno,min(sal) min_sal from emp
group by deptno
having min(sal)>(select min(sal) from emp where deptno=30))x
inner join emp e
on e.sal=x.min_sal;

13. 查询和SALESMAN同部门,但是不是SALESMAN的员工的信息

select * from emp where
deptno in (select distinct deptno from emp where job='SALESMAN')
and job<>'SALESMAN';

14.查询比任何一个SALESMAN的薪水高但不是SALESMAN的员工信息

select * from emp where sal>any(select sal from emp where job='SALESMAN') and job<>'SALESMAN';

-->any:大于最小的

--<any:小于最大的

15. 查询比所有一个SALESMAN的薪水高但不是SALESMAN的员工信息。

>all:大于最大的

<all:小于最小的

select * from emp where sal>all(select sal from emp where job='SALESMAN') and job<>'SALESMAN';
select * from emp where sal>(select max(sal) from emp where job='SALESMAN') and job<>'SALESMAN';

16.按照部门名称和工作,来查看emp表每个部门,每种职位每个月的总开支,并且按照总开支进行降序排列

--(1)表连接
select e.*,d.* from emp e,dept d where e.deptno=d.deptno;
--(2)分组求和排序
select d.dname,e.job,sum(sal) from emp e,dept d where e.deptno=d.deptno
group by d.dname,e.job
order by sum(sal) desc;

17. 查询和scott相同部门相同职位的员工

select * from emp
where (deptno,job) = (select deptno,job from emp where ename='SCOTT')

18. 查询每个部门下面工资最低的员工

select deptno,min(sal) from emp group by deptno;
select * from emp
where (deptno,sal) in (select deptno,min(sal) from emp group by deptno); select * from emp e1
where (deptno,sal)=(select deptno,min(sal) from emp e2 where e1.deptno=e2.deptno group by deptno);

19.列出emp表中,每个部门的员工人数和部门号

select deptno,count(*) from emp group by deptno;

20.列出emp表中,每个部门的员工人数和部门名称

select d.dname,count(*)
from emp e,dept d
where e.deptno=d.deptno
group by d.dname;

21.列出emp表中,部门人数大于3的员工人数和部门编号

select deptno,count(*)
from emp
group by deptno
having count(*)>3;

22.列出emp表中,部门人数大于3的员工人数和部门名称

1
select d.dname,count(*)
from dept d,emp e
where d.deptno=e.deptno
group by d.dname
having count(*)>3; 2
--(1)查询每个部门的员工人数 --x
select deptno,count(*) co from emp group by deptno having count(*)>3;
--(2)表连接
select d.dname,x.co from dept d,x where d.deptno=x.deptno;
--(3)替换
select d.dname,x.co
from dept d,(select deptno,count(*) co from emp group by deptno having count(*)>3)x
where d.deptno=x.deptno;

--exists(select...from...)

--如果select语句返回的结果集为空,exists(select...from...)结果是false

--如果select语句返回的结果集不为空,exists(select...from...)结果是true

23.查询有员工的部门的部门编号和部门名称

select deptno,dname from dept d where exists(select * from emp e where d.deptno=e.deptno);

24.显示职位是’MANAGER’且薪水大于2500的员工的信息

select * from emp where job='MANAGER'
intersect
select * from emp where sal>2500;

25 显示职位是MANAGER,但是薪水低于2500的员工的信息

select * from emp where job='MANAGER'
minus
select * from emp where sal>=2500;

oracle习题集-高级查询的更多相关文章

  1. oracle习题集-高级查询2

    1.列出员工表中每个部门的员工数和部门编号 Select deptno,count(*) from emp group by deptno; 2.列出员工表中,员工人数大于3的部门编号和员工人数 ; ...

  2. Oracle 函数高级查询

    目录 oracle高级查询 Oracle SQL获取每个分组中日期最新的一条数据 求平均值(为0的参数不均摊) 字符串清除前面的0 判断字符串串是否包含某个字符串 switch 判断 oracle不足 ...

  3. oracle的高级查询

    1.简单连接 基本连接语法:SELECT [ALL|DISTINCT]column_name[,expression…]FROM table1_name[,table2_name,view_name, ...

  4. Oracle数据库高级查询(五)集合查询

    现实需求有时候需要将多个查询组合到一个查询中去 这时就需要使用集合查询操作了 这个操作类似于数学中的交集,并集,和补集的操作   交集就是返回两个查询共有的记录,关键字是INTERSECT 并集是返回 ...

  5. oracle高级查询(实例基于scott用户四张表)

    oracle高级查询(实例基于scott用户四张表) 分组查询 多表查询 子查询 综合实例 ====================================================== ...

  6. Oracle学习笔记(7)——高级查询(1)

    在学习高级查询之前,我们先了解一下怎样查看Oracle数据库中的全部表.由于我们要使用到Oracle数据库中SCOTT用户下的几张表(这些表是Oracle数据库自带的表). 分组查询 分组函数的概念: ...

  7. oracle学习笔记(十二) 查询练习(二) 高级查询

    高级查询练习 /*--------------------------------------------- 分组查询 -------------------------------------*/ ...

  8. oracle学习笔记(十一) 高级查询

    高级查询 分组查询 select * from student [where ] [having ] --二次限定 [order by] --asc升序 desc降序 默认升序 查看EMPLOYEE表 ...

  9. [NewLife.XCode]高级查询(化繁为简、分页提升性能)

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netcore,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和 ...

随机推荐

  1. dijkstra (模板)

    突然意识到以前写的都是假的dij,感谢GhostCai神犇. #include<iostream> #include<cstdio> #include<cstring&g ...

  2. Mac系统下安装Vue-cli详细步骤

    Vue-cli安装 因为是mac系统,所以和视频里老师讲的有些许不同. 1.首先打开终端 按照老师的操作,首先检查node版本 下面是我的操作 打开终端,输入命令 node -v 我去,找不到node ...

  3. 【LGP4705】玩游戏

    题目 显然这个题的期望就是逗你玩的,我们算出来总贡献除以\(nm\)就好了 设\(ans_t=\sum_{i=1}^n\sum_{j=1}^n(a_i+b_j)^t\) 二项式定理展开一下 \[ans ...

  4. PKU--2184 Cow Exhibition (01背包)

    题目http://poj.org/problem?id=2184 分析:给定N头牛,每头牛都有各自的Si和Fi 从这N头牛选出一定的数目,使得这些牛的 Si和Fi之和TS和TF都有TS>=0 F ...

  5. 深度学习(二十六)Network In Network学习笔记

    深度学习(二十六)Network In Network学习笔记 Network In Network学习笔记 原文地址:http://blog.csdn.net/hjimce/article/deta ...

  6. 如何做系列(1)- mybatis 如何实现分页?

    第一个做法,就是直接使用我们的sql语句进行分页,也就是在mapper里面加上分页的语句就好了. <select id="" parameterType="&quo ...

  7. Cyclic GCDs

    Cyclic GCDs 题目链接 题面描述 有\(n\)个点,每个点有权值. 现有排列\(P\),\(p_i\)表示\(i\)个点向\(p_i\)连了一条边. 显然会形成若干个简单环.每个简单环的权值 ...

  8. JZOJ2368 【SDOI2011】黑白棋

    题目 题目大意 在一个1*n的棋盘上,有黑棋和白棋交错分布,每次,一个人可以移动自己的ddd颗旗子. 问先手必胜的方案数. 思考历程 在一开始,我就有点要放弃的念头. 因为这题是一道博弈问题. 我是非 ...

  9. Vue 本地代理 纯前端技术解决跨域

    vue-axios获取数据很多小伙伴都会使用,但如果前后端分离且后台没设置跨域许可,那要怎样才能解决跨域问题? 常用方法有几种: 通过jsonp跨域 通过修改document.domain来跨子域 使 ...

  10. 提示框插件layer的使用讲解

    官方网站:http://layer.layui.com/ 在页面中引入: <script src="js/layer-v3.0.3/layer/layer.js">&l ...