1. 程式人生 > >sql練習60題(mysql)

sql練習60題(mysql)

lac red distinct style creat select primary 數據 ima

兩張表:員工表、部門表

準備數據

CREATE DATABASE oa;
USE oa;


CREATE TABLE dept(
deptno INT PRIMARY KEY,
dname VARCHAR(20),
loc VARCHAR(20)
)

DROP TABLE emp
CREATE TABLE emp(
empno INT PRIMARY KEY,
ename VARCHAR(20) NOT NULL,
job VARCHAR(20) CHECK (job IN (CLERK,SALESMAN,MANAGER,SALESMAN,ANALYST)),
mgp INT
, hiredate DATETIME , sal DECIMAL(10,2), comm DECIMAL(10,2), DEPTNO INT FOREIGN KEY REFERENCES dept(deptno) ) INSERT INTO dept VALUES (10,ACCOUNTING,NEWTORK) INSERT INTO dept VALUES (20,RESEARCH,DALLAS) INSERT INTO dept VALUES (30,SALES,CHICAGO) INSERT INTO dept VALUES (40,OPERATIONS,BOSTON
) insert into emp values(7369,SMITH,CLERK,7902,1980-12-17,1640,NULL,20); insert into emp values(7499,ALLEN,SALESMAN,7698,1981-2-20,11400,300,30); insert into emp values(7521,WARD,SALESMAN,7698,1981-2-22,5200,500,30); insert into emp values(7566,JOENS,MANAGER,7839,1981-4-2,7015,NULL,20); insert
into emp values(7654,MARTIN,SALESMAN,7698,1981-9-28,5200,1400,30); insert into emp values(7698,BLAKE,MANAGER,7839,1981-5-1,5900,NULL,30); insert into emp values(7782,CLARK,MANAGER,7839,1981-6-9,2470,NULL,10); insert into emp values(7788,SCOTT,ANALYST,7566,1987-4-19,3040,NULL,20); insert into emp values(7844,TURNER,SALESMAN,7698,1980-12-17,6200,0,30); insert into emp values(7876,ADAMS,CLERK,7788,1981-9-8,2240,NULL,20); insert into emp values(7900,JAMES,CLERK,7698,1987-5-23,4000,NULL,30); insert into emp values(7902,FORD,ANALYST,7566,1981-12-3,3040,NULL,20); insert into emp values(7934,MILLER,CLERK,7782,1982-12-3,2620,NULL,10);

員工部門表綜合查詢60題

(1) 查詢20部門的所有員工信息。

select * from emp where deptno=20;

(2) 查詢所有工種為CLERK的員工的員工號、員工名和部門號。

select empno,ename,deptno from emp where job=‘CLERK‘

(3) 查詢獎金(COMM)高於工資(SAL)的員工信息。

select * from emp where isnull(comm,0)>sal

(4) 查詢獎金高於工資的20%的員工信息。

select * from emp where isnull(comm,0)>sal*0.2

(5) 查詢10號部門中工種為MANAGER和20部門中工種為CLERK的員工的信息。

select * from emp

where job=‘MANAGER‘ and deptno=10

union

select * from emp

where job=‘CLERK‘ and deptno=20

--

select * from emp

where (job=‘MANAGER‘ and deptno=10)

or (job=‘CLERK‘ and deptno=20

)

(6) 查詢所有工種不是MANAGER和CLERK,

--且工資大於或等於2000的員工的詳細信息。

select * from emp

where job not in(‘MANAGER‘,‘CLERK‘)

and sal>=2000

(7) 查詢有獎金的員工的不同工種。

select distinct job from emp

where comm is not null

(8) 查詢所有員工工資與獎金的和。

select ename,sal+isnull(comm,0) 實發工資

from emp

(9) 查詢沒有獎金或獎金低於100的員工信息。

select *

from emp

where comm is null or comm<100

(10) 查詢各月倒數第3天(倒數第2天)入職的員工信息。

select *

from emp

where DATENAME(day,hiredate+3)=1

(11) 查詢工齡大於或等於25年的員工信息。

select ename 姓名,hiredate 雇用日期,datediff(year,hiredate,getdate()) 工資

from emp

where datediff(year,hiredate,getdate())>=25

(12) 查詢員工信息,要求以首字母大寫的方式顯示所有員工的姓名。

select upper(SUBSTRING(ename,1,1))+lower(substring(ename,2,(len(ename)-1)))

from emp

(13) 查詢員工名正好為6個字符的員工的信息。

select ename from emp where len(ename)=6

(14) 查詢員工名字中不包含字母“S”的員工。

select ename from emp where ename not like ‘%S%‘

(15) 查詢員工姓名的第二字母為“M”的員工信息。

select ename from emp

where ename like ‘_M%‘

(16) 查詢所有員工姓名的前三個字符。

select ename 員工姓名,substring(ename,1,3) 員工姓名的前三個字符

from emp

(17) 查詢所有員工的姓名,如果包含字母“S”,則用“s”替換。

--返回被替換了指定子串的字符串。

--REPLACE (<string_expression1>,<string_expression2>,<string_expression3>)

--用string_expression3 替換在string_expression1 中的子串string_expression2。

select replace(ename,‘S‘,‘s‘)

from emp

(18) 查詢員工的姓名和入職日期,並按入職日期從先到後進行排序。

select ename,hiredate

from emp

order by hiredate

(19) 顯示所有員工的姓名、工種、工資和獎金,按工種降序排序,

--若工種相同則按工資升序排序。

select ename,job,sal,comm

from emp

order by job desc

(20) 顯示所有員工的姓名、入職的年份和月份,

--按入職日期所在的月份排序,若月份相同則按入職的年份排序。

select ename,hiredate 入職日期,DATENAME(yy,hiredate) 入職的年份,datename(mm,hiredate) 入職的月份

from emp

(21) 查詢在2月份入職的所有員工信息。

select * from emp

where datename(mm,hiredate)=2

(22) 查詢所有員工入職以來的工作期限,用“XX年XX月XX日”的形式表示。

select ename,datename(yy,hiredate)+‘年‘+datename(mm,hiredate)+‘月‘+datename(dd,hiredate)+‘日‘ 工作期限

from emp

(23.1) 查詢至少有一個員工的部門信息。

select d.dname,count(empno) 部門人數

from emp e

right join dept d on d.deptno=e.deptno

group by d.dname,e.deptno

having count(empno)>=1

(23.2) 查詢至少有兩個員工的部門信息。

select d.dname,count(empno) 部門人數

from emp e

right join dept d on d.deptno=e.deptno

group by d.dname,e.deptno

having count(empno)>1

(24) 查詢工資比SMITH員工工資高的所有員工信息。

select *

from emp

where sal>(

select sal from emp where ename=‘SMITH‘

)

(25) 查詢所有員工的姓名及其直接上級的姓名。

select ename 員工的姓名,(

select ename from emp e2 where e2.empno=e1.mgp

) 直接上級

from emp e1

(26) 查詢入職日期早於其直接上級領導的所有員工信息。

select ename 員工的姓名,hiredate 入職日期,(

select ename from emp e2 where e2.empno=e1.mgp

) 直接上級,(

select hiredate from emp e2 where e2.empno=e1.mgp

) 直接上級入職日期

from emp e1

where e1.hiredate<(select hiredate

from emp e2 where e2.empno=e1.mgp

)

(27) 查詢所有部門及其員工信息,包括那些沒有員工的部門。

select dept.dname,emp.ename

from dept

left outer join emp on emp.deptno=dept.deptno

(28) 查詢所有員工及其部門信息,包括那些還不屬於任何部門的員工。

select dept.dname,emp.ename

from emp

left outer join dept on emp.deptno=dept.deptno

(29) 查詢所有工種為CLERK的員工的姓名及其部門名稱。

select dept.dname,emp.ename,emp.job

from emp

left outer join dept on emp.deptno=dept.deptno

where job=‘CLERK‘

(30) 查詢最低工資大於2500的各種工作。

select job,sal

from emp

where sal>2500

-----------------------------------------------------------------

員工部門表綜合查詢60題(下)

2010-11-26 11:31員工部門表綜合查詢60題(下)

(31) 查詢平均工資低於2000的部門及其員工信息。

select *

from dept left outer join emp on dept.deptno=emp.deptno

where dept.deptno in (

select deptno from emp

group by deptno

having avg(sal)<2000)

(32) 查詢在SALES部門工作的員工的姓名信息。

--法一:表連接

select *

from dept left outer join emp on dept.deptno=emp.deptno

where dept.dname=‘SALES‘

--法二:子查詢

select * from emp

where emp.deptno=(

select deptno from dept where dname=‘SALES‘

)

(33) 查詢工資高於公司平均工資的所有員工信息。

select * from emp

where sal>(

select avg(sal)

from emp)

(34) 查詢出與SMITH員工從事相同工作的所有員工信息。

select * from emp where job = (

select job

from emp

where ename=‘SMITH‘)

(35) 列出工資等於30部門中某個員工的工資的所有員工的姓名和工資。

select *

from emp

where sal in (

select sal

from emp

where deptno=30) and deptno!=30

(36) 查詢工資高於30部門工作的所有員工的工資的員工姓名和工資。

select *

from emp

where sal > all(

select sal

from emp

where deptno=30)

(37) 查詢每個部門中的員工數量、平均工資和平均工作年限。

select dname 部門,count(ename) 員工數量,isnull(avg(sal),0) 平均工資,

isnull(avg(datediff(yy,hiredate,getdate())),0) 平均工作年限

from dept d

left outer join emp e on d.deptno=e.deptno

group by d.dname

(38) 查詢從事同一種工作但不屬於同一部門的員工信息。

select *

from emp e1

where e1.job in (

select distinct e2.job

from emp e2

where e2.deptno != e1.deptno

)

(39) 查詢各個部門的詳細信息以及部門人數、部門平均工資。

select d.dname 部門名稱,d.deptno 部門編號,count(e.empno) 人數,avg(e.sal) 平均工資

from dept d

left outer join emp e on d.deptno=e.deptno

group by d.deptno,d.dname

(40) 查詢各種工作的最低工資。

select job 工種,min(sal) 最低工資

from emp

group by job

(41) 查詢各個部門中不同工種的最高工資。

select dname 部門名稱,job 工種,max(isnull(sal,0)) 最高工資

from dept d left join emp e on d.deptno=e.deptno

group by job,dname

(42) 查詢10號部門員工及其領導的信息。

select deptno 部門,ename 姓名 ,(select e2.ename from emp e2 where e2.mgp=e1.empno) 上級領導

from emp e1

where deptno=10

(43) 查詢各個部門的人數及平均工資。

select dname 部門名稱,count(ename) 部門人數,avg(isnull(sal,0)) 平均工資

from dept d left outer join emp e on d.deptno=e.deptno

group by d.dname

(44) 查詢工資為某個部門平均工資的員工的信息。

select * from emp

where sal in(

select avg(sal)

from emp group by deptno

)

(45) 查詢工資高於本部門平均工資的員工的信息。

select *

from emp e1

where sal>(

select avg(sal)

from emp e2

where e2.deptno=e1.deptno

)

(46) 查詢工資高於本部門平均工資的員工的信息及其部門的平均工資。

select *,(select avg(sal) from emp e2 where e2.deptno=e1.deptno) 部門平均工資

from emp e1

where sal>(

select avg(sal) from emp e2 where e2.deptno=e1.deptno

)

(47) 查詢工資高於20號部門某個員工工資的員工的信息。

select *

from emp e1

where sal> any(

select sal from emp e where deptno=20

)

(48)統計各個工種的員工人數與平均工資。

select job 工種,count(empno) 員工人數,avg(sal) 平均工資

from emp

group by job

(49) 統計每個部門中各工種的人數與平均工資。

select dname 部門,job 工種,count(empno) 人數,avg(isnull(sal,0)) 平均工資

from dept d left outer join emp e on d.deptno=e.deptno

group by job,dname

(50) 查詢其他部門中工資、獎金與30號部門某員工工資、

--獎金都相同的員工的信息。沒有查詢結果

select *

from emp e

where isnull(sal,0)+isnull(comm,0) in (

select isnull(sal,0)+isnull(comm,0)

from emp e1

where e1.deptno=30 and e.sal=e1.sal and e.comm=e1.comm and e.deptno!=30

)

(51) 查詢部門人數大於5的部門的員工信息。

select * from emp

where deptno in(

select deptno

from emp

group by deptno

having count(empno)>5)

(52) 查詢所有員工工資都大於1000的部門的信息。

select *

from dept d

where deptno in (

select deptno from emp e

group by deptno

having min(sal)>1000

)

(53) 查詢所有員工工資都大於1000的部門的信息及其員工信息。

select *

from dept d left outer join emp e on d.deptno=e.deptno

where e.deptno in (

select deptno from emp e1

group by deptno

having min(sal)>1000

)

(54) 查詢所有員工工資都在900~3000之間的部門的信息。

select * from dept

where deptno in(

select deptno from emp

group by deptno

having min(sal)>900 and max(sal)<3000

)

(55) 查詢有工資在900~3000之間的員工所在部門的員工信息。

select * from emp

where deptno in(

select deptno from emp

group by deptno

having min(sal)>900 and max(sal)<3000

)

(56) 查詢每個員工的領導所在部門的信息。

select ename 員工,(

select e1.ename from emp e1 where emp.mgp=e1.empno

) 領導,(

select d.dname

from emp e left outer join dept d on e.deptno=d.deptno

where emp.mgp=e.empno

) 領導所在部門

from emp

(57) 查詢人數最多的部門信息。

select * from dept

where deptno =(

select top 1 deptno

from emp

group by deptno

order by -count(empno))

(58) 查詢30號部門中工資排序前3名的員工信息。

select top 3 *

from emp

where deptno=30

order by -sal

(59) 查詢所有員工中工資排序在5到10名之間的員工信息。

select top 5 *

from (

select top 10 *

from emp

order by -sal) e

order by sal

(60) 查詢指定年份之間入職的員工信息。(1980-1985)

select *

from emp

where datename(year,hiredate) between 1980 and 1985

CREATE DATABASE oa;USE oa;

CREATE TABLE dept(deptno INT PRIMARY KEY,dname VARCHAR(20),loc VARCHAR(20))
DROP TABLE empCREATE TABLE emp(empno INT PRIMARY KEY,ename VARCHAR(20) NOT NULL,job VARCHAR(20) CHECK (job IN (‘CLERK‘,‘SALESMAN‘,‘MANAGER‘,‘SALESMAN‘,‘ANALYST‘)),mgp INT ,hiredate DATETIME ,sal DECIMAL(10,2),comm DECIMAL(10,2),DEPTNO INT FOREIGN KEY REFERENCES dept(deptno))
INSERT INTO dept VALUES (10,‘ACCOUNTING‘,‘NEWTORK‘)INSERT INTO dept VALUES (20,‘RESEARCH‘,‘DALLAS‘)INSERT INTO dept VALUES (30,‘SALES‘,‘CHICAGO‘)INSERT INTO dept VALUES (40,‘OPERATIONS‘,‘BOSTON‘)
insert into emp values(7369,‘SMITH‘,‘CLERK‘,7902,‘1980-12-17‘,1640,NULL,20);insert into emp values(7499,‘ALLEN‘,‘SALESMAN‘,7698,‘1981-2-20‘,11400,300,30);insert into emp values(7521,‘WARD‘,‘SALESMAN‘,7698,‘1981-2-22‘,5200,500,30);insert into emp values(7566,‘JOENS‘,‘MANAGER‘,7839,‘1981-4-2‘,7015,NULL,20);insert into emp values(7654,‘MARTIN‘,‘SALESMAN‘,7698,‘1981-9-28‘,5200,1400,30);insert into emp values(7698,‘BLAKE‘,‘MANAGER‘,7839,‘1981-5-1‘,5900,NULL,30);insert into emp values(7782,‘CLARK‘,‘MANAGER‘,7839,‘1981-6-9‘,2470,NULL,10);insert into emp values(7788,‘SCOTT‘,‘ANALYST‘,7566,‘1987-4-19‘,3040,NULL,20);insert into emp values(7844,‘TURNER‘,‘SALESMAN‘,7698,‘1980-12-17‘,6200,0,30);insert into emp values(7876,‘ADAMS‘,‘CLERK‘,7788,‘1981-9-8‘,2240,NULL,20);insert into emp values(7900,‘JAMES‘,‘CLERK‘,7698,‘1987-5-23‘,4000,NULL,30);insert into emp values(7902,‘FORD‘,‘ANALYST‘,7566,‘1981-12-3‘,3040,NULL,20);insert into emp values(7934,‘MILLER‘,‘CLERK‘,7782,‘1982-12-3‘,2620,NULL,10);

sql練習60題(mysql)