用SQL完成以下問題列表:
1.列出至少有一個員工的所有部門。 2.列出薪金比“SMITH”多的所有員工。 3.列出所有員工的姓名及其直接上級的姓名。 4.列出受雇日期早于其直接上級的所有員工。 5.列出部門名稱和這些部門的員工信息曹抬,同時列出那些沒有員工的部門 6.列出所有“CLERK”(辦事員)的姓名及其部門名稱。 7.列出最低薪金大于1500的各種工作急鳄。 8.列出在部門“SALES”(銷售部)工作的員工的姓名谤民,假定不知道銷售部的部門編號。 9.列出薪金高于公司平均薪金的所有員工攒岛。 10.列出與“SCOTT”從事相同工作的所有員工赖临。 11.列出薪金等于部門30中員工的薪金的所有員工的姓名和薪金。 12.列出薪金高于在部門30工作的所有員工的薪金的員工姓名和薪金灾锯。 13.列出在每個部門工作的員工數(shù)量、平均工資和平均服務(wù)期限嗅榕。 14.列出所有員工的姓名顺饮、部門名稱和工資。 15.列出所有部門的詳細信息和部門人數(shù)凌那。 16.列出各種工作的最低工資兼雄。 17.列出各個部門的MANAGER(經(jīng)理)的最低薪金。 18.列出所有員工的年工資,按年薪從低到高排序帽蝶。
[](javascript:void(0); "復(fù)制代碼")
各答案如下赦肋,歡迎大家給出不出的解答方式。
[](javascript:void(0); "復(fù)制代碼")
--------1.列出至少有一個員工的所有部門励稳。--------- SQL> select dname from dept where deptno in(select deptno fromemp);
DNAME -------------- RESEARCH
SALES
ACCOUNTING --------或-------- SQL> select dname from dept where deptno in(select deptno from emp group by deptno having count(deptno) >=1);
DNAME -------------- ACCOUNTING
RESEARCH
SALES --------2.列出薪金比“SMITH”多的所有員工佃乘。---------- SQL> select * from emp where sal > (select sal from emp where ename = 'SMITH');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ---------- --------- ----- ----------- --------- --------- ------ 7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30 7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30 7566 JONES MANAGER 7839 1981-4-2 2975.00 20 7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30 7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30 7782 CLARK MANAGER 7839 1981-6-9 2450.00 10 7788 SCOTT ANALYST 7566 1987-4-19 4000.00 20 7839 KING PRESIDENT 1981-11-17 5000.00 10 7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30 7876 ADAMS CLERK 7788 1987-5-23 1100.00 20 7900 JAMES CLERK 7698 1981-12-3 950.00 30 7902 FORD ANALYST 7566 1981-12-3 3000.00 20 7934 MILLER CLERK 7782 1982-1-23 1300.00 10 102 EricHu Developer 1455 2011-5-26 1 5500.00 14.00 10 104 huyong PM 1455 2011-5-26 1 5500.00 14.00 10 105 WANGJING Developer 1455 2011-5-26 1 5500.00 14.00 10 16rows selected --------3.列出所有員工的姓名及其直接上級的姓名。---------- SQL> select a.ename,(select ename from emp b where b.empno=a.mgr) as boss_name fromemp a;
ENAME BOSS_NAME ---------- ---------- SMITH FORD
ALLEN BLAKE
WARD BLAKE
JONES KING
MARTIN BLAKE
BLAKE KING
CLARK KING
SCOTT JONES
KING
TURNER BLAKE
ADAMS SCOTT
JAMES BLAKE
FORD JONES
MILLER CLARK
EricHu
huyong
WANGJING 17rows selected --------4.列出受雇日期早于其直接上級的所有員工驹尼。---------- SQL> select a.ename from emp a where a.hiredate<(select hiredate from emp b where b.empno=a.mgr);
ENAME ---------- SMITH
ALLEN
WARD
JONES
BLAKE
CLARK 6rows selected --------5.列出部門名稱和這些部門的員工信息趣避,同時列出那些沒有員工的部門---------- SQL> select a.dname,b.empno,b.ename,b.job,b.mgr,b.hiredate,b.sal,b.deptno
2 from dept a left join emp b on a.deptno=b.deptno;
DNAME EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO -------------- ----- ---------- --------- ----- ----------- --------- ------ RESEARCH 7369 SMITH CLERK 7902 1980-12-17 800.00 20 SALES7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 30 SALES7521 WARD SALESMAN 7698 1981-2-22 1250.00 30 RESEARCH7566 JONES MANAGER 7839 1981-4-2 2975.00 20 SALES7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 30 SALES7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30 ACCOUNTING7782 CLARK MANAGER 7839 1981-6-9 2450.00 10 RESEARCH7788 SCOTT ANALYST 7566 1987-4-19 4000.00 20 ACCOUNTING7839 KING PRESIDENT 1981-11-17 5000.00 10 SALES7844 TURNER SALESMAN 7698 1981-9-8 1500.00 30 RESEARCH7876 ADAMS CLERK 7788 1987-5-23 1100.00 20 SALES7900 JAMES CLERK 7698 1981-12-3 950.00 30 RESEARCH7902 FORD ANALYST 7566 1981-12-3 3000.00 20 ACCOUNTING7934 MILLER CLERK 7782 1982-1-23 1300.00 10 ACCOUNTING102 EricHu Developer 1455 2011-5-26 1 5500.00 10 ACCOUNTING104 huyong PM 1455 2011-5-26 1 5500.00 10 ACCOUNTING105 WANGJING Developer 1455 2011-5-26 1 5500.00 10 50abc
OPERATIONS
Developer 20rows selected --------6.列出所有“CLERK”(辦事員)的姓名及其部門名稱。---------- SQL> select a.ename,b.dname from emp a join dept b on a.deptno=b.deptno and a.job='CLERK';
ENAME DNAME ---------- -------------- SMITH RESEARCH
ADAMS RESEARCH
JAMES SALES
MILLER ACCOUNTING --------7.列出最低薪金大于1500的各種工作新翎。---------- SQL> select distinct job as HighSalJob from emp group by job having min(sal)>1500;
HIGHSALJOB ---------- ANALYST
Developer
MANAGER
PM
PRESIDENT --------8.列出在部門“SALES”(銷售部)工作的員工的姓名程帕,假定不知道銷售部的部門編號。---------- SQL> select ename from emp where deptno=(select deptno from dept where dname='SALES');
ENAME ---------- ALLEN
WARD
MARTIN
BLAKE
TURNER
JAMES 6rows selected --------9.列出薪金高于公司平均薪金的所有員工地啰。---------- SQL> select ename from emp where sal>(select avg(sal) fromemp);
ENAME ---------- JONES
BLAKE
SCOTT
KING
FORD
EricHu
huyong
WANGJING 8rows selected --------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 SCOTT4000.00 KING5000.00 FORD3000.00 EricHu5500.00 huyong5500.00 WANGJING5500.00 7rows selected --------13.列出在每個部門工作的員工數(shù)量岭埠、平均工資和平均服務(wù)期限。--------- 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 bydeptno;
DEPTNAME DEPTCOUNT DEPTAVGSAL -------------- ---------- ---------- ACCOUNTING 6 4208.33333 RESEARCH5 2375 SALES6 1566.66666 --------14.列出所有員工的姓名、部門名稱和工資枫攀。--------- SQL> select a.ename,(select b.dname from dept b where b.deptno=a.deptno) as deptname,sal fromemp a;
ENAME DEPTNAME SAL ---------- -------------- --------- SMITH RESEARCH 800.00 ALLEN SALES1600.00 WARD SALES1250.00 JONES RESEARCH2975.00 MARTIN SALES1250.00 BLAKE SALES2850.00 CLARK ACCOUNTING2450.00 SCOTT RESEARCH4000.00 KING ACCOUNTING5000.00 TURNER SALES1500.00 ADAMS RESEARCH1100.00 JAMES SALES950.00 FORD RESEARCH3000.00 MILLER ACCOUNTING1300.00 EricHu ACCOUNTING5500.00 huyong ACCOUNTING5500.00 WANGJING ACCOUNTING5500.00 17rows selected --------15.列出所有部門的詳細信息和部門人數(shù)括饶。--------- 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 fromdept a;
DEPTNO DNAME LOC DEPTCOUNT ------ -------------- ------------- ---------- 10 ACCOUNTING NEW YORK 6 20 RESEARCH DALLAS 5 30 SALES CHICAGO 6 40 OPERATIONS BOSTON
50 50abc 50def
60Developer HaiKou 6rows selected --------16.列出各種工作的最低工資。--------- SQL> select job,avg(sal) from emp group by job;
JOB AVG(SAL) --------- ---------- ANALYST 3500 CLERK1037.5 Developer5500 MANAGER2758.33333 PM5500 PRESIDENT5000 SALESMAN1400 7rows selected --------17.列出各個部門的MANAGER(經(jīng)理)的最低薪金来涨。-------- SQL> select deptno,min(sal) from emp where job='MANAGER' group by deptno;
DEPTNO MIN(SAL) ------ ---------- 10 2450 20 2975 30 2850 --------18.列出所有員工的年工資,按年薪從低到高排序图焰。--------- SQL> select ename,(sal+nvl(comm,0))*12 as salpersal from emp order bysalpersal;
ENAME SALPERSAL ---------- ---------- SMITH 9600 JAMES11400 ADAMS13200 MILLER15600 TURNER18000 WARD21000 ALLEN22800 CLARK29400 MARTIN31800 BLAKE34200 JONES35700 FORD36000 SCOTT48000 KING60000 EricHu66168 huyong66168 WANGJING66168 17 rows selected