当前位置:网站首页>MySQL multi table query introduction classic case
MySQL multi table query introduction classic case
2022-07-26 06:09:00 【I'm grey wolf】
-- Departmental table
CREATE TABLE dept (
did INT PRIMARY KEY, -- department id
dname VARCHAR ( 50 ), -- Department name
loc VARCHAR ( 50 ) -- Department Address
)
-- Job list
CREATE TABLE job (
jid INT PRIMARY KEY, -- position id
jname VARCHAR ( 20 ), -- Job title
description VARCHAR ( 50 ) -- Job description
);
-- The employee table
CREATE TABLE emp (
eid INT PRIMARY KEY,-- staff id
ename VARCHAR ( 50 ),-- Employee name
job_id INT,-- position id
mgrid INT,-- Superior leaders id
joindate DATE,-- Date of entry
salary DECIMAL ( 7, 2 ),-- Wages
bonus DECIMAL ( 7, 2 ),-- Bonus
dept_id INT,-- Department number
CONSTRAINT emp_job_id_ref_job_jid FOREIGN KEY ( job_id ) REFERENCES job ( jid ),
CONSTRAINT emp_dept_id_ref_dept_did FOREIGN KEY ( dept_id ) REFERENCES dept ( did )
);
-- Pay scale
CREATE TABLE salarygrade (
grade INT PRIMARY KEY, -- Level
losalary INT, -- minimum wage
hisalary INT -- Maximum wage
);
-- add to 4 Departments
INSERT INTO dept ( did, dname, loc )
VALUES
( 10, ' Teaching and Research Department ', ' Beijing ' ),
( 20, ' Department of science and Engineering ', ' Shanghai ' ),
( 30, ' The sales department ', ' Guangzhou ' ),
( 40, ' Finance Department ', ' Shenzhen ' );
-- add to 4 Positions
INSERT INTO job ( jid, jname, description )
VALUES
( 1, ' Chairman of the board of directors ', ' Managing the whole company , order ' ),
( 2, ' The manager ', ' Management staff ' ),
( 3, ' Salesperson ', ' Sell products to customers ' ),
( 4, ' Clerk ', ' Using office software ' );
-- Add employees
INSERT INTO emp ( eid, ename, job_id, mgrid, joindate, salary, bonus, dept_id )
VALUES
( 1001, ' The Monkey King ', 4, 1004, '2000-12-17', '8000.00', NULL, 20 ),
( 1002, ' Jun-yi lu ', 3, 1006, '2001-02-20', '16000.00', '3000.00', 30 ),
( 1003, ' Lin Chong ', 3, 1006, '2001-02-22', '12500.00', '5000.00', 30 ),
( 1004, ' Tang's monk ', 2, 1009, '2001-04-02', '29750.00', NULL, 20 ),
( 1005, ' Li Kui ', 4, 1006, '2001-09-28', '12500.00', '14000.00', 30 ),
( 1006, ' Song Jiang ', 2, 1009, '2001-05-01', '28500.00', NULL, 30 ),
( 1007, ' Liu bei ', 2, 1009, '2001-09-01', '24500.00', NULL, 10 ),
( 1008, ' Pig eight quit ', 4, 1004, '2007-04-19', '30000.00', NULL, 20 ),
( 1009, ' Luo Guanzhong ', 1, NULL, '2001-11-17', '50000.00', NULL, 10 ),
( 1010, ' Wu Yong ', 3, 1006, '2001-09-08', '15000.00', '0.00', 30 ),
( 1011, ' Monk sha ', 4, 1004, '2007-05-23', '11000.00', NULL, 20 ),
( 1012, ' Li Kui ', 4, 1006, '2001-12-03', '9500.00', NULL, 30 ),
( 1013, ' Small white dragon ', 4, 1004, '2001-12-03', '30000.00', NULL, 20 ),
( 1014, ' Guan yu ', 4, 1007, '2002-01-23', '13000.00', NULL, 10 );
-- add to 5 A salary scale
INSERT INTO salarygrade ( grade, losalary, hisalary )
VALUES
( 1, 7000, 12000 ),
( 2, 12010, 14000 ),
( 3, 14010, 20000 ),
( 4, 20010, 30000 ),
( 5, 30010, 99990 );
-- 1. Query all employee information . Enquiry of employee number , Employee name , Wages , Job title , Job description
-- Involving tables :emp,job
SELECT e.eid,e.ename,e.salary,j.jname,j.description
FROM emp e,job j
WHERE e.job_id = j.jid;
SELECT e.eid,e.ename,e.salary,j.jname,j.description
FROM emp e
JOIN job j
ON e.job_id = j.jid;
-- 2. Enquiry of employee number , Employee name , Wages , Job title , Job description , Department name , Department position
-- Involving tables :emp,job,dept
SELECT e.eid,e.ename,e.salary,j.jname,j.description,d.did,d.dname
FROM emp e,job j,dept d
WHERE e.job_id = j.jid and e.dept_id = d.did;
SELECT e.eid,e.ename,e.salary,j.jname,j.description,d.did,d.dname
FROM emp e
JOIN job j
ON e.job_id = j.jid
JOIN dept d
ON e.dept_id = d.did;
-- 3. Check the name of the employee , Wages , Pay scale
-- Involving tables :emp,salarygrade
SELECT e.ename,e.salary,s.grade
FROM emp e,salarygrade s
WHERE salary BETWEEN s.losalary AND s.hisalary;
SELECT e.ename,e.salary,s.grade
FROM emp e
JOIN salarygrade s
ON salary > s.losalary
AND salary < s.hisalary;
-- 4. Check the name of the employee , Wages , Job title , Job description , Department name , Department position , Pay scale
-- Involving tables :emp,job,dept,salarygrade
SELECT e.eid,e.ename,e.salary,j.jname,j.description,d.did,d.dname,s.grade
FROM emp e,job j,dept d,salarygrade s
WHERE e.job_id = j.jid
AND e.dept_id = d.did
AND salary BETWEEN s.losalary AND s.hisalary;
SELECT e.eid,e.ename,e.salary,j.jname,j.description,d.did,d.dname,s.grade
FROM emp e
JOIN job j
ON e.job_id = j.jid
JOIN dept d
ON e.dept_id = d.did
JOIN salarygrade s
ON salary BETWEEN s.losalary AND s.hisalary;
-- 5. Find out the department number 、 Department name 、 Department position 、 Number of departments
-- Involving tables :emp,dept
SELECT *
FROM dept;
SELECT dept_id, count(*)
FROM emp
GROUP BY dept_id;
SELECT d.did,d.dname,d.loc,t1.count
FROM dept d,(SELECT dept_id,count(*) count
FROM emp
GROUP BY dept_id
) t1
WHERE d.did = t1.dept_id;
SELECT d.did,d.dname,d.loc,t1.count
FROM dept d
LEFT JOIN (SELECT dept_id,count(*) count
FROM emp
GROUP BY dept_id
) t1
ON d.did = t1.dept_id;边栏推荐
- Modifiers should be declared in the correct order 修饰符应按正确的顺序声明
- Workflow activiti5.13 learning notes (I)
- WebAPI整理
- em和rem
- 金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
- 1.12 Web开发基础
- Mysql45 talking about logging system: how does an SQL UPDATE statement execute?
- 2022年下半年系统集成项目管理工程师(软考中级)报名条件
- Registration conditions for system integration project management engineer (intermediate level of soft exam) in the second half of 2022
- Understanding the mathematical essence of machine learning
猜你喜欢

Convolutional neural network (IV) - special applications: face recognition and neural style transformation

1.12 basis of Web Development

二叉树的前中后序遍历——本质(每个节点都是“根”节点)

Acquisition of bidding information
![[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism](/img/19/32206eb6490c2a5a7a8e746b5003c1.png)
[(SV & UVM) knowledge points encountered in written interview] ~ phase mechanism

"Recursive processing of subproblems" -- judging whether two trees are the same tree -- and the subtree of another tree

金仓数据库 KingbaseES SQL 语言参考手册 (10. 查询和子查询)

Implementation of PHP multitask second timer

Easycvr video square channel display and video access full screen display style problem repair

金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
随机推荐
Understanding the mathematical essence of machine learning
Matlab vector and matrix
Intelligent fire protection application based on fire GIS system
Huawei cloud koomessage is a new marketing weapon in the hot public beta
Print linked list in reverse order
【(SV && UVM) 笔试面试遇到的知识点】~ phase机制
Talking about the practice of software defect management
Database SQL language practice
【Oracle SQL】计算同比与环比(列转行进行偏移)
【Day_06 0423】不要二
Sequential search, half search, block search~
逆序打印链表
The time complexity of two recursive entries in a recursive function
Sequential action localization | fine grained temporal contrast learning for weak supervised temporal action localization (CVPR 2022)
em和rem
Widget is everything, widget introduction
2022年下半年系统集成项目管理工程师(软考中级)报名条件
C language explanation series - comprehensive exercises, guessing numbers games
Balanced binary tree (AVL)~
Amd zen4 game God u reached 208mb cache within this year, which is unprecedented