当前位置:网站首页>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;
边栏推荐
- QT 使用FFmpeg4将argb的Qimage转换成YUV422P
- Easyexcel complex data export
- linux下清理系统缓存并释放内存
- Resttemplate remote call tool class
- [ecological partner] Kunpeng system engineer training
- Object memory layout
- Medium pen test questions: flip the string, such as ABCD, print out DCBA
- String type conversion BigDecimal, date type
- 微信开放平台扫码登录[通俗易懂]
- CIO's discussion and Analysis on the definition of high-performance it team
猜你喜欢
Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
内部字段分隔符
高攀不起的希尔排序,直接插入排序
Slope compensation
比较版本号[双指针截取自己想要的字串]
首席信息官对高绩效IT团队定义的探讨和分析
Flume interview questions
Easyexcel complex data export
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
三翼鸟两周年:羽翼渐丰,腾飞指日可待
随机推荐
【生态伙伴】鲲鹏系统工程师培训
效率提升 - 鼓捣个性化容器开发环境
小红书Scheme跳转到指定页面
linux下清理系统缓存并释放内存
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]
Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
Mask wearing detection method based on yolov5
详解Volatile关键字
Wechat open platform scanning code login [easy to understand]
Make a three digit number of all daffodils "recommended collection"
指标陷阱:IT领导者易犯的七个KPI错误
【日常训练】66. 加一
3DE 资源没东西或不对
【图像分割】2021-SegFormer NeurIPS
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
The fixed assets management subsystem reports are divided into what categories and which accounts are included
Use of vscode
基准环路增益与相位裕度的测量
对象内存布局
MySQL中对于事务的理解