Oracle数据库——SQL高级查询

一、涉及内容

  1.掌握SELECT语句的多表连接查询。

  2.掌握SELECT语句的子查询。

二、具体操作

(一)根据Oracle数据库scott方案下的emp表和dept表,完成下列操作:

  1.查询所有工种为CLERK的员工的姓名及其部门名称。

select ename,dname 
from scott.emp t1 inner join scott.dept t2 on t1.deptno=t2.deptno
where job='CLERK';

  2.查询所有部门及其员工信息,包括那些没有员工的部门。

select * from scott.emp t1 right join scott.dept t2 on t1.deptno=t2.deptno

  3.查询所有员工及其部门信息,包括那些还不属于任何部门的员工。

select * from scott.emp t1 left join scott.dept t2 on t1.deptno=t2.deptno

  4.查询在SALES部门工作的员工的姓名信息。

用子查询实现:

select * from scott.emp 
where deptno=(select deptno from scott.dept where dname='SALES')

用连接查询实现:

 

select * from scott.emp t1 inner join scott.dept t2 on t1.deptno=t2.deptno 
where t2.dname='SALES';

 

 

注意两种实现方式,在行和列上的变化。

  5.查询所有员工的姓名及其直接上级的姓名。

select t1.ename as 员工姓名,t2.ename 经理姓名 from scott.emp t1,scott.emp t2 
where t1.mgr=t2.empno;

  6.查询入职日期早于其上级领导的所有员工的信息。

select t1.ename as 员工姓名,t2.ename 经理姓名 from scott.emp t1,scott.emp t2 
where t1.mgr=t2.empno and t1.hiredate<t2.hiredate

  7.查询从事同一种工作但不属于同一部门的员工信息。

select t1.ename,t1.job,t1.deptno,t2.ename,t2.job,t2.deptno from scott.emp t1 cross join scott.emp t2
 where t1.job=t2.job and t1.deptno <>t2.deptno

  8.查询10号部门员工及其领导的信息。

select t1.ename as 员工姓名,t2.ename 经理姓名 from scott.emp t1,scott.emp t2 
where t1.mgr=t2.empno and t1.deptno=10;

  9.使用UNION将工资大于2500的雇员信息与工作为ANALYST的雇员信息合并。

select * from scott.emp where sal>2500 
union 
select * from scott.emp where job='ANALYST'

 10.通过INTERSECT集合运算,查询工资大于2500,并且工作为ANALYST的雇员信息。

select * from scott.emp where sal>2500 
intersect
select * from scott.emp where job='ANALYST

 11.使用MINUS集合查询工资大于2500,但工作不是ANALYST的雇员信息。

select * from scott.emp where sal>2500
minus
select * from scott.emp where job='ANALYST';

 12.查询工资高于公司平均工资的所有员工信息。

select * from scott.emp where sal>(select avg(sal) from scott.emp)

 13.查询与SMITH员工从事相同工作的所有员工信息。

select * from scott.emp where job=(select job from scott.emp 
where ename='SMITH')

 14.查询工资比SMITH员工工资高的所有员工信息。

select * from scott.emp where sal>(select  sal from scott.emp 
where ename='SMITH')

 15.查询比所有在30号部门中工作的员工的工资都高的员工姓名和工资。

select ename,sal from scott.emp where sal>all(select  sal from scott.emp where deptno=30)

 16.查询部门人数大于5的部门的员工信息。

select * from scott.emp 
where deptno in (select deptno from scott.emp group by deptno having count(*)>5);

 17.查询所有员工工资都大于2000的部门的信息。

select * from scott.dept 
where deptno in(select deptno from scott.emp group by deptno having min(sal)>2000)

 18.查询人数最多的部门信息。

select * from scott.dept 
where deptno in (select deptno from (select deptno,count(*) 
                         as 人数
                         from scott.emp 
                         group by deptno) 
where 人数=(select max(人数) 
                 from(select deptno,count(*) 
                 as 人数 
                 from scott.emp 
                 group by deptno)));                

 19.查询至少有一个员工的部门信息。

select * from scott.dept 
where deptno in(select deptno
           from scott.emp 
          group by deptno 
          having count(*)>=1)

 20.查询工资高于本部门平均工资的员工信息。

select * from scott.emp e 
where sal>(select avg(sal)
       from scott.emp 
      group by deptno 
      having e.deptno=deptno);

 21.查询工资高于本部门平均工资的员工信息及其部门的平均工资。

select * from((select * from scott.emp e
                     where sal>(select avg(sal)
                     from scott.emp 
                     group by deptno 
                     having e.deptno=deptno)) t1 
                     inner join
                   (select avg(sal),deptno 
                    from scott.emp 
                    group by deptno) t2 on t1.deptno=t2.deptno);

 22.查询每个员工的领导所在部门的信息。

select * from scott.dept 
where deptno in(select distinct deptno 
          from scott.emp 
          where empno in(select distinct mgr
                    from scott.emp));

 23.查询平均工资低于2000的部门及其员工信息。

select * from scott.emp t1,scott.dept t2 
where t1.deptno=t2.deptno and t1.deptno in(select deptno
                              from scott.emp 
                            group by deptno 
                            having avg(sal)<2000)                            

 

(二)习题

  1.如果需要将雇员表中的所有行连接到雇员表中的所有行,则应创建哪种类型的连接?(B)

    A.等值连接    B.笛卡尔乘积  C.内连接  D.外连接

  2.如果需要从顾客表和订单表中查询所有顾客及其下达的所有订单,并且要求查询结果中先按顾客所在公司名称的升序排列,再按订单金额的降序排列。应执行以下哪条语句?(B)

A.SELECT c.顾客标识,c.公司名称,o.订单日期,o.顾客标识,o.金额

  FROM 顾客 c,订单 o

  WHERE c.顾客标识=o.顾客标识

  ORDER BY 金额 DESC, 公司名称;

B. SELECT c.顾客标识,c.公司名称,o.订单日期,o.顾客标识,o.金额

  FROM 顾客 c,订单 o

  WHERE c.顾客标识=o.顾客标识

  ORDER BY公司名称,金额 DESC;

C. SELECT c.顾客标识,c.公司名称,o.订单日期,o.顾客标识,o.金额

  FROM 顾客 c,订单 o

  WHERE c.顾客标识=o.顾客标识

  ORDER BY公司名称,金额;

D. SELECT c.顾客标识,c.公司名称,o.订单日期,o.顾客标识,o.金额

  FROM 顾客 c,订单 o

  WHERE c.顾客标识=o.顾客标识

  ORDER BY公司名称 ASC,金额 DESC;

 3.评估以下SQL语句:

    SELECT e.雇员标识,e.姓氏,e.名字,d.部门名称

  FROM 雇员 e,部门 d

  WHERE e.部门标识=d.部门标识

  AND 雇员.部门标识>5000

  ORDER BY 4;

哪个字句的语法有错误?(E)

A. SELECT e.雇员标识,e.姓氏,e.名字,d.部门名称 

B. FROM 雇员 e,部门 d

C. WHERE e.部门标识=d.部门标识

D. AND 雇员.部门标识>5000

E. ORDER BY 4;

 4.评估以下语句:

    SELECT 部门标识,AVG(薪金)

  FROM 雇员

  WHERE 职务标识<> 69 879

  GROUP BY 部门标识

  HAVING AVG(薪金)>35 000

  ORDER BY部门标识;

哪些子句限制了返回结果?请选择两个正确答案。(BD)

A. SELECT 部门标识,AVG(薪金)

B. WHERE 职务标识<> 69 879

C. GROUP BY 部门标识

D. HAVING AVG(薪金)>35 000

 

5.在SELECT语句中各个子句的正确顺序是什么?(C)

A. SELECT

   FROM

   WHERE

   ORDER BY

   GROUP BY

   HAVING

B.  SELECT

    FROM

HAVING  

    ORDER BY

WHERE

    GROUP BY

  

C.  SELECT

    FROM

WHERE

GROUP BY

HAVING  

    ORDER BY

   

D.  SELECT

    FROM

WHERE

HAVING  

    ORDER BY

    GROUP BY

 6.以下哪个运算符可用于多行子查询?(A)

    A.IN             B.<>           C.=             D.LIKE

 

 7.假设数据库中有顾客表和订单历史记录表。其中,顾客表中包括:客户标识NUMBER(5)、姓名VARCHAR2(25)、信贷限额NUMBER(8,2)、开户日期(DATE);订单历史记录表中包括:订单标识NUMBER(5)、客户标识NUMBER(5)、订单日期(DATE)、总计NUMBER(8,2)。以下哪种方案需要使用子查询来返回需要的结果?(D)

A.需要显示每个顾客账户下的开户日期

B.需要显示顾客下达订单的各个日期

C.需要显示在特定日期下达的所有订单

D.需要显示与编号为25950的订单的下达日期相同的所有订单

8.如果希望在报表中显示成本值高于所有产品平均成本的产品名称,应使用以下哪些SELECT语句?(B)

A. SELECT 产品名称 FROM 产品 WHERE 成本>(SELECT AVG(成本) FROM 产品);

B. SELECT 产品名称 FROM 产品 WHERE 成本> AVG(成本);

C. SELECT AVG(成本), 产品名称 FROM 产品 WHERE 成本> AVG(成本)GROUP BY 产品名称;

D. SELECT 产品名称 FROM(SELECT AVG(成本) FROM 产品) WHERE 成本> AVG(成本);

9.如果单行子查询返回了空值且使用了等于比较运算符,外部查询会返回什么结果?(B)

 A.不返回任何行            B.返回表中的所有行

 C.返回空值                D.返回错误

10.如果需要创建包含多行子查询的SELECT语句,可以使用哪个(些)比较运算符?(A)

 

A.IN、ANY和ALL            B.LIKE

C.BETWEEN…AND…           D.=、< 和 >

 

 

 

select ename,dname from scott.emp t1 inner join scott.dept t2 on t1.deptno=t2.deptno where job=’CLERK’;

    原文作者:SQL
    原文地址: https://www.cnblogs.com/shenxiaolin/p/5514072.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞