SQL高级语言查询

来源:互联网 发布:免费淘宝代理童装 编辑:程序博客网 时间:2024/04/28 19:20

为用户解锁授权

conn system/a12345

alter user scott identified by a12345account unlock;

grant resource,connect to scott;

conn scott/a12345;

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

1)select ename,dname

from scott.emp t1 inner join scott.deptt2 on t1.deptno=t2.deptno

where job='CLERK';

2)select ename,dname

from emp,dept

where emp.deptno=dept.deptno andjob=’CLERK’;

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

1)select * from scott.emp t1 right joinscott.dept t2 on t1.deptno=t2.deptno;

2) select *

 from emp right join dept onemp.deptno=dept.deptno;

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

1)select * from scott.emp t1 left joinscott.dept t2 on t1.deptno=t2.deptno

2) select *

 from emp left join dept onemp.deptno=dept.deptno

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

1)select * from scott.emp

where deptno=(select deptno fromscott.dept where dname='SALES')

2)select ename from emp innerjoin dept on emp.deptno=dept.deptno

where dname='SALES';

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

select e1.ename as 员工姓名,e2.ename上级姓名 from emp e1,emp e2

where e1.mgr=e2.empno;

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

select e1.* from emp e1,emp e2

where e1.mgr=e2.empno ande1.hiredate<e2.hiredate

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

select e1.ename,e1.job,e1.deptno fromemp e1 ,emp e2

where e1.job=e2.job and e1.deptno<>e2.deptno;

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

select e1.ename as 员工姓名,e1.*,e2.ename上级姓名 from emp e1,emp e2

where e1.mgr=e2.empno and e1.deptno=10;

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

select e1.ename as 员工姓名,e1.*,e2.ename上级姓名 from emp e1,emp e2

where e1.mgr=e2.empno and e1.deptno=10;

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

select * from emp where sal>2500

intersect

select * from emp where job='ANALYST’;

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

select * from emp where sal>2500

minus

select * from emp where job='ANALYST';

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

select * from emp

where sal>(select avg(sal) fromemp);

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

select * from emp where job=(select jobfrom emp  where ename='SMITH'); 14.查询工资比SMITH员工工资高的所有员工信息。

select * from emp wheresal>(select  sal from emp  where ename='SMITH'); 15.查询比所有在30号部门中工作的员工的工资都高的员工姓名和工资。

1) select ename,sal from scott.emp wheresal>all(select  sal from scott.empwhere deptno=30);

2) select ename,sal from emp where sal>(selectmax(sal) from emp where deptno=30);

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

select * from emp

where deptno in (select deptno from empgroup by deptno having count(*)>5);

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

select * from  dept

where deptno in(select deptno from empgroup by deptno having min(sal)>2000); 

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

select * from dept

where deptno in (select deptno from(select deptno,count(*) as 人数

from emp group by deptno) where 人数=(selectmax(人数) from(select deptno,count(*) as 人数 from emp group by deptno)));                            

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

select * from dept

where deptno in(select deptno from emp groupby deptno having count(*)>=1);

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

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

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

select * from((select * from emp e wheresal>(select avg(sal)

from emp group by deptno  having e.deptno=deptno)) t1 inner join

(select avg(sal),deptno from emp groupby deptno) t2 on t1.deptno=t2.deptno);

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

select * from dept

where deptno in(select distinct deptno fromemp  where empno in(select distinct mgr fromemp));

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

select * from emp , dept

where emp.deptno=dept.deptno and emp.deptnoin(select deptno from scott.emp group by deptno having avg(sal)<2000)   

 

 

原创粉丝点击