♥️Author : Xiao Liu at Station C
♥️Personal homepage: Xiao Liu's homepage
♥️ Share cloud computing network operation and maintenance classroom notes every day, hard work may not be rewarded, but there will be gains, come on! Work hard together for a better life!
♥️The tree is thousands of feet tall, and the fallen leaves return to the roots. Life is not easy, but the true love in the world
foreword
When we explained the SQL statement before, we explained the DQL statement, that is, the data query statement, but the queries explained before are all single-table queries, and what we want to learn in this chapter is the multi-table query operation. The previous chapter mentioned 5.2 this chapter continue
Table of contents
1). Query employee's name, age, position, department information (implicit inner connection)
3). Query the department ID and department name of the employee
5). Query the salary level of all employees
6). Query the information and salary level of all employees in the "R&D Department"
7). Query the average salary of employees in the "R&D department"
8). Query the employee information whose salary is higher than "Extinction".
9). Query employee information with higher than average salary
11). Query all department information, and count the number of employees in the department
MySQL
MySQL is a relational database management system, developed by the Swedish company MySQL AB, which is a product of Oracle. MySQL is one of the most popular relational database management systems. In terms of WEB applications, MySQL is one of the best RDBMS (Relational Database Management System, relational database management system) application software.
MySQL is a relational database management system. Relational databases store data in different tables instead of putting all the data in one big warehouse, which increases speed and improves flexibility.
The SQL language used by MySQL is the most commonly used standardized language for accessing databases. MySQL software adopts a dual authorization policy, which is divided into community edition and commercial edition. Due to its small size, fast speed, low overall cost of ownership, especially the open source feature, MySQL is generally chosen as the website for the development of small, medium and large websites. database.
6 subqueries
6.1 Overview
1). Concept
SELECT * FROM t1 WHERE column1 = ( SELECT column1 FROM t2 );
2). Classification
6.2 Scalar Subqueries
case :
select id from dept where name = '销售部';
select * from emp where dept_id = (select id from dept where name = '销售部');
select entrydate from emp where name = '方东白';
select * from emp where entrydate > (select entrydate from emp where name = '方东
白');
6.3 Column subquery
![](https://img-blog.csdnimg.cn/a20a3bee91944d6a95c68b10d2917505.png)
case :
select id from dept where name = '销售部' or name = '市场部';
select * from emp where dept_id in (select id from dept where name = '销售部' or
name = '市场部');
select salary from emp where dept_id = (select id from dept where name = '研发部');
select * from emp where salary > any ( select salary from emp where dept_id =
(select id from dept where name = '研发部') );
6.4 Row Subqueries
case:
select salary, managerid from emp where name = '张无忌';
select * from emp where (salary,managerid) = (select salary, managerid from emp
where name = '张无忌');
6.5 Table subqueries
select job, salary from emp where name = '鹿杖客' or name = '宋远桥'; 1
select * from emp where (job,salary) in ( select job, salary from emp where name =
'鹿杖客' or name = '宋远桥' );
select * from emp where entrydate > '2006-01-01';
select e.*, d.* from (select * from emp where entrydate > '2006-01-01') e left
join dept d on e.dept_id = d.id ;
5.7 Multi-table query case
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);
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;
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;
3). Query the department ID and department name of the employee
select distinct d.id , d.name from emp e , dept d where e.dept_id = d.id;
4). Query all employees who are 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
come ( outer join )
select e.*, d.name from emp e left join dept d on e.dept_id = d.id where e.age >
40 ;
5). Query the salary level of all employees
-- 方式一
select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary >=
s.losal and e.salary <= s.hisal;
-- 方式二
select e.* , s.grade , s.losal, s.hisal from emp e , salgrade s where e.salary
between s.losal and s.hisal;
6). Query the information and salary levels of all employees in the " R&D Department "
select e.* , s.grade from emp e , dept d , salgrade s where e.dept_id = d.id and (
e.salary between s.losal and s.hisal ) and d.name = '研发部';
7). Query the average salary of employees in the " R&D Department "
select avg(e.salary) from emp e, dept d where e.dept_id = d.id and d.name = '研发
部';
8). Query information about employees whose salary is higher than " Extinction " .
select salary from emp where name = '灭绝';
select * from emp where salary > ( select salary from emp where name = '灭绝' );
9). Query information about employees whose salary is higher than the average
select avg(salary) from emp;
select * from emp where salary > ( select avg(salary) from emp ); 1
10). Query the information of employees whose salary is lower than the average salary of the department
select avg(e1.salary) from emp e1 where e1.dept_id = 1;
select avg(e1.salary) from emp e1 where e1.dept_id = 2;
select * from emp e2 where e2.salary < ( select avg(e1.salary) from emp e1 where
e1.dept_id = e2.dept_id );
11). Query all department information and count the number of employees in the department
select d.id, d.name , ( select count(*) from emp e where e.dept_id = d.id ) '人数'
from dept d;
12). Query the course selection of all students , display the student name , student number , and course name
select s.name , s.no , c.name from student s , student_course sc , course c where
s.id = sc.studentid and sc.courseid = c.id ;
Remarks : There may be many ways to realize the above requirements , and there are also many SQL writing methods. As long as our needs can be met, we can query the records that meet the conditions.
♥️Following is the driving force for my creation
♥️Like, is the greatest recognition for me
♥️This is Xiaoliu, I am inspiring to do every article well, thank you everyone