1.列出至少有一個員工的所有部門。
分析:每個部門有多少員工 —— 根據部門編號進行分組
select deptno,count(*) from emp group by deptno having count(*) >= 1;
2.列出薪金比“SMITH”多的所有員工。
分析:先查詢出SMITH工資 : select sal from emp where ename=’SMITH’;
select * from emp where sal > (select sal from emp where ename=’SMITH’);
3.***** 列出所有員工的姓名及其直接上級的姓名。
分析:表自對映,為表起別名,進行關聯 t1 表模擬員工表 t2 表儲存直接上級資訊
select e 員工姓名, e 直接上級 from emp t1,emp t2 where = o;
4.列出受僱日期早於其直接上級的所有員工。
分析:原理和上題類似
select t1.*,date from emp t1,emp t2 where = o and date < date;
5.列出部門名稱和這些部門的員工資訊,同時列出那些沒有員工的部門。
分析:部門沒員工也要顯示 — 外連線。無論怎樣部門資訊一定要顯示,通過部門去關聯員工
select * from dept left outer join emp on no = no ;
6.列出所有“CLERK”(辦事員)的姓名及其部門名稱。
分析:查詢job為CLERK 員工姓名和部門名稱
員工姓名 emp表
部門名稱 dept表
select e,e, from emp,dept where no = no and =’CLERK’;
7.列出最低薪金大於1500的`各種工作。
分析:工作的最低薪金 —- 按工作分組,求最低薪金
select min(sal) from emp group by job;
大於1500 是一個分組條件 — having
select job,min(sal) from emp group by job having min(sal) > 1500;
8.列出在部門“SALES”(銷售部)工作的員工的姓名,假定不知道銷售部的部門編號。
分析:員工姓名位於 emp 部門名稱 dept
select e from emp,dept where no = no and e = ‘SALES’;
9.列出薪金高於公司平均薪金的所有員工。
分析:先求公司平均薪金 select avg(sal) from emp;
select * from emp where sal > (select avg(sal) from emp);
10.列出與“SCOTT”從事相同工作的所有員工。
分析:先查詢SCOTT : select job from emp where ename =’SCOTT’;
select * from emp where ename <> ‘SCOTT’ and job = (select job from emp where ename =’SCOTT’);
11.列出薪金等於部門30中員工的薪金的所有員工的姓名和薪金。
分析:檢視部門30 中所有員工薪資列表 select sal from emp where deptno = 30;
select * from emp where sal in (select sal from emp where deptno = 30);
12.列出薪金高於在部門30工作的所有員工的薪金的員工姓名和薪金。
分析:
select * from emp where sal > all(select sal from emp where deptno = 30);
select * from emp where sal > (select max(sal) from emp where deptno = 30);
13.列出在每個部門工作的員工數量、平均工資。
分析:按部門分組
select deptno, count(*),avg(sal) from emp group by deptno;
14.列出所有員工的姓名、部門名稱和工資。
分析: