MySQL & Navicat(三)进阶练习_多表联查练习_GROUP BY练习_JOIN练习

文章目录

  • 一、题目
    • Ⅰ、表格
    • Ⅱ、列名代号
  • 二、答案解析

一、题目

MySQL & Navicat(三)进阶练习_多表联查练习_GROUP BY练习_JOIN练习_第1张图片

Ⅰ、表格

CREATE TABLE emp(
empno INT NOT NULL PRIMARY KEY,
ename VARCHAR(10),
job VARCHAR(10),
mgr INT,
hiredate DATETIME,
sal DOUBLE,
comm DOUBLE,
deptno INT,
FOREIGN KEY(deptno) REFERENCES dept(deptno)
);
INSERT INTO dept VALUES(10, 'Accounting', 'New York') ;
INSERT INTO dept VALUES(20, 'Research', 'Dallas') ;
INSERT INTO dept VALUES(30, 'Sales', 'Chicago') ;
INSERT INTO dept VALUES(40, 'Operations', 'Boston') ;
INSERT INTO dept VALUES(50, 'Admin', 'Washing') ;

INSERT INTO emp VALUES(7369, 'Smith', 'Clerk',7902, '1980-12-17',800,0,20) ;
INSERT INTO emp VALUES(7499, 'Allen', 'Salesman',7698,'1981-2-20',1600,300,30) ;
INSERT INTO emp VALUES(7844, 'Turner', 'Salesman',7499, '1981-9-8',1500,0,30) ;
INSERT INTO emp VALUES(7698, 'Tom', 'Manager',0, '1981-9-8',6100,600,40) ;
INSERT INTO emp VALUES(7876, 'Adams', 'Clerk',7900, '1987-5-23',1100,0,20) ;
INSERT INTO emp VALUES(7900, 'James', 'Clerk',7698, '1981-12-3',2400,0,30) ;
INSERT INTO emp VALUES(7902, 'Ford', 'Analyst',7698, '1981-12-3',3000,NULL,20) ;
INSERT INTO emp VALUES(7901, 'Kik', 'Clerk',7900, '1981-12-3',1900,0,30) ;

Ⅱ、列名代号

-- emp员工表
empno员工号/ename员工姓名/job工作/mgr上级人员编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)

-- dept部门表
deptno部门编号/dname部门名称/loc地点

二、答案解析


	01.列出至少有一个员工的所有部门
	
	//使用GROUP BY和count()聚合并统计		
		SELECT dname FROM dept 
		WHERE deptno IN
		(SELETE deptno FROM emp GROUP BY deptno HAVING count(deptno)>=1)
	
	02.列出薪金比“SMITH”多的所有员工
	
		SELECT ename FROM emp
		WHERE emp.sal > (SELECT sal FROM emp WHERE emp.ename= 'Smith')
	
	03.列出所有员工的姓名及其直接上级的姓名
	
	//先得到姓名和上级的表,再使原表RIGHT JOIN新表
	//(因为有一个是空值,不用RIGHT会少一行)
	//最后将上级的编号与员工编号对应		
		SELETE f.ename,emp.ename
		FROM emp RIGHT JOIN (SELECT ename,mgr FROM emp) AS f
		ON f.mgr = emp.empno
	
	04.列出受雇日期早于其直接上级的所有员工
	
		SELECT f.ename
		FROM emp RIGHT JOIN (SELECT ename,mgr,hiredate FROM emp) AS f
		ON f.mgr = emp.empno
		WHERE f.hiredate > emp.hiredate
	
	05.列出所有工作为“CLERK”的姓名及其部门名称

		SELECT f.ename,dept.dname
		FROM dept INNER JOIN (SELECT ename,deptno from emp WHERE job = 'Clerk') AS f
		ON f.deptno = dept.deptno
	
	06.列出在部门“SALES”工作的员工的姓名,假定不知道销售部的部门编号
	
		SELECT f.ename,dept.dname
		FROM dept INNER JOIN
		 (SELECT ename,deptno FROM emp WHERE deptno!=20 AND deptno!=10 AND deptno!=40 AND deptno!=50)  
		 AS f
		ON f.deptno = dept.deptno
	
	07.列出薪金高于公司平均薪金的所有员工
	
	//AVG()函数的使用	
		SELECT ename FROM emp
		WHERE sal > (SELECT AVG(sal) FROM emp )
	
	08.列出与“Kik”从事相同工作的所有员工
	
		SELECT ename FROM emp
		WHERE job = (SELECT job FROM emp WHERE ename= 'Kik')
		AND 
		ename != 'Kik'
	
	
	09.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金

		SELECT ename,sal FROM emp
		WHERE deptno !=30
		AND
		sal > (SELECT max(sal) FROM emp WHERE deptno = 30)
	
	
	10.列出在每个部门工作的员工数量、平均工资和平均服务期限

	//count统计数量,avg得到平均
	//COALESCE(int,int,...)函数会返回第一个非空数
	//因为有空值不能直接相加
	//TIMESTAMPDIFF(type,oldtime,newtime)函数得到时间间隔.type可选day,week,month...
	//SYSDATE得到运行时的时间
		SELECT dname,f.counter,f.average,f.time
		FROM dept INNER JOIN 
		(SELECT deptno,count(deptno) AS counter,avg(sal+COALESCE(comm,0)) AS average,avg((TIMESTAMPDIFF(day,hiredate,SYSDATE()))) 
		AS time FROM emp GROUP BY deptno)AS f
		ON dept.deptno = f.deptno
		
	11.列出所有员工的姓名、部门名称和工资.
	
	//思路同上 
		SELECT f.ename,dept.dname,(f.sal+COALESCE(f.comm,0))
		FROM dept INNER JOIN (SELECT ename,deptno,sal,comm FROM emp) AS f
		ON f.deptno = dept.deptno
		

欢迎在评论区留言
感谢浏览

你可能感兴趣的:(#,MySQL)