子查询 练习题

发布 2021-05-09 05:45:28 阅读 1101

1、 查询出比员工编号7654工资要高的全部员工的信息。

select * from emp where sal>(select sal from emp where empno=7654)

2、 查询出比员工编号7654工资高,同时与7788从事相同工作的全部员工的信息。

select * from emp where sal>(select sal from emp where empno=7654)

and job=(select job from emp where empno=7788)

3、 查询出工资最低的员工的姓名、工作、工资。

select ename,job,sal

from emp

where sal=(select min(sal) from emp)

4、 查询出每个部门的最低工资的员工的信息。

select * from emp

where sal in(select min(sal) from emp group by deptno)

5、 分别使用any和all完成第4题。

select * from emp

where sal =any(select min(sal) from emp group by deptno)

all完成不了第4题。

6、 查询至少有一个员工的所有部门信息。

· 求出所有部门的员工数量。

select deptno,count(empno) from emp

group by deptno ;

· 列出部门人数大于等于 1 的所有部门编号。

select deptno,count(empno)

from emp

group by deptno

h**ing count(empno)>=1 ;

通过部门表,查询出部门的信息即可。

select d.*,from dept d,select deptno,count(empno) cou from emp group by deptno h**ing count(empno) >1) ed

where

采用sql-1999标准―

select

from dept d join (select deptno,count(empno) cou from emp group by deptno h**ing count(empno) >1) ed on(

7、查询薪金比“smith”多的所有员工。

· 求出 smith 的工资。

select sal from emp where ename='smith'

· 以上面的结果为条件,查询所有符合条件的雇员信息。

select * from emp

where sal>(select sal from emp where ename='smith')

8、查询受雇日期早于其直接领导的所有员工的编号、姓名、部门名称。

· 自身关联,查找 mgr=empno 的同时还要比较 hiredate,先查询编号、姓名。

select

from emp e,emp m

where and <

· 如果要加入部门的名称,则肯定应该加入 dept 表,做表关联查询。

select from emp e,emp m,dept d

where and < and

sql-1999标准―

select

from emp e1 join emp e2 on( join dept d on(

where <

9、查询部门名称和这些部门的员工的信息,同时列出那些没有员工的部门。

· 左、右关联问题。

select

from dept d,emp e

where

sql-1999标准―

select

from dept d left join emp e on(

10、查询所有“clerk”(办事员)的姓名及其部门名称及部门人数。

· 找出所有办事员的姓名及部门编号。

select ename,deptno

from emp

where job='clerk'

· 如果要找到部门名称,则肯定要使用部门表。

select

from emp e,dept d

where job='clerk' and ;

部门人数肯定要使用分组完成,一旦使用分组,肯定是 group by

select

from emp e,dept d,(select deptno,count(empno) cou from emp group by deptno) ed

where job='clerk' and and ;

或。select

from emp e,dept d,(select deptno,count(empno) cou from emp group by deptno) ed

where job='clerk' and and ;

或-采用sql-1999标准。

select

from emp e join dept d on(

join (select deptno,count(empno) co from emp group by deptno) c on(

where job='clerk'

查询练习题

网络数据库 讲稿。简单的单表查询。例1 检索出1983年6月1日之后出生的学生的姓名 性别和民族,并按降序排列。select 姓名,性别,民族 from 学生 where 出生日期 1983 6 1 order by 出生日期 desc 多表查询。在from后跟随多个表。例1 查找出已修学分达到20...

练习6 子查询

课堂实训 6 姓名日期。学号专业。号 年级班级。成绩。任务名称 子查询。目的与要求 掌握子查询的使用方法。任务清单 1查询与王一平同系的教师姓名。2查询信管系的所有教师姓名,性别。3查询张宏 j a程序设计 的成绩。4查询计算机系的所有班级名称。5查询比 电子商务物流管理 计算机应用基础 学分都高的...

练习10 子查询

子查询。子查询是嵌套于 select select.into insert.into delete 或 update 语句内部或嵌套于另一个子查询内部的 select 语句。子查询是相对的概念,构成子查询的select语句本身就是一个查询,当它被嵌入到其他查询语句中时,就被视为子查询。如果需要先进行...