foreword
Today we are going to learn the next module of multi-table query—subquery. Subquery includes scalar subquery, column subquery, row subquery, and table subquery. Let’s start learning without much talk.
Table of contents
2.1 According to different subquery results, it can be divided into:
2.2 According to the position of the subquery, it is divided into:
1. Sub query
1. The concept of subquery
The use of select statements nested in SQL statements is called nested queries , also known as subqueries.
2. Subquery syntax format
select * from 表1 where column1=(select column1 from 表2);
The statement outside the subquery can be any one of insert/update/delete/select .
2.1 According to different subquery results, it can be divided into:
Scalar subquery: subquery results as a single value
Column subquery: the result of the subquery is a column
Row subquery: the subquery result is one row
Table subquery: the result of the subquery is multiple rows and multiple columns
2.2 According to the position of the subquery, it is divided into:
select id from dept where name='市场部';
after where
after from
after select
2.3 Scalar Subquery Concept
The result returned by a subquery is a single value (number, string, date, etc.). In its simplest form, such a subquery is called a scalar subquery. Commonly used operators: =, <> ( not equal to ), >, >=, <, <=.
2.4 Scalar Subquery Exercises
2.4.1 Query the information of all employees in the "Marketing Department" (using the table structure of the previous period)
a. Query the "Marketing Department" department id
select id from dept where name='市场部';
b. Query employee information according to the market department id
select * from emp where dept_id=4;
ab two into one
select * from emp where dept_id=(select id from dept where name='市场部');
2.4.2 Query Du Fu's employee information after joining
a. Query Du Fu's entry date
select entrydate from emp where name='杜甫';
b. Query the information of employees who joined after the specified date
select * from emp where entrydate>'0120-01-01';
two in one
select * from emp where entrydate>(select entrydate from emp where name='杜甫');
2.3 Column subquery concept
The result returned by a subquery is a column (can be multiple rows), and this subquery is called a column subquery.
Commonly used operators: IN , NOTIN , ANY , SOME , ALL
2.4 Column query practice
2.4.1 Query the information of all employees in the R&D department and the marketing department
a. Query the department IDs of the R&D department and the marketing department
select id from dept where name='市场部'or name='研发部';
b. Query employee information according to department id
select * from emp where dept_idin(2,3);
two in one
select *
from emp
where dept_id in (select id from dept where name = '市场部' or name = '研发部');
2.4.2 Query information about employees whose salary is higher than that of everyone in the marketing department
a. Query the salaries of all marketing staff
select salary from emp where dept_id=(select id from dept where name='市场部');
b. Query information about employees whose salary is higher than that of all employees in the marketing department
select * from empwhere salary>all
(select salary from emp where dept_id=(select id from dept where name='市场部'));
2.4.3 Query information about employees whose salary is higher than that of one of the marketing department
select * from emp where salary>any
(select salary from emp where dept_id=(select id from dept where name='市场部'));
2.5 Row Subquery Concept
The result returned by a subquery is one row (can be multiple columns), and this subquery is called a row subquery.
Commonly used operators: = , <>, IN , NOT IN
2.6 Row Subquery Exercise
Query the same employee information as Du Fu's salary and direct leadership
2.6.1 Row Subqueries
select *
from emp
where (salary, mangagerid) = (select salary, mangagerid from emp where name = '杜甫');
2.7 Table subquery concept
The result returned by a subquery is multiple rows and multiple columns. This kind of subquery is called a table subquery.
Commonly used operators: IN
2.7.1 Query employee information with the same position and salary as Li Bai and Du Fu
select *
from emp
where (job, mangagerid) in (select salary, mangagerid
from emp
where name = '李白'
or name = '杜甫');
2.7.2 Query the date information and department information of employees whose joining date is after '100-01-01'
a. The entry date is information after '100-01-01'
select * from emp where entrydate>'100-01-01';
b. Query this part of employees, the corresponding department information
select e.*,d.* from(select * from emp where entrydate>'100-01-01') e
left join dept on e.dept_id=d.id;
2. Summary
Multi-table query is over at this point, have you learned multi-table query? In the next issue, we will conduct a comprehensive practice course on multi-table queries. I hope you can really learn how to query multiple tables. Looking forward to seeing you next time!