mysql数据库命令练习题及答案
1、查询emp中最高薪水人的名字
2、查询每个部门中的最高薪水人的名字和所在的部门编号
3、查询薪水在平均薪水之上的雇员的名字
4、查询雇员的名字和所在部门的名字
5、查询薪水在在本部门平均薪水之上的雇员的名字
6、查询每个员工的薪水的等级,员工的姓名
7、查询每个部门的平均薪水的等级,部门的编号
8、查询雇员的名字,所在部门的名字,工资的等级
9、查询雇员的名字和其经理的名字
10、查询雇员中是经理人的名字
11、查询平均薪水最高的部门的编号和名称
12、查询薪水最高的前5名雇员编号,名称,薪水
13、查询薪水最高的第6名到第10名雇员编号,名称,薪水
14、查询部门的名字和部门的人数(如果部门里没有人数,显示0个)
15、查询员工的编号,工资和所在部门的平均工资
三个表的结构介绍:
三个表的创建语句:
/********************************部门表dept********************************/
/*创建表*/
DROP TABLE IF EXISTS DEPT;
CREATE TABLE DEPT(
DEPTNO INT PRIMARY KEY,
DNAME VARCHAR(14) ,
LOC VARCHAR(13)
);
/*插入数据*/
INSERT INTO DEPT VALUES
(10,'ACCOUNTING','NEW YORK'),
(20,'RESEARCH','DALLAS'),
(30,'SALES','CHICAGO'),
(40,'OPERATIONS','BOSTON');
/*查询数据*/
SELECT * FROM DEPT;
/********************************员工表emp********************************/
/*创建表*/
DROP TABLE IF EXISTS EMP;
CREATE TABLE EMP(
EMPNO INT PRIMARY KEY,
ENAME VARCHAR(14) ,
JOB VARCHAR(9),
MGR INT,
HIREDATE DATE,
SAL DECIMAL(7,2),
COMM DECIMAL(7,2),
DEPTNO int REFERENCES DEPT
);
/*插入数据*/
INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20),
(7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600,300,30),
(7521,'WARD','SALESMAN',7698,'1981-2-22',1250,500,30),
(7566,'JONES','MANAGER',7839,'1981-4-2',2975,NULL,20),
(7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250,1400,30),
(7698,'BLAKE','MANAGER',7839,'1981-5-1',2850,NULL,30),
(7782,'CLARK','MANAGER',7839,'1981-6-9',2450,NULL,10),
(7788,'SCOTT','ANALYST',7566,'1987-7-13',3000,NULL,20),
(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10),
(7844,'TURNER','SALESMAN',7698,'1981-9-8',1500,0,30),
(7876,'ADAMS','CLERK',7788,'1987-7-13',1100,NULL,20),
(7900,'JAMES','CLERK',7698,'1981-12-3',950,NULL,30),
(7902,'FORD','ANALYST',7566,'1981-12-3',3000,NULL,20),
(7934,'MILLER','CLERK',7782,'1982-1-23',1300,NULL,10);
/*查询数据*/
SELECT * FROM EMP;
/********************************工资等级表salgrade********************************/
/*创建表*/
DROP TABLE IF EXISTS SALGRADE;
CREATE TABLE SALGRADE(
GRADE INT PRIMARY KEY,
LOSAL INT,
HISAL INT
);
/*插入数据*/
INSERT INTO SALGRADE VALUES
(1,700,1200),
(2,1201,1400),
(3,1401,2000),
(4,2001,3000),
(5,3001,9999);
/*查询数据*/
SELECT * FROM SALGRADE;
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
答案:
/*1、查询emp中最高薪水人的名字*/
#1、查询最高的薪水
select max(sal) from EMP;
#2、将1作为条件
select
ename
from
EMP
where
sal = (select max(sal) from EMP);
123456789101112
/*2、查询每个部门中的最高薪水人的名字和所在的部门编号*/
#1、各个部门的最高薪水
select
max(sal),deptno from EMP
group
by deptno;
#2、将1的结果作为一个新的表,联表查询
select
t1.ename,t1.deptno
from
EMP t1
inner join
(select max(sal) max_sal,deptno from EMP group by deptno) t2
on
t1.deptno = t2.deptno and sal = t2.max_sal;
12345678910111213141516
/*3、查询薪水在平均薪水之上的雇员的名字*/
select ename from EMP where sal >(select avg(sal) from EMP);
12
/*4、查询雇员的名字和所在部门的名字*/
select
t1.ename,t2.dname
from
EMP t1,DEPT t2
where
t1.deptno = t2.deptno;
1234567
/*5、查询薪水在在本部门平均薪水之上的雇员的名字*/
select
t1.ename,t1.deptno
from
EMP t1
inner join
(select avg(sal) avg_sal,deptno from EMP group by deptno) t2
on t1.deptno = t2.deptno and sal > t2.avg_sal;
12345678
/*6、查询每个员工的薪水的等级,员工的姓名*/
select * from EMP;
select * from SALGRADE;
select t1.ename,t2.grade
from EMP t1,SALGRADE t2
where t1.sal between t2.losal and t2.hisal;
1234567
/*7、查询每个部门的平均薪水的等级,部门的编号*/
#1、各个部门的平均薪水
select avg(sal),deptno from EMP group by deptno;
#2、将1的结果当作一个表
select
t1.grade,t2.deptno
from
SALGRADE t1,(select avg(sal) avg_sal,deptno from EMP group by deptno) t2
where
t2.avg_sal between t1.losal and t1.hisal
12345678910
/*8、查询雇员的名字,所在部门的名字,工资的等级*/
select t1.ename,t2.dname,t3.grade
from
EMP t1,DEPT t2,SALGRADE t3
where
t1.deptno = t2.deptno
and
t1.sal between t3.losal and t3.hisal;
select t1.ename,t2.dname,t3.grade
from
EMP t1
inner join
DEPT t2
on
t1.deptno = t2.deptno
inner join
SALGRADE t3
on
t1.sal between t3.losal and t3.hisal;
1234567891011121314151617181920
/*9、查询雇员的名字和其经理的名字*/
select employee.ename,employer.ename
from EMP employer,EMP employee
where employee.mgr = employer.empno;
1234
/*10、查询雇员中是经理人的名字*/
#1、经理上的编号
select distinct mgr from EMP;
#2、将1当作条件
select
ename
from
EMP
where
empno in(select distinct mgr from EMP);
12345678910
/*11、查询平均薪水最高的部门的编号和名称*/
#1、每个部门的平均薪水
select avg(sal) avg_sal,deptno from EMP group by deptno;
#2、求1表中的最高平均薪水
select max(t.avg_sal) from (select avg(sal) avg_sal,deptno from EMP group by deptno) t
#3、将2的结果当作条件
select t1.deptno
from (select avg(sal) avg_sal,deptno from EMP group by deptno) t1
where t1.avg_sal = (select max(t2.avg_sal) from (select avg(sal) avg_sal,deptno from EMP group by deptno) t2)
#4、将3作为条件
select t.deptno,t.dname
from DEPT t
where deptno in (
select t1.deptno
from (select avg(sal) avg_sal,deptno from EMP group by deptno) t1
where t1.avg_sal = (select max(t2.avg_sal) from (select avg(sal) avg_sal,deptno from EMP group by deptno) t2)
);
1234567891011121314151617
/*12、查询薪水最高的前5名雇员编号,名称,薪水*/
select empno,ename,sal
from EMP
order by sal desc
limit 5;
12345
/*13、查询薪水最高的第6名到第10名雇员编号,名称,薪水*/
select empno,ename,sal
from EMP
order by sal desc
limit 5,5;
12345
/*14、查询部门的名字和部门的人数(如果部门里没有人数,显示0个)*/
select
t1.dname,ifnull(t2.num,0)
from
DEPT t1
left join
(select count(*) num,deptno from EMP group by deptno) t2
on
t1.deptno = t2.deptno;
123456789
/*15、查询员工的编号,工资和所在部门的平均工资*/
select
empno,ename,sal,t2.deptno,(select avg(sal) from EMP t1 where t1.deptno = t2.deptno)
from
EMP t2;
select
empno,ename,sal,t2.deptno,t2.avg_sal
from
EMP t1
inner join
(select avg(sal) avg_sal,deptno from EMP group by deptno) t2
on
t1.deptno = t2.deptno;