Oracle精選面試題及答案
答案:
select * from emp;
使用*的缺點有:查詢出了不必要的列;效率上不如直接指定列名。
2. 查詢職位(JOB)為'PRESIDENT'的員工的工資
答案:
select * from emp where job = 'PRESIDENT';
3. 查詢佣金(COMM)為0或為NULL的員工資訊
答案:
select * from emp where comm = 0 or comm is null;
4. 查詢入職日期在1981-5-1 到1981-12-31之間的所有員工資訊
答案:
select * from emp where hiredate
between to_date('1981-5-1','yyyy-mm-dd') and to_date('1981-12-31','yyyy-mm-dd');
5. 查詢所有名字長度為4 的員工的員工編號,姓名
答案:
select * from emp where length(ename) = 4;
6. 顯示10 號部門的所有經理('MANAGER')和20號部門的所有職員('CLERK')的詳細資訊
答案:
select * from emp where deptno = 10 and job = 'MANAGER' or deptno = 20 and job='CLERK';
7. 顯示姓名中沒有'L'字的員工的詳細資訊或含有'SM'字的員工資訊
答案:
select * from emp where ename not like '%L%' or ename like '%SM%';
8. 顯示各個部門經理('MANAGER')的工資
答案:
select sal from emp where job = 'MANAGER';
9. 顯示佣金(COMM)收入比工資(SAL)高的員工的詳細資訊
答案:
select * from emp where comm > sal;
10. 把hiredate列看做是員工的生日,求本月過生日的員工
答案:
select * from emp where to_char(hiredate, 'mm') = to_char(sysdate , 'mm');
11. 把hiredate列看做是員工的生日,求下月過生日的員工
答案:
select * from emp where to_char(hiredate, 'mm') = to_char(add_months(sysdate,1) ,'mm');
12. 求1982年入職的員工
答案:
select * from emp where to_char(hiredate,'yyyy') = '1982';
13. 求1981年下半年入職的員工
答案:
select * from emp where hiredate
between to_date('1981-7-1','yyyy-mm-dd') and to_date('1982-1-1','yyyy-mm-dd') - 1;
14. 求1981年各個月入職的的員工個數
答案:
select count(*), to_char(trunc(hiredate,'month'),'yyyy-mm')
from emp where to_char(hiredate,'yyyy')='1981'
group by trunc(hiredate,'month')
order by trunc(hiredate,'month');
15. 查詢各個部門的平均工資
答案:
select deptno,avg(sal) from emp group by deptno;
16. 顯示各種職位的最低工資
答案:
select job,min(sal) from emp group by job;
17. 按照入職日期由新到舊排列員工資訊
答案:
select * from emp order by hiredate desc;
18. 查詢員工的基本資訊,附加其上級的姓名
答案:
select e.*, e2.ename from emp e, emp e2 where e.mgr = e2.empno;
19. 顯示工資比'ALLEN'高的所有員工的姓名和工資
答案:
select * from emp where sal > (select sal from emp where ename='ALLEN');
20. 顯示與'SCOTT'從事相同工作的員工的詳細資訊
答案:
select * from emp where job = (select * from emp where ename='SCOTT');
21. 顯示銷售部('SALES')員工的姓名
答案:
select ename from emp e, dept d where e.deptno = d.deptno and d.dname='SALES';
22. 顯示與30號部門'MARTIN'員工工資相同的員工的姓名和工資
答案:
select ename, sal from emp
where sal = (select sal from emp where deptno=30 and ename='MARTIN');
23. 查詢所有工資高於平均工資(平均工資包括所有員工)的銷售人員('SALESMAN')
答案:
select * from emp where job='SALESMAN' and sal > (select avg(sal) from emp);
24. 顯示所有職員的姓名及其所在部門的名稱和工資
答案:
select ename, job, dname from emp e, dept d where e.deptno = d.deptno;
25. 查詢在研發部('RESEARCH')工作員工的編號,姓名,工作部門,工作所在地
答案:
select empno,ename,dname,loc from emp e, dept d
where e.deptno = d.deptno and danme='RESEARCH';
26. 查詢各個部門的名稱和員工人數
答案:
select * from (select count(*) c, deptno from emp group by deptno) e
inner join dept d on e.deptno = d.deptno;
27. 查詢各個職位員工工資大於平均工資(平均工資包括所有員工)的人數和員工職位
答案:
select job, count(*) from emp where sal > (select avg(sal) from emp) group by job;
28. 查詢工資相同的員工的工資和姓名
答案:
select * from emp e where (select count(*) from emp where sal = e.sal group by sal)> 1;
29. 查詢工資最高的3名員工資訊
答案:
select * from (select * from emp order by sal desc) where rownum <= 3;
30. 按工資進行排名,排名從1開始,工資相同排名相同(如果兩人並列第1則沒有第2名,從第三名繼續排)
答案:
select e.*, (select count(*) from emp where sal > e.sal)+1 rank from emp e order byrank;
31. 求入職日期相同的(年月日相同)的員工
答案:
select * from emp e where (select count(*) from emp where e.hiredate=hiredate)>1;
32. 查詢每個部門的最高工資
答案:
select deptno, max(sal) maxsal from emp group by deptno order by deptno;
33. 查詢每個部門,每種職位的最高工資
答案:
select deptno, job, max(sal) from emp group by deptno, job order by deptno, job;
34. 查詢每個員工的資訊及工資級別
答案:
select e.*, sg.grade from emp e, salgrade sg where sal between losal and hisal;
35. 查詢工資最高的第6-10名員工
答案:
select * from (
select e.*,rownum rn from
(select * from emp order by sal desc) e
where rownum <=10)
where rn > 5;
36. 查詢各部門工資最高的員工資訊
答案:
select * from emp e where e.sal = (select max(sal) from emp where (deptno =e.deptno));
37. 查詢每個部門工資最高的前2名員工
答案:
select * from emp e where
(select count(*) from emp where sal > e.sal and e.deptno = deptno) < 2
order by deptno, sal desc;
38. 查詢出有3個以上下屬的員工資訊
答案:
select * from emp e where
(select count(*) from emp where e.empno = mgr) > 2;
39. 查詢所有大於本部門平均工資的員工資訊
答案:
select * from emp e where sal >
(select avg(sal) from emp where (deptno = e.deptno))
order by deptno;
40. 查詢平均工資最高的部門資訊
答案:
select d.*, avgsal from dept d, (select avg(sal) avgsal, deptno from emp group bydeptno) se
where avgsal = (select max(avg(sal)) from emp group by deptno) and d.deptno =se.deptno;
41. 查詢大於各部門總工資的平均值的部門資訊
答案:
select d.*,sumsal from dept d, (select sum(sal) sumsal, deptno from emp group bydeptno) se
where sumsal >(select avg(sum(sal)) from emp group by deptno) and se.deptno =d.deptno;
42. 查詢大於各部門總工資的平均值的部門下的員工資訊
答案:
select e.*,sumsal from emp e, (select sum(sal) sumsal, deptno from emp group bydeptno) se
where sumsal >(select avg(sum(sal)) from emp group by deptno) and se.deptno =e.deptno;
43. 查詢沒有員工的部門資訊
答案:
select d.* from dept d left join emp e on (e.deptno = d.deptno) where empno is null;
44. 查詢當前月有多少天
答案:
select trunc(add_months(sysdate,1),'month') - trunc(sysdate,'month') from dual;
45. 列出最低薪金大於1500的各種工作及此從事此工作的全部僱員人數
答案:
SELECT job,COUNT(empno)
FROM emp
GROUP BY job HAVING MIN(sal)>1500 ;
46. 列出薪金高於公司平均薪金的所有員工,所在部門,上級領導,公司的工資等級
答案:
SELECT e.empno,e.ename,d.dname,m.ename,s.grade
FROM emp e,dept d,emp m,salgrade s
WHERE sal>(SELECT AVG(sal) FROM emp) AND e.mgr=m.empno AND d.deptno=e.deptno(+)AND e.sal BETWEEN s.losal AND s.hisal ;
47. 列出薪金高於在部門30工作的所有員工的薪金的員工姓名和薪金、部門名稱
答案:
SELECT e.ename,e.sal,d.dname FROM emp e,dept d
WHERE sal > ALL (SELECT sal FROM emp WHERE deptno=30) AND e.deptno=d.deptno;
48. 列出所有部門的詳細資訊和部門人數
答案:
SELECT d.dname,d.loc,dt.count
FROM dept d,(SELECT deptno,COUNT(*) count FROM emp GROUP BY deptno) dt
WHERE d.deptno=dt.deptno ;
49. 顯示非銷售人員工作名稱以及從事同一工作僱員的月工資的總和,並且要滿足從事同一工作的僱員的月工資合計大於$5000,輸出結果按月工資的合計升序排列
答案:
SELECT job,SUM(sal) sum
FROM emp
WHERE job<>'SALESMAN'
GROUP BY job HAVING sum>5000
ORDER BY sum ;
50. 客戶表a(id name address) 登陸流水錶b(id time) 購物流水錶c(id time productid productnum)
1.求每個客戶的最新登陸時間time,姓名name,客戶id?
答案:
select a.id,a.name,d.time as time
from a left join (select id,max(time) as time from b group by id) d
on a.id =d.id ;
2.查最新登陸並且已經購買商品的客戶id,name,登陸的時間time(一條sql語句)
答案:
select a.id,a.name,d.time as time
from a,(select id,max(time) as time from b group by id) d
where a.id =d.id
and exists (select * from c where id = a.id);