当前位置:网站首页>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;边栏推荐
- Blurring of unity pixel painting
- Amd zen4 game God u reached 208mb cache within this year, which is unprecedented
- Understanding the mathematical essence of machine learning
- Calling mode and execution sequence of JS
- Using dynamic libraries in VS
- Leetcode:741. picking cherries
- 金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
- [the most complete and detailed] ten thousand words explanation: activiti workflow engine
- 【2023杰理科技提前批笔试题】~ 题目及参考答案
- Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
猜你喜欢

金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)

Flex layout

Solution to slow download speed of vagrant

Using dynamic libraries in VS

The time complexity of two recursive entries in a recursive function

Interview difficulties: difficulties in implementing distributed session, this is enough!

2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers

【Day_06 0423】不要二

Database SQL language practice

基于消防GIS系统的智慧消防应用
随机推荐
ament_ Cmake generates the ros2 library and links it
Ganglia安装部署流程
VRRP protocol and experimental configuration
英语句式参考纯享版 - 定语从句
CV (1)- Introduction
flex布局
Recursive processing - subproblem
招标信息获取
Niuke network: TOPK problem of additive sum between two ordinal groups
JS的调用方式与执行顺序
What is spark serialization for?
递归函数中 有两个递归入口的时间复杂度
递归处理——子问题
【Day_04 0421】计算糖果
Mysql45 talking about infrastructure: how is an SQL query executed?
【Day_07 0425】合法括号序列判断
Talking about the practice of software defect management
Kingbasees SQL language reference manual of Jincang database (8. Functions (XI))
K. Link with Bracket Sequence I dp
Taobao pinduoduo Tiktok 1688 Suning taote jd.com and other keyword search commodity API interfaces (keyword search commodity API interface, keyword search commodity list interface, classification ID s