資料庫面試題(三)
41、查詢各職位的員工工資的最大值,最小值,平均值,總和
select job, max(sal),min(sal), avg(sal), sum(sal)
fromemp
groupby job;
42、選擇具有各個job的員工人數(提示:對job進行分組)
select job, count(*)
fromemp
groupby job;
43、查詢員工最高工資和最低工資的差距,列名為DIFFERENCE;
selectmax(sal)-min(sal) "DIFFERENCE"
fromemp;
44、查詢各個管理者屬下員工的最低工資,其中最低工資不能低於800,沒有管理者的員工不計算在內
select mgr, min(sal)
from emp
where mgr is not null
group by mgr
having min(sal) >=800;
45、查詢所有部門的部門名字dname,所在位置loc,員工數量和工資平均值;
select dept.dname,dept.loc, COUNT, AVG
from dept
join(
select deptno, count(*)as"COUNT", avg(sal) as "AVG"
from emp
group by deptno
)
using(deptno);
46、查詢和scott相同部門的員工姓名ename和僱用日期hiredate
select ename, hiredate
from emp
where deptno = (select deptnofrom empwhere emp.ename = 'SCOTT');
47、查詢工資比公司平均工資高的所有員工的員工號empno,姓名ename和工資sal。
select empno, ename, sal
from emp
where sal > (select avg(sal)from emp);
48、查詢和姓名中包含字母u的員工在相同部門的員工的員工號empno和姓名ename
select empno, ename
from emp
where deptno in (select deptnofrom empwhere ename like '%U%');
49、查詢在部門的loc為newYork的部門工作的員工的員工姓名ename,部門名稱dname和崗位名稱job
select e.ename, d.dname, e.job
from emp e join dept d
using (deptno)
where deptno = (select deptnofrom deptwhere loc = 'NEW YORK');
50、查詢管理者是king的員工姓名ename和工資sal
select ename, sal
from emp
where mgr = (select empno fromemp whereename = 'KING');
51、顯示Operations部門有哪些職位
select distinct e.job from empe , dept d where e.deptno = d.deptnoand d.dname = 'Operations';
52、各個部門中工資大於1500的員工人數
select dept_id , count(*) cntfrom s_emp where salary > 1500group by dept_id;
53、哪些員工的工資,高於整個公司的平均工資,列出員工的名字和工資(降序)
select first_name , salaryfrom s_emp where salary > (selectavg(salary) from s_emp) order by salarydesc;
54、所在部門平均工資高於1500的員工名字
select first_name , salaryfrom s_emp where dept_id in (selectdept_id from s_emp group by dpet_id havingavg(salary) > 1500);
55、列出各個部門中工資最高的員工的資訊:名字、部門號、工資
select first_name , salary ,dept_id from s_emp where (dept_id ,salary) in (select dept_id , max(salary)from s_emp group by dept_id);
56、哪個部門的平均工資是最高的,列出部門號、平均工資
select dept_id,avg(salary)from s_emp group by dept_id havingavg(salary) = (select max(avg(salary)) froms_emp);
57、建立表employee,欄位為:
Id number
First_Name varchar2(20),
last_Name varchar2(20),
mgrid NUMBER,
Job varchar2(20),
Salary number(7,2)
CREATE TABLE employee(
id NUMBER,
first_name VARCHAR2(20),
last_nameVARCHAR2(20),
mgrid NUMBER,
salary NUMBER(7,2));
58、向表中插入下列資料,並提交,查詢資料;
ID |
FIRST_NAME |
LAST_NAME |
MGRID |
SALARY |
1 |
Rose |
Tyler |
4 |
1500 |
2 |
Matha |
Jones |
4 |
2200 |
3 |
Donna |
Noble |
4 |
1300 |
4 |
Doctor |
Who |
3500 |
|
5 |
Jack |
Harkness |
1 |
3000 |
INSERT INTO employee VALUES(1, 'Rose','Tyler', 4, 1500);
INSERT INTO employee VALUES(2, 'Martha','Jones', 4, 2200);
INSERT INTO employee VALUES(3, 'Donna','Noble', 4, 1300);
INSERT INTO employee VALUES(4, 'Doctor','Who', NULL,3500);
INSERT INTO employee VALUES(5, 'Jack','Harkness', 1, 3000);
59.將3號員工的last_name修改為“Tate”,並提交,查詢資料;
UPDATE employee SET last_name= 'Tate'WHERE id = 3;
60.將所有工資少於2000的員工的工資修改為2000 (不提交),並設定儲存點,查詢資料;
UPDATE employee SET salary =2000 WHEREsalary < 2000;
SAVEPOINT A;
61.刪除employee表中所有資料(不提交),查詢資料;
DELETE FROM employee;
SELECT * FROM employee;
62.回滾到60題中的設定的儲存點,查詢資料;
Rollback to A;
SELECT * FROM employee;
63.刪除表employee中所有資料,並提交,查詢資料;
DELETE FROM employee;
COMMIT;
SELECT * FROM employee;
64、現有資料表Customer,其結構如下所示:
cust_idNUMBER(4) Primary Key, --客戶編碼
cnameVARCHAR2(25) Not Null, --客戶姓名
csexCHAR(6) --客戶性別
birthdayDATE, --客戶生日
account NUMBER. --客戶賬戶餘額
1)、建立表,客戶編碼為主鍵,姓名不能為空,性別只能選擇”男”或”女”;
CREATE TABLEcustomer(
cust_id NUMBERPRIMARY KEY,
cname VARCHAR2(25)NOT NULL,
csex CHAR(6),
birthday DATE,
account NUMBER,
CONSTRAINTcust_csex_chk CHECK(csex IN (‘男’, ‘女’),));
2).構造SQL語句,向Customer資料表中插入一條記錄,其資訊如下:客戶編碼使用第一步建立的序列獲得,客戶姓名為sean,性別為”男”,生日為1987-11-17,帳戶餘額為12345元。
INSERT INTOcustomer(cust_id,cname, birthday, account) VALUES(cust_seq.NEXTVAL, 'sean',
TO_DATE('1987-11-17','yyyy-mm-dd'),12345);
65、建立一個序列,初始值是1000,步進是10;
Create sequence start with1000 increment by 10;
66、建立一個檢視v_emp,內容是按部門分組,各個部門的薪水總和和員工人數;檢視欄位定義為deptno,total_sal, total_count;
Create or replace view
Is
Select deptno, sum(sal)total_sal, count(*) total_count
From emp group by deptno;
67、查詢檢視,列出全部記錄;
Select * from v_emp;
68、列出薪水最低的三名員工的名字;
SELECT ename, sal, ROWNUM
FROM (SELECT ename,sal
FROM emp
ORDER BYsal)
WHERE ROWNUM <= 3;
69、列出按員工編碼排序,第6到第10條員工記錄。
SELECT ename, sal, rn
FROM (SELECT ROWNUMrn, ename, sal
FROM emp
ORDER BY empno )
WHERE rn BETWEEN 6 AND10;
70、有員工表empinfo
(
Fempno varchar2(10) not nullpk,
Fempname varchar2(20) notnull,
Fage number not null,
Fsalary number not null
);
假如資料量很大約1000萬條;寫一個你認為最高效的SQL,用一個SQL計算以下四種人:
fsalary>9999 and fage >35
fsalary>9999 and fage <35
fsalary <9999 and fage >35
fsalary <9999 and fage <35
每種員工的數量;
select sum(case when fsalary> 9999 and fage > 35
then 1
else 0 end) as"fsalary>9999_fage>35",
sum(case when fsalary >9999 and fage < 35
then 1
else 0
end) as"fsalary>9999_fage<35",
sum(case when fsalary <9999 and fage > 35
then 1
else 0
end) as"fsalary<9999_fage>35",
sum(case when fsalary <9999 and fage < 35
then 1
else 0
end) as "fsalary<9999_fage<35"
from empinfo;
71、aa,bb表都有20個欄位,且記錄數量都很大,aa,bb表的X欄位(非空)上有索引, 請用SQL列出aa表裡面存在的X在bb表不存在的X的值,請寫出認為最快的語句,並解譯原因。
select aa.x from aa
where not exists (select 'x'from bb where aa.x = bb.x) ;
以上語句同時使用到了aa中x的索引和的bb中x的索引
72. having與where的區別
having 和where 都是用來篩選用的,having 是分組後過濾 而where是分組前過濾