当前位置:网站首页>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;边栏推荐
- Solve vagrant's error b:48:in `join ': incompatible character encodings: GBK and UTF-8 (encoding:: Compatib
- Can you make a JS to get the verification code?
- Print linked list in reverse order
- 英语句式参考纯享版 - 定语从句
- 【Day_05 0422】连续最大和
- Convolutional neural network (II) - deep convolutional network: case study
- H. Take the Elevator 贪心
- Niuke network: TOPK problem of additive sum between two ordinal groups
- Mobile web
- Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!
猜你喜欢

Leetcode:336. palindrome pair

Age is a hard threshold! 42 years old, Tencent level 13, master of 985, looking for a job for three months, no company actually accepted!

H. Take the Elevator 贪心

Convolutional neural network (III) - target detection

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

Modifiers should be declared in the correct order

H. Take the elevator greedy

时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)

Amd zen4 game God u reached 208mb cache within this year, which is unprecedented

A company installs monitoring for each station: write code only to see employees?
随机推荐
YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
漫谈软件缺陷管理的实践
Optical quantum milestone: 3854 variable problems solved in 6 minutes
Interview questions for software testing is a collection of interview questions for senior test engineers, which is exclusive to the whole network
Convolutional neural network (III) - target detection
Properties of binary tree~
时序动作定位 | 用于弱监督时态动作定位的细粒度时态对比学习(CVPR 2022)
Code Runner for VS Code,下载量突破 4000 万!支持超过50种语言
数据库sql语言实战
【Day02_0419】C语言选择题
What is spark serialization for?
知识沉淀一:架构师是做什么?解决了什么问题
[free and easy to use] holiday query interface
招标信息获取
ETCD数据库源码分析——Cluster membership changes日志
Distributed | practice: smoothly migrate business from MYCAT to dble
Solutions to the failure of copy and paste shortcut keys
程序员如何改善精神内耗?
Matlab vector and matrix
redis 哨兵集群搭建