Table of contents
1.Multiple table relationships
Before doing the case exercise, you need to add a table
create table salgrade(
grade int,
losal int, -- 对应等级的最低薪资
hisal int -- 对应等级的最高薪资
) comment '薪资等级表';
insert into salgrade values (1,0,3000);
insert into salgrade values (2,3001,5000);
insert into salgrade values (3,5001,8000);
insert into salgrade values (4,8001,10000);
insert into salgrade values (5,10001,15000);
insert into salgrade values (6,15001,20000);
insert into salgrade values (7,20001,25000);
insert into salgrade values (8,25001,30000);
Before practicing, we take out all three tables:
Exercise 1
- Query employee's name, age, position, department information (implicit inner connection)
- Query the name, age, position, and department information of employees younger than 30 years old (explicit inner connection)
- Query the department ID and department name with employees
- Query all employees older than 40 years old and the name of the department they belong to; if the employee is not assigned a department, it also needs to be displayed
- Query the salary grade of all employees
1-1
-- Query employee's name, age, position, department information (implicit inner connection)
select e.name '姓名',e.age '年龄',e.job '职位',d.name '部门信息'
from emp e,dept d where e.dept_id = d.id;
search result:
1-2
-- Query the name, age, position, and department information of employees younger than 30 years old (explicit inner connection)
select e.name '姓名',e.age '年龄',e.job '职位',d.name '部门信息'
from emp e inner join dept d on e.dept_id = d.id where e.age < 30;
search result:
1-3
-- Query department IDs and department names with employees
-- Key points: self-connection, deduplication of keywords
select distinct d.id,d.name from emp e,dept d where e.dept_id = d.id;
search result:
1-4
-- Query all employees older than 40 years old and their department names; if employees are not assigned departments, they also need to be displayed
-- Key points: left outer join
select e.name '姓名',d.name '部门名称'
from emp e left join dept d on e.dept_id = d.id where age > 40;
search result:
1-5
-- Query the salary grade of all employees
-- Key points: The table structure is emp and salgrade, clarify the connection conditions of the two tables
select e.name '姓名',s.grade '工资等级'
from emp e left join salgrade s on e.salary >= s.losal and e.salary <= s.hisal;
-- 另一种写法
select e.name '姓名',s.grade '工资等级'
from emp e left join salgrade s on e.salary between s.losal and s.hisal;
search result:
Exercise 2
- Query the information and salary levels of all employees in the "R&D Department"
- Query the average salary of employees in the "R&D Department"
- Query information about employees whose salary is higher than "extinction".
- Query information about employees with higher than average salary
- Query information about employees whose salary is lower than the average salary of the department
- Query all department information and count the number of employees in the department
- Query the course selection status of all students, displaying the student name, student number, and course name
2-1
-- Query the information and salary levels of all employees in the "R&D Department"
-- Key points: Understand the connection conditions and query conditions
--Connection conditions:
(e.dept_id = d.id)
(e.salary between losal and hisal)
-- Query conditions (d.name = 'R&D Department')
select e.*,s.grade
from emp e,
dept d,
salgrade s
where (e.dept_id = d.id)
and (e.salary between losal and hisal)
and (d.name = '研发部');
search result:
2-2
-- Query the average salary of employees in the "R&D Department"
-- Key points: function avg()
select avg(e.salary)
from emp e,
dept d
where e.dept_id = d.id
and d.name = '研发部';
search result:
2-3
-- Query employee information whose salary is higher than "extinction".
select *
from emp
where salary > (select salary from emp where name = '灭绝');
search result:
2-4
-- Query information about employees with higher than average salary
select *
from emp
where salary > (select avg(salary) from emp);
search result:
2-5
-- Query information about employees whose salary is lower than the average salary of the department
-- Key points: Find out the average salary of each department
select *, (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id) '所在部门平均工资'
from emp e2
where e2.salary < (select avg(e1.salary) from emp e1 where e1.dept_id = e2.dept_id);
search result:
2-6
-- Query all department information and count the number of employees in the department
First query the department information of all departments:
select * from dept;
Then count the number of employees in a single department:
select count(*) from emp where dept_id = 1;
Put it all together:
select d.*, (select count(*) from emp e where e.dept_id = d.id) '员工人数'
from dept d;
search result:
2-7
-- Query the course selection status of all students and display the student name, student number, and course name
Involving three other tables, it is a many-to-many relationship.
You can query it by clarifying the connection relationship between the three tables.
select s.name '学生名称', s.no '学号', c.name '课程名称'
from student s,
course c,
student_course sc
where (s.id = sc.studentid)
and (c.id = sc.courseid);
search result:
Summarize
1.Multiple table relationships
One-to-many: Set a foreign key on the many side and associate it with the primary key of the one side
Many-to-many: Create an intermediate table that contains two foreign keys and associates the primary keys of the two tables.
One-to-one: used to split the table structure, set a foreign key (UNIQUE) on either side, and associate the primary key of the other side
2.Multiple table query
self-connection
Implicit: SELECT...FROM table A, table B WHERE condition...
Explicit: SELECT...FROM table A INNER JOIN table B ON condition...
Outer join:
Left outer: SELECT...FROM table A LEFT JOIN table B ON condition...
Right outer: SELECT...FROM table A RIGHT JOIN table B ON condition...
Self-join: SELECT ... FROM table A alias 1, table A alias 2 WHERE condition...
Subquery: scalar subquery, column subquery, row subquery, table subquery
end
Learn from: Dark Horse Programmer - MySQL Database Course