当前位置:网站首页>MySQL view exercise
MySQL view exercise
2022-07-01 22:39:00 【Fire eye Dragon】
Data preparation
CREATE TABLE dept (
deptno int NOT NULL,
dname varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
loc varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`deptno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO dept VALUES (10, ' Teaching and Research Department ', ' Beijing ');
INSERT INTO dept VALUES (20, ' Department of science and Engineering ', ' Shanghai ');
INSERT INTO dept VALUES (30, ' The sales department ', ' Guangzhou ');
INSERT INTO dept VALUES (40, ' Finance Department ', ' wuhan ');
CREATE TABLE emp (
empno int NOT NULL,
ename varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
job varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
mgr int NULL DEFAULT NULL,
hiredate date NULL DEFAULT NULL,
sal decimal(7, 2) NULL DEFAULT NULL,
COMM decimal(7, 2) NULL DEFAULT NULL,
deptno int NULL DEFAULT NULL,
PRIMARY KEY (empno) USING BTREE,
INDEX fk_emp(mgr) USING BTREE,
CONSTRAINT fk_emp FOREIGN KEY (mgr) REFERENCES emp (empno) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO emp VALUES (1001, ' Gan Ning ', ' Clerk ', 1013, '2000-12-17', 8000.00, NULL, 20);
INSERT INTO emp VALUES (1002, ' Daisy ', ' Salesperson ', 1006, '2001-02-20', 16000.00, 3000.00, 30);
INSERT INTO emp VALUES (1003, ' Yin Tianzheng ', ' Salesperson ', 1006, '2001-02-22', 12500.00, 5000.00, 30);
INSERT INTO emp VALUES (1004, ' Liu bei ', ' The manager ', 1009, '2001-04-02', 29750.00, NULL, 20);
INSERT INTO emp VALUES (1005, ' Thank sun ', ' Salesperson ', 1006, '2001-09-28', 12500.00, 14000.00, 30);
INSERT INTO emp VALUES (1006, ' Guan yu ', ' The manager ', 1009, '2001-05-01', 28500.00, NULL, 30);
INSERT INTO emp VALUES (1007, ' Zhang Fei ', ' The manager ', 1009, '2001-09-01', 24500.00, NULL, 10);
INSERT INTO emp VALUES (1008, ' Zhugeliang ', ' analysts ', 1004, '2007-04-19', 30000.00, NULL, 20);
INSERT INTO emp VALUES (1009, ' Zeng a Niu ', ' Chairman of the board of directors ', NULL, '2001-11-17', 50000.00, NULL, 10);
INSERT INTO emp VALUES (1010, ' Xiangr ', ' Salesperson ', 1006, '2001-09-08', 15000.00, 0.00, 30);
INSERT INTO emp VALUES (1011, ' Zhou Tai ', ' Clerk ', 1008, '2007-05-23', 11000.00, NULL, 20);
INSERT INTO emp VALUES (1012, ' Cheng pu ', ' Clerk ', 1006, '2001-12-03', 9500.00, NULL, 30);
INSERT INTO emp VALUES (1013, ' Pang Tong ', ' analysts ', 1004, '2001-12-03', 30000.00, NULL, 20);
INSERT INTO emp VALUES (1014, ' Huang Gai ', ' Clerk ', 1007, '2002-01-23', 13000.00, NULL, 10);
CREATE TABLE salgrade (
grade int NOT NULL,
losal int NULL DEFAULT NULL,
hisal int NULL DEFAULT NULL,
PRIMARY KEY (grade) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO salgrade VALUES (1, 7000, 12000);
INSERT INTO salgrade VALUES (2, 12010, 14000);
INSERT INTO salgrade VALUES (3, 14010, 20000);
INSERT INTO salgrade VALUES (4, 20010, 30000);
INSERT INTO salgrade VALUES (5, 30010, 99990);
Exercises
1: Query the name of the Department with the highest average salary in the Department
2: Query the Department name of the employee whose salary is higher than that of the leader 、 Employee name 、 Employee leader number
3: Query salary grade is 4 level ,2000 The place of employment after 2010 is the employee number in Shanghai 、 Name and salary , And find out the information of employees with the top three salaries
The answer to the exercise
-- 1: Query the name of the Department with the highest average salary in the Department
SELECT
a.deptno,
a.dname,
a.loc,
avg_sal
FROM
dept a,
(
SELECT
*
FROM
( SELECT *, RANK ( ) ) OVER ( ORDER BY avg_sal DESC ) rn
FROM
( SELECT deptno, AVG( deptno ) avg_sal FROM emp GROUP BY deptno ) t
) tt
WHERE
rn = 1
) ttt
WHERE
a.deptno = ttt.deptno
-- Method 2
CREATE VIEW test_view1 AS SELECT deptno, AVG( deptno ) avg_sal FROM emp GROUP BY deptno
CREATE VIEW test_view2 AS SELECT *, RANK ( ) ) OVER ( ORDER BY avg_sal DESC) rn FROM test_view1
CREATE VIEW test_view3 AS SELECT * FROM test_view2 tt WHERE rn=1
SELECT
a.deptno,
a.dname,
a.loc,
avg_sal
FROM
dept a,
test_view3 ttt
WHERE
a.deptno = ttt.deptno
-- 2: Query the Department name of the employee whose salary is higher than that of the leader 、 Employee name 、 Employee leader number
-- 2.1 Query the department number of the employee whose salary is higher than that of the leader
CREATE view test_view4
AS
SELECT
a.ename enane,
a.sal sal,
b.ename mgrname,
b.sal msal,
a.deptno
FROM
emp a,
emp b
WHERE
a.mgr = b.empno
AND a.sal > b.sal;
-- 2.2 Query the department number and Department list found in the first step with a linked list
SELECT
*
FROM
dept a
JOIN test_view4 b ON a.deptno = b.deptno;
-- 3: Query salary grade is 4 level ,2000 The place of employment after 2010 is the employee number in Shanghai 、 Name and salary , And find out the information of employees with the top three salaries
-- 3.1 demand 1: Query salary grade is 4 level ,2000 The place of employment after 2010 is the employee number in Shanghai 、 Name and salary
CREATE VIEW test_view5 AS SELECT
a.deptno,
a.dname,
a.loc,
b.empno,
b.ename,
b.sal
FROM
dept a
JOIN emp b ON a.deptno = b.deptno
AND YEAR ( hiredate ) > '2000'
AND a.loc = ' Shanghai '
JOIN salgrade c ON c.grade = 4
AND ( b.sal BETWEEN c.losal AND c.hisal );
SELECT * FROM
(
SELECT *,RANK() OVER(ORDER BY sal DESC)rn
FROM
test_view5
)t
WHERE rn<=3;
边栏推荐
猜你喜欢

The fixed assets management subsystem reports are divided into what categories and which accounts are included

PyTorch磨刀篇|argmax和argmin函数

黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,

Configure filter

详解Kubernetes网络模型

91.(cesium篇)cesium火箭发射模拟

The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner

QT版本华睿相机的Demo程序实现

EasyExcel 复杂数据导出

Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
随机推荐
Why must digital transformation strategies include continuous testing?
牛客月赛-分组求对数和
详解ThreadLocal
【QT小作】封装一个简单的线程管理类
2020-ViT ICLR
Slope compensation
【JetCache】JetCache的使用方法与步骤
从零开始学 MySQL —数据库和数据表操作
恶意软件反向关闭EDR的原理、测试和反制思考
Mysql——》MyISAM存储引擎的索引
高攀不起的希尔排序,直接插入排序
效率提升 - 鼓捣个性化容器开发环境
Separate the letters and numbers in the string so that the letters come first and the array comes last
Awoo's favorite problem (priority queue)
详解LockSupport的使用
【MySQL】索引的创建、查看和删除
Interview question: what is the difference between MySQL's Union all and union, and how many join methods MySQL has (Alibaba interview question) [easy to understand]
MySQL之MHA高可用配置及故障切换
keras训练的H5模型转tflite
H5 model trained by keras to tflite