Oracle數據庫面試練習題


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 t1.ename 員工姓名, t2.ename 直接上級 from emp t1,emp t2 where t1.MGR = t2.empno;

4.列出受雇日期早於其直接上級的所有員工。
分析:原理和上題類似
select t1.*,t2.hiredate from emp t1,emp t2 where t1.MGR = t2.empno and t1.hiredate < t2.hiredate;

5.列出部門名稱和這些部門的員工信息,同時列出那些沒有員工的部門。
分析:部門沒員工也要顯示 — 外連接。無論怎樣部門信息一定要顯示,通過部門去關聯員工
select * from dept left outer join emp on dept.deptno = emp.deptno ;

6.列出所有“CLERK”(辦事員)的姓名及其部門名稱。
分析:查找job為CLERK 員工姓名和部門名稱 
員工姓名 emp表
部門名稱 dept表
select emp.ename,dept.dname,emp.job from emp,dept where emp.deptno = dept.deptno and emp.job=’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 emp.ename from emp,dept where emp.deptno = dept.deptno and dept.dname = ‘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.列出所有員工的姓名、部門名稱和工資。
分析:
select emp.ename,dept.dname,emp.sal from emp,dept where emp.deptno = dept.deptno;

15.列出所有部門的詳細信息和部門人數。
分析:
select dept.*,count(emp.*) from emp,dept where emp.deptno = dept.deptno group by deptno ; 錯誤!
求各個部門編號和人數 select deptno,count(*) from emp group by deptno;
再和dept表關聯 select dept.*,temp.部門人數 from dept , (select deptno,count(*) 部門人數 from emp group by deptno) temp where dept.deptno = temp.deptno ;

16.列出各種工作的最低工資。
分析:各個工作 分組 , 最低工資 min
select job,min(sal) from emp group by job;

17.列出各個部門的MANAGER(經理)的最低薪金。
分析:where job=’MANAGER’ 過濾所有不是經理數據
select deptno,min(sal) from emp where job =’MANAGER’ group by deptno;

18.列出所有員工的年工資,按年薪從低到高排序。 
分析: select ename, sal*12 from emp order by sal*12 asc;

19.查出emp表中薪水在3000以上(包括3000)的所有員工的員工號、姓名、薪水。
分析: select * from emp where sal >= 3000;

20.查詢出所有薪水在’ALLEN’之上的所有人員信息。
分析:select * from emp where sal > (select sal from emp where ename =’ALLEN’);

21.查詢出emp表中部門編號為20,薪水在2000以上(不包括2000)的所有員工,顯示他們的員工號,姓名以及薪水,以如下列名顯示:員工編號 員工名字 薪水
分析: select empno 員工編號,ename 員工姓名 ,sal 薪水 from emp where deptno = 20 and sal > 2000;

22.查詢出emp表中所有的工作種類(無重復)
分析: select distinct job from emp;

23.查詢出所有獎金(comm)字段不為空的人員的所有信息。
分析:不為空 is not null
select * from emp where comm is not null;

24.查詢出薪水在800到2500之間(閉區間)所有員工的信息。(注:使用兩種方式實現and以及between and)
分析:select * from emp where sal >= 800 and sal <= 2500;
select * from emp where sal between 800 and 2500;

25.查詢出員工號為7521,7900,7782的所有員工的信息。(注:使用兩種方式實現,or以及in)
分析:select * from emp where empno in(7521,7900,7782);
select * from emp where empno=7521 or empno = 7900 or empno = 7782;

26.查詢出名字中有“A”字符,並且薪水在1000以上(不包括1000)的所有員工信息。
分析: 模糊查詢
select * from emp where ename like ‘%A%’ and sal > 1000;

27.查詢出名字第三個字母是“M”的所有員工信息。
分析:第三個字母 __M%
select * from emp where ename like ‘__M%';

28.將所有員工按薪水升序排序,薪水相同的按照入職時間降序排序。
分析:select * from emp order by sal asc,hiredate desc;

29.將所有員工按照名字首字母升序排序,首字母相同的按照薪水降序排序。
分析:SUBSTRING(‘字符串’,第幾個字符,長度);  —- 首字母 substring(ename,1,1)
select * from emp order by substring(ename,1,1) asc,sal desc;

30.查詢出最早工作的那個人的名字、入職時間和薪水。
分析:最早工作人 — hiredate 最小值
select ename,hiredate,sal from emp where hiredate = (select min(hiredate) from emp);
select ename,hiredate,sal from emp where hiredate <= all(select hiredate from emp);

> any === > min
> all === > max
< any === < max
< all === < min

31.顯示所有員工的名字、薪水、獎金,如果沒有獎金,暫時顯示100.
分析:select ename,sal,comm from emp; —- 沒有獎金顯示100  函數ifnull
select ename,sal,ifnull(comm,100) from emp;

32.顯示出薪水最高人的職位。
分析: select job from emp where sal = (select max(sal) from emp);
select job from emp where sal >= all(select sal from emp);

33.查出emp表中所有部門的最高薪水和最低薪水,部門編號為10的部門不顯示。
分析:按部門分組 select deptno,max(sal),min(sal) from emp where deptno<>10 group by deptno;

34.刪除10號部門薪水最高的員工。
分析:delete from emp where deptno=10 and sal >= all(select sal from emp where deptno=10 ); // MYSQL 不支持
Mysql 規范,修改或者刪除 表中記錄,不允許在子查詢中 查詢相同表
ERROR 1093 (HY000): You can’t specify target table ‘emp’ for update in FROM clause

解決方案:臨時表
delete from emp where deptno=10 and sal >= all(select t.sal from (select sal from emp where deptno=10) t );

35.將薪水最高的員工的薪水降30%。
分析:update emp set sal = sal*0.7 where sal = (select max(sal) from emp); // MYSQL 不支持
引入 臨時表
update emp set sal = sal*0.7 where sal = (select t.maxsal  from (select max(sal) maxsal from emp) t);

36.查詢員工姓名,工資和 工資級別(工資>=3000 為3級,工資>2000 為2級,工資<=2000 為1級)
分析:
select ename,sal, case when sal>=3000 then ‘3級’ when sal>2000 then ‘2級’ else ‘1級’ end 級別 from emp;

語法:case … when … then … when … then … else … end

行列互換
姓名 課程 分數

張三 語文 74

張三 數學 83

張三 物理 93

李四 語文 74

李四 數學 84

李四 物理 94

想變成(得到如下結果):

姓名 語文 數學 物理

—- —- —- —-

李四 74   84   94

張三 74   83   93

——————-

select name,max(case when cource =’語文’ then score else 0 end) from scores group by name;

select name,max(case when cource =’語文’ then score else 0 end)  語文,max(case when cource =’數學’ then score else 0 end) 數學,
max(case when cource =’英語’ then score else 0 end) 英語  from scores group by name;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM