1.列出员工表中每个部门的员工数和部门编号

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

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

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

3.列出员工表中,员工人数大于3的部门的部门编号,部门名称和部门位置

答案1
(1). Select deptno,count(*) from emp group by deptno having count(*)>3;--x (2).
select d.*
from dept d ,(Select deptno,count(*) from emp group by deptno having count(*)>3)x
where x.deptno=d.deptno;
答案2:
select * from dept
where deptno in (select deptno from emp group by deptno having count(*)>3);

4.列出员工表中,员工人数大于3的部门的部门编号,名称,位置和员工人数

Select d.*,x.co 
from dept d,(select deptno,count(*) co from emp group by deptno having count(*)>3)x
where d.deptno=x.deptno;
(注意看聚合函数的处理方式)

5. 列出员工表中每个部门的员工数(员工数必须大于3),和部门名称

select * from dept d,(select deptno,count(*) from emp group by deptno having count(*)>3)x where d.deptno=x.deptno;

6. 找出工资比JONES多的员工

Select * from emp where sal>(select sal from emp where ename=’JONES’);

7.列出所有员工的姓名和其上级的姓名(表的自连接)

Select w.ename,m.ename from emp w,emp m where w.mgr=m.empno;

8.以职位分组,找出平均工资最高的两种职位

select * 
from (select job,avg(sal) from emp group by job order by avg(sal) desc)
where rownum<=2;

 9.查找出不在部门20,且比部门20中任何一个人工资都高的员工的姓名、部门名称

解法1    
select e.ename,e.sal,d.dname
from emp e,dept d
where e.deptno=d.deptno and e.deptno<> and e.sal>all(select sal from emp where deptno=);
解法2
select e.ename,e.sal,d.dname
from emp e,dept d
where e.deptno=d.deptno and e.deptno<> and e.sal>(select max(sal) from emp where deptno=);

10.得到平均工资大于2000的工作职种

select job,avg(sal)
from emp
group by job
having avg(sal)>;

11. 分部门得到工资大于2000的所有员工的平均工资,并且平均工资还要大于2500

解法1
Select deptno,avg(sal) from emp where sal>2000 group by deptno having avg(sal)>2500
解法2
select deptno ,avg (sal) avg
from emp
where sal>2000---x group by deptno ;
select * from x where x.avg>2500;

12.得到每个月工资总数最少的那个部门的部门编号,部门名称,部门位置

--拿着最低工资和去找编号,拿到编号在去到编号部门,部门名称,部门位置
select min(sum(sal)) from emp3 group by deptno;---x select deptno from emp3
group by deptno
having sum(sal)=(select min(sum(sal)) from emp3 group by deptno)x;--w select *
from dept
where deptno in(select deptno from emp3
          group by deptno
          having sum(sal)=(select min(sum(sal)) from emp3 group by deptno));

13. 分部门得到平均工资等级为3级(等级表)的部门编号

select deptno ,avg(sal) from emp group by deptno --e

select e.* ,s.grade
from salgrade s,(select deptno ,avg(sal)avg_sal from emp group by deptno)e
where e.avg_sal between s.losal and s.hisal and s.grade=3;

14. 查找出部门10和部门20中,工资最高第3名到第5名的员工的员工名字,部门名字,部门位置

思路1:连接,排序,取3-5
思路2:排序,取3-5,连接
思路3:排序,连接,取3-5;(错误思路)
下面是思路2的解法:
select emp.*from emp where deptno in(10,20) order by sal desc;---排序--x select rownum ro,x.* from (select emp.*from emp order by sal desc)x-- rownum加入--w select *
from (select rownum ro,x.* from (select emp.*from emp order by sal desc)x)w
where ro >=3 and ro<=5; --取3-5行 --e -- 表连接
select e.ename ,d.dname,d.loc
from dept d ,(select *
        from (select rownum ro,x.* from (select emp.*from emp where deptno in(10,20) order by sal desc)x)w
        where ro >=3 and ro<=5)e
where e.deptno=d.deptno;

 15.查找出收入(工资加上奖金),下级比自己上级还高的员工编号,员工名字,员工收入

解法1
select w.* from emp w,emp m
where w.mgr = m.empno and w.sal+nvl(w.comm,0) > m.sal+nvl(m.comm,0);

解法2
Select ename,sal+nvl(comm,0)
from emp w
where (sal+nvl(comm,0))>(select sal+nvl(comm,0) from emp m where w.mgr=m.empno);

16 .查找出工资等级不为4级的员工的员工名字,部门名字,部门位置

解法1
select e.deptno,e.ename,s.grade from emp e ,salgrade s
where e.sal between s.losal and s.hisal;--x select x.ename,d.dname,d.loc,x.grade
from (select e.deptno,e.ename,s.grade from emp e ,salgrade s
where e.sal between s.losal and s.hisal)x,dept d
where x.deptno=d.deptno and x.grade <> 4; 解法2
Select e.ename,d.dname,d.loc,s.grade
from emp e,dept d,salgrade s
where e.deptno=d.deptno and e.sal between s.losal and s.hisal and s.grade<>4;

 17. 查找出职位和‘MARTIN’或者‘SMITH’一样的员工的平均工资

select job from emp where ename in('MARTIN','SMITH');

select avg(sal) from emp
where job in(select job from emp where ename in('MARTIN','SMITH'))

18.查找出不属于任何部门的员工

Select * from emp where deptno is null;

19. 按照部门统计员工数,查出员工数最多的部门的第二名到第五名(列出部门名字,部门位置)

解法1
思路:子查询—行内视图
1. 列出每个部门的员工人数,并且按照员工人数进行降序排列 ---x
Select deptno,count(*) cou from emp group by deptno order by count(*) desc;
2. 将x与dept表做表连接 ---m
Select x.cou dept.* from x,dept where x.deptno=dept.deptno;
3.取出结果集的第二条到第五条记录
Select * from (select rownum ro,m.* from m) where m.ro>=2 and m.ro<=5;
4.替换,得到最终的结果。
Select *
from (select rownum ro,m.*
    from (Select x.cou dept.* from (Select deptno,count(*) cou from emp group by deptno order by count(*) desc)x,dept
    where.deptno=dept.deptno)m)
where m.ro>=2 and m.ro<=5;
解法2
select deptno,count(*)
from emp
group by deptno
order by count(*) desc --x select rownum ro,x.*
from (select deptno,count(*)
from emp
group by deptno
order by count(*) desc)x --e select d.deptno,d.loc
from (select rownum ro,x.*
from (select deptno,count(*)
from emp
group by deptno
order by count(*) desc)x)e,dept d
where e.deptno=d.deptno and (ro between 2 and 2)
解法3
思路2:
1. 先做表连接,表连接之后分组,求count(*),最后安装count(*)进行降序排列,得到视图X
Select e.deptno,count(*),d.dname,d.loc
from emp e,dept d
where d.deptno=e.deptno
group by e.deptno,d.dname,d.loc
order by count(*) desc;
2. 取出视图x的第二到第五行
Select *
from (select rownum ro, x.* from x)
where ro>=2 and ro<=5;
3. 替换x,得到最终结果:
Select *
from (select rownum ro, x.*
from(Select e.deptno,count(*),d.dname,d.loc
from emp e,dept d
where d.deptno=e.deptno
group by e.deptno,d.dname,d.loc
order by count(*) desc) x

where ro>=2 and ro<=5;

20. 查出KING所在部门的工作年限最大的员工名字

select *
from emp
where deptno =(select deptno from emp where ename='KING')
order by hiredate -- x select x.*,rownum ro
from (select *
from emp
where deptno =(select deptno from emp where ename='KING')
order by hiredate)x --w select w.ename
from (select x.*,rownum ro
from (select *
from emp
where deptno =(select deptno from emp where ename='KING')
order by hiredate)x)w
where ro=1;

 21 查出工资成本最高的部门的部门号和部门名称

select deptno,sum(sal)
from emp
group by deptno
order by sum(sal) desc --x select x.deptno,d.dname
from (select deptno,sum(sal)
from emp
group by deptno
order by sum(sal) desc)x,dept d
where x.deptno=d.deptno and rownum=

22.  显示与Blake在同一部门工作的雇员的工作和受雇日期,Blake不包含在内

Select ename,job,hiredate
from emp
where deptno=(select deptno from emp where ename=’BLAKE’) and ename<>’BLAKE’;

23.显示被King直接管理的雇员的姓名以及工资

select ENAME,SAL
from EMP
where MGR=(SELECT EMPNO FROM EMP WHERE ENAME=’KING’);

24.显示能获得与Scott一样工资和奖金的其他雇员的姓名、受雇日期以及工资。

SELECT ENAME,JOB,SAL
FROM EMP
WHERE (SAL,nvl(COMM,0))=(select sal,nvl(comm,0)
from emp
where ename=’SCOTT’)AND ENAME<>’SCOTT’;

面试题1

create table emp5(
id number(2),
name varchar2(5)
) create table sext(
id number(2),
sex varchar(2)
) insert into emp5
select 1,'A' from dual union
select 2,'B' from dual union
select 3,'C' from dual union
select 4,'D' from dual ; insert into sext
select 1,'男' from dual union
select 4,'女' from dual union
select 5,'男' from dual
;

1. 找出忘记填写性别的员工(用两种方法)

解法1
select e.*,s.sex
from emp5 e left join sext s on e.id=s.id --x select id ,name
from (select e.*,s.sex
from emp5 e left join sext s on e.id=s.id)x
where x.sex is null解法2 select * from emp1 where id not in(select id from sext); 解法3 select id from emp1 minus select id from sext;

面试题2

create table AAA(
mc varchar2(3),
sl number(3)
)
create table BBB(
mc varchar(3),
sl number(3)
) insert into AAA
select 'A',100 from dual union
select 'B',120 from dual insert into BBB
select 'A',10 from dual union
select 'A',20 from dual union
select 'B',10 from dual union
select 'B',20 from dual union
select 'B',30 from dual ;

1.用一条SQL语句计算出商品A,B目前还剩多少

用一条SQL语句计算出商品A,B目前还剩多少

select mc,sum(sl) sum_sl
from BBB
group by mc --x select a.mc,a.sl-x.sum_sl
from AAA a,(select mc,sum(sl) sum_sl
from BBB
group by mc)x
where a.mc=x.mc;

面试题三

人员情况表(employee)中字段包括:员工号(ID),姓名(name),年龄(age),文化程度(wh):包括四种情况(本科以上,大专,高中,初中以下),
现在我要根据年龄字段查询统计出:表中文化程度为本科以上,大专,高中,初中以下,各有多少人,占总人数多少。结过如下:
学历 年龄 人数 百分比
本科以上 20 34 14
大专 20 33 13
高中 20 33 13
初中以下 20 100 40
本科以上 21 50 20
Select wh as 学历,age as 年龄,count(*) as 人数,round(count(*)/(select count(*) from employee)*100) as 百分比
from employee
group by age,wh;

面试题 四

四张表:学生表student(sid, sname),教师表teacher(tid, tname),课程表course(cid,cname,ctype),选课表choose_course(ccid,sid,tid,cid)

创建表

create table  student(
sid number(2),
sname varchar2(5)
) create table teacher(
tid number(2),
tname varchar2(5)
) create table course(
cid number(2),
cname varchar2(5),
ctype varchar2(5)
)
create table choose_course(
ccid number(2),
sid number(2),
tid number(2),
cid number(2)
)

插入记录

Insert into student values(1, '小明');
Insert into student values(2, '小花'); Insert into teacher values(1, '陈红');
Insert into teacher values(2, '陈白'); Insert into course values(1, '语文' ,'文科');
Insert into course values(2, '数学','理科'); --小明选了陈红老师的语文
Insert into choose_course values(1,1,1,1);
--小明选了陈红老师的数学
Insert into choose_course values(2,1,1,2);
--小花选了陈红老师的数学
Insert into choose_course values(3,2,1,2);
--小明选了陈白老师的语文
Insert into choose_course values(4,1,2,1);
--小花选了陈红老师的语文
Insert into choose_course values(5,2,1,1);

  

1.查找陈红老师教的学生是哪些?

解法1
select distinct ch.sid from teacher t,choose_course ch
where t.tid=ch.tid and t.tname='陈红' --x select s.sid,s.sname
from student s
where sid in(select distinct ch.sid from teacher t,choose_course ch
where t.tid=ch.tid and t.tname='陈红'
)

解法2
Select s.sid,s.sname
from student s,choose_course cc,teacher t
where s.sid=cc.sid and cc.tid=t.tid and t.tname=’陈红’;

2.找学生小明所有的文科老师

解法1
(1)学生表和选课表相连---x
select s.*,cc.*
from student s,choose_course cc
where s.sid=cc.sid;
(2) 将(1)的结果与course表相连---m
Select c.*,x.*
from course c,x
where c.cid=x.cid;
(3) 将(2)的结果与teacher表相连
Select t.*,m.*
from course teacher,m
where t.tid =m.tid;
(4) 替换,得到最终结果:
Select t.*
from teacher inner join (Select c.*,x.*
from course c,(select s.*,cc.*
from student s,choose_course cc
where s.sid=cc.sid)x
where c.cid=x.cid)m
on t.tid=m.tid
where m.sname=’小明’,m.type=’文科’;
解法2
Select *
from student s,choose_course cc, teacher t, course c
where s.sid=cc.sid and cc.tid=t.tid and cc.cid=c.cid and s.name=‘小明’and c.type=’文科’;

3.教的学生最少的老师是哪位?

最少的老师可以大于两个
教学生最少的个数-匹配的老师的id-找老师的姓名 select min(count(*))
from choose_course
group by tid --x select tid
from choose_course
group by tid
having count(*) = (select min(count(*))
from choose_course
group by tid
);--w select *
from teacher
where tid in (select tid
from choose_course
group by tid
having count(*) = (select min(count(*))
from choose_course
group by tid
))

面试题五

用一条SQL语句,查询出每门课都大于80分的学生姓名
Name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

Select name from stu where name not in(select name from stu where fenshu<80);

华为面试题

一个表中的id有多条记录,把所有这个id的记录查出来,并显示共有多少条记录?

Select id,count(*) from 表名 group by id

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

  1. oracle习题集-高级查询

    1.问题:查询每个员工的部门名称,列出员工姓名和部门名称 select e.ename,d.dname from emp e,dept d where e.deptno=d.deptno 2. 问题: ...

  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. eigen矩阵操作练习

    // // Created by qian on 19-7-16. // /* 相机位姿用四元数表示 q = [0.35, 0.2, 0.3, 0.1] x,y,z,w * 注意:输入时Quatern ...

  2. js 手机号加密 中间星号表示

    var tel = String(this.memberMsg.phoneNo); var dh=tel.substr(0,3)+"******"+tel.substr(8); r ...

  3. Mui本地打包笔记(一)使用AndroidStudio运行项目 转载 https://blog.csdn.net/baidu_32377671/article/details/79632411

    转载 https://blog.csdn.net/baidu_32377671/article/details/79632411 使用AndroidStudio运行HBuilder本地打包的Mui项目 ...

  4. diskpart

    比如格式化成fat32 format fs=fat32 quick 比鼠标方便 如何使用: 打开cmd输入diskpart进入命令 首先 list disk 然后 然后 clean 然后 create ...

  5. double转String

    两种方式: 1.value = new DecimalFormat().format(double); 2.value = String.valueOf(double); 要相保留所有的double位 ...

  6. Django之模板语言(二)-----Filter

    1.其他常用的模板语言: 通过模板语言可以让前端页面显示数据,数据可以是基本数据类型,也可以是对象亦或者对象的列表,结合着模板中的for.if等配合使用. 要注意前端页面中,出现没有后端数据的情况,随 ...

  7. eclipse中使用lombok不生效

    eclipse中使用lombok,在实体类中添加@Data后,还是不能调用get.set方法.需要修改eclipse配置 1.将 lombok.jar 复制到eclipse.ini同级目录.下载的lo ...

  8. ES6之主要知识点(五)函数

    函数参数的默认值 作用域 ; function f(x, y = x) { console.log(y); } f() let x = ; function f(y = x) { let x = ; ...

  9. Free- Linux必学的60个命令

    1.作用 free命令用来显示内存的使用情况,使用权限是所有用户. 2.格式 free [-b|-k|-m] [-o] [-s delay] [-t] [-V] 3.主要参数 -b -k -m:分别以 ...

  10. N!中素因子p的个数 【数论】

    求N!中素因子p的个数,也就是N!中p的幂次 公式为:cnt=[n/p]+[n/p^2]+[n/p^3]+...+[n/p^k]; 例如:N=12,p=2 12/2=6,表示1~12中有6个数是2的倍 ...