技术开发 频道

Oracle SQL:经典查询练手四篇

--------10.列出与“SCOTT”从事相同工作的所有员工。--------
SQL> select ename from emp where job=(select job from emp where ename='SCOTT');
ENAME
----------
SCOTT
FORD

--------11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金。---------
SQL> select a.ename,a.sal from emp a where a.sal in (select b.sal
2 from emp b where b.deptno=30) and a.deptno<>30;
ENAME SAL
---------- ---------

--------12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。---------
SQL> select ename,sal from emp where sal>(select max(sal) from emp where deptno=30);
ENAME SAL
---------- ---------
JONES 2975.00
SCOTT
4000.00
KING
5000.00
FORD
3000.00
EricHu
5500.00
huyong
5500.00
WANGJING
5500.00
7 rows selected

--------13.列出在每个部门工作的员工数量、平均工资和平均服务期限。---------
SQL> select (select b.dname from dept b where a.deptno=b.deptno) as deptname ,count(deptno) as deptcount,avg(sal) as deptavgsal
2 from emp a group by deptno;
DEPTNAME DEPTCOUNT DEPTAVGSAL
-------------- ---------- ----------
ACCOUNTING 6 4208.33333
RESEARCH
5 2375
SALES
6 1566.66666
 
--------14.列出所有员工的姓名、部门名称和工资。---------
SQL> select a.ename,(select b.dname from dept b where b.deptno=a.deptno) as deptname,sal from emp a;

ENAME DEPTNAME SAL
---------- -------------- ---------
SMITH RESEARCH 800.00
ALLEN SALES
1600.00
WARD SALES
1250.00
JONES RESEARCH
2975.00
MARTIN SALES
1250.00
BLAKE SALES
2850.00
CLARK ACCOUNTING
2450.00
SCOTT RESEARCH
4000.00
KING ACCOUNTING
5000.00
TURNER SALES
1500.00
ADAMS RESEARCH
1100.00
JAMES SALES
950.00
FORD RESEARCH
3000.00
MILLER ACCOUNTING
1300.00
EricHu ACCOUNTING
5500.00
huyong ACCOUNTING
5500.00
WANGJING ACCOUNTING
5500.00

17 rows selected

--------15.列出所有部门的详细信息和部门人数。---------
SQL> select a.deptno,a.dname,a.loc,(select count(deptno) from emp b where b.deptno=a.deptno group by b.deptno) as deptcount from dept a;
DEPTNO DNAME LOC DEPTCOUNT
------ -------------- ------------- ----------
10 ACCOUNTING NEW YORK 6
20 RESEARCH DALLAS 5
30 SALES CHICAGO 6
40 OPERATIONS BOSTON
50 50abc 50def
60 Developer HaiKou

6 rows selected
0
相关文章