作者:叁念
SQL语句练习以及答案
使用以下语句做好作业环境:
部门表dept(部门序号,部门名,部门位置)
员工表emp(员工编号,名字,职业,直接上级编号,受雇日期,月薪,补贴,部门序号)
--创建使用数据库:
create database scott;
use scott;
--创建表格:
CREATE TABLE DEPT
(DEPTNO INT,
DNAME VARCHAR(20),
LOC VARCHAR(20),
CONSTRAINT PK_DEPT PRIMARY KEY(DEPTNO));
CREATE TABLE EMP
(EMPNO int,
ENAME VARCHAR(20),
JOB VARCHAR(20),
MGR int,
HIREDATE DATE,
SAL int,
COMM int,
DEPTNO int,
CONSTRAINT PK_EMP PRIMARY KEY(EMPNO),
CONSTRAINT FK_DEPTNO FOREIGN KEY (DEPTNO)
REFERENCES DEPT (DEPTNO));
--插入数据
Insert into DEPT (DEPTNO,DNAME,LOC) values (10,'ACCOUNTING','NEW YORK');
Insert into DEPT (DEPTNO,DNAME,LOC) values (20,'RESEARCH','DALLAS');
Insert into DEPT (DEPTNO,DNAME,LOC) values (30,'SALES','CHICAGO');
Insert into DEPT (DEPTNO,DNAME,LOC) values (40,'OPERATIONS','BOSTON');
INSERT INTO `emp` VALUES (7369, 'SMITH', 'CLERK', 7902, '1980-12-17', 800, NULL, 20);
INSERT INTO `emp` VALUES (7499, 'ALLEN', 'SALESMAN', 7698, '1981-2-20', 1600, 300, 30);
INSERT INTO `emp` VALUES (7521, 'WARD', 'SALESMAN', 7698, '1981-2-22', 1250, 500, 30);
INSERT INTO `emp` VALUES (7566, 'JONES', 'MANAGER', 7839, '1981-4-2', 2975, NULL, 20);
INSERT INTO `emp` VALUES (7654, 'MARTIN', 'SALESMAN', 7698, '1981-9-28', 1250, 1400, 30);
INSERT INTO `emp` VALUES (7698, 'BLAKE', 'MANAGER', 7839, '1981-5-1', 2850, NULL, 30);
INSERT INTO `emp` VALUES (7782, 'CLARK', 'MANAGER', 7839, '1981-6-9', 2450, NULL, 10);
INSERT INTO `emp` VALUES (7788, 'SCOTT', 'ANALYST', 7566, '1987-4-19', 3000, NULL, 20);
INSERT INTO `emp` VALUES (7839, 'KING', 'PRESIDENT', NULL, '1981-11-17', 5000, NULL, 10);
INSERT INTO `emp` VALUES (7844, 'TURNER', 'SALESMAN', 7698, '1981-9-8', 1500, 0, 30);
INSERT INTO `emp` VALUES (7876, 'ADAMS', 'CLERK', 7788, '1987-5-23', 1100, NULL, 20);
INSERT INTO `emp` VALUES (7900, 'JAMES', 'CLERK', 7698, '1981-12-3', 950, NULL, 30);
INSERT INTO `emp` VALUES (7902, 'FORD', 'ANALYST', 7566, '1981-12-3', 3000, NULL, 20);
INSERT INTO `emp` VALUES (7934, 'MILLER', 'CLERK', 7782, '1982-1-23', 1300, NULL, 10);
--1.查询至少有一个员工的部门
SELECT *FROM dept
WHERE deptno IN (SELECT DEPTNO FROM emp);
SELECT *FROM dept
WHERE EXISTS (SELECT * FROM emp WHERE dept.deptno = emp.deptno)
--2.查询薪水比SMITH高的员工信息
SELECT * FROM emp
WHERE sal >(SELECT sal FROM emp WHERE ename = 'SMITH')
--3.查询最低薪水低于1500的所有工作和最低薪水
SELECT job,MIN(sal) FROM emp
WHERE MIN(sal) < 1500
*WHERE 字句中不能有聚合函数,应修改为如下:
SELECT job,MIN(sal) FROM emp
WHERE sal < 1500
GROUP BY job
*HAVING 字句中可以使用聚合函数,如下:
SELECT job,MIN(sal)
FROM emp
GROUP BY job
HAVING MIN(sal)<1500
--4.查询部门编号为10号的员工信息
SELECT *
FROM emp
WHERE deptno = 10
--5.查询工资在2000到3000之间的员工所有信息
SELECT *
FROM emp
WHERE sal BETWEEN 2000 AND 3000
--6.查询有补贴的员工信息
SELECT *
FROM emp
WHERE comm IS NOT NULL
--7.查询是否有员工叫SMITH,KING,OBAMA的?
SELECT *
FROM emp
WHERE ename IN ('SMITH','king','OBAMA')
*注意以上king小写也能查出,若要区分大小写,修改如下:
SELECT *
FROM emp
WHERE ename COLLATE utf8_bin
IN ('SMITH','king','OBAMA')
--8.查询S开头的员工信息
SELECT *
FROM emp
WHERE ename LIKE 'S%'
--9.查询第二个字母是A的员工信息
SELECT *
FROM emp
WHERE ename LIKE '_A%'
--10.查询1982年之后入职的员工信息
SELECT *
FROM emp
WHERE hiredate > '1982'
11.查询各部门的平均薪资
SELECT AVG(sal)
FROM emp
GROUP BY deptno
+查询各部门的部门名和平均薪资
SELECT dname,AVG(sal)
FROM dept LEFT JOIN emp ON dept.deptno = emp.deptno
GROUP BY dept.deptno
12.查询各部门各岗位的平均薪资和最高薪资
SELECT deptno,job,AVG(sal),MAX(sal)
FROM emp
GROUP BY deptno ,job
查询各部门各岗位的部门名,岗位名,平均薪资和最高薪资
SELECT dname,job,AVG(sal),MAX(sal)
FROM dept LEFT JOIN emp ON dept.deptno = emp.deptno
GROUP BY dname,job
13.查询各个部门经理的最低薪水
SELECT deptno, MIN(sal)
FROM emp
WHERE job ='MANAGER'
GROUP BY deptno
14.查询所有员工的年薪,并按照年薪从高到低排序
SELECT empno,sal*12+IFNULL (comm,0) AS 年薪
FROM emp
ORDER BY 年薪 DESC
15.查询比30部门最高薪资的人薪资更高的所有员工信息
SELECT *
FROM emp
WHERE sal >
(SELECT MAX(sal) FROM emp WHERE deptno = '30')
查询比30部门所有人薪资都高员工信息
SELECT *
FROM emp
WHERE sal >
ALL (SELECT sal FROM emp WHERE deptno = '30')
16.查询每一个员工的姓名,职位以及领导的姓名
SELECT a.ename,a.job,b.ename AS 领导
FROM emp a LEFT JOIN emp b ON a.mgr = b.empno
SELECT a.ename,a.job,b.ename AS 领导
FROM emp a,emp b
WHERE a.mgr = b.empno
17.查询部门号为10号的部门名,员工名。以及薪资
SELECT dname,ename,sal
FROM emp,dept
WHERE emp.deptno = dept.deptno
AND emp.deptno = '10'
18.查询在销售部工作的员工姓名
SELECT ename
FROM emp
WHERE deptno =
(SELECT deptno FROM dept WHERE dname = 'SALES')
19.查询ford的详细信息,以及领导姓名
SELECT a.* ,b.ename 领导
FROM emp a,emp b
WHERE a.mgr = b.empno
AND a.ename = 'ford'
20.查询薪水高于公司的平均水平的所有员工名字以及薪水
SELECT ename ,sal
FROM emp
WHERE sal >
(SELECT AVG(sal) FROM emp )
1. 查询各个部门中工资最高的员工信息:名字、部门号、工资
SELECT ename,deptno,sal
FROM emp
WHERE (sal,deptno) IN
(SELECT MAX(sal),deptno FROM emp GROUP BY deptno)
2. 查询部门名称和这些部门的员工信息,同时列出那些没有员工的部门,并根据部门号排序
SELECT b.deptno,b.dname,a.*
FROM dept b LEFT JOIN emp a ON a.deptno = b.deptno
ORDER BY b.deptno ASC
3. 查询各个管理者属下员工的最低工资,其中最低工资不能低于800,没有管理者的员工不计在内
SELECT mgr 管理者,ename 员工,MIN(sal)
FROM emp
WHERE sal >= 800
AND mgr IS NOT NULL
GROUP BY mgr
4. 哪个部门的平均工资是最高的,列出部门号和平均工资
SELECT deptno ,AVG(sal) 平均
FROM emp
GROUP BY deptno
ORDER BY 平均 DESC LIMIT 0,1
思路:
1.求出所有部门的平均工资
2.从所有的平均工资找到最高的平均工资
3.百所有部门的平均工资和找出最高工资比较,相等的话取出部门ID和平均工资
SELECT b.deptno,b.sal
FROM (SELECT deptno,AVG(sal) AS sal FROM emp GROUP BY deptno) AS b
WHERE b.sal = (SELECT MAX(sal) FROM (SELECT AVG( sal) AS sal FROM emp GROUP BY deptno) AS a)