当前位置:网站首页>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;
边栏推荐
- Which securities company should we choose to open an account for flush stock? Is it safe to open an account with a mobile phone?
- Chapter 9 Yunji datacanvas company has been ranked top 3 in China's machine learning platform market
- 【MySQL】索引的分类
- String type conversion BigDecimal, date type
- 记录一次spark on yarn 任务报错 Operation category READ is not supported in state standby
- Medium pen test questions: flip the string, such as ABCD, print out DCBA
- Delete AWS bound credit card account
- 详解Kubernetes网络模型
- 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]
- leetcode - 287. 寻找重复数
猜你喜欢

Learn MySQL from scratch - database and data table operations

企业架构与项目管理的关联和区别

Dark horse programmer - software testing - stage 06 2-linux and database-01-08 Chapter 1 - description of the content of the Linux operating system stage, description of the basic format and common fo
![[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial](/img/ac/655fd534ef7ab9d991d8fe1c884853.png)
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial

对象内存布局

内部字段分隔符

多种智能指针

FFMpeg学习笔记

# CutefishOS系统~

详解ThreadLocal
随机推荐
# CutefishOS系统~
leetcode - 287. 寻找重复数
内部字段分隔符
Wechat open platform scanning code login [easy to understand]
删除AWS绑定的信用卡账户
MySQL的存储过程
【MySQL】数据库优化方法
【JetCache】JetCache的使用方法与步骤
Mysql——》MyISAM存储引擎的索引
flink sql 命令行 连接 yarn
FFMpeg学习笔记
GaussDB(DWS)主动预防排查
EasyExcel 复杂数据导出
详解Kubernetes网络模型
2020-ViT ICLR
Relationship and difference between enterprise architecture and project management
Redis配置与优化
Medium pen test questions: flip the string, such as ABCD, print out DCBA
园区全光技术选型-中篇
Clean up system cache and free memory under Linux