当前位置:网站首页>MySQL query related knowledge (advanced level 7: sub query
MySQL query related knowledge (advanced level 7: sub query
2022-06-28 02:31:00 【Minsk open source】
classification :
Click the location where the subquery appears :
select Back :
Only scalar subqueries are supported
from Back :
Support table subquery
where or having Back *
Scalar subquery ( A single )√
Column query ( Multiple lines )√
Line sub query
exists Back ( Correlation subquery )
Table sub query
Depending on the number of rows and columns in the result set :
Scalar subquery ( The result set has only one row and one column )
Column query ( The result set has only one row and more columns )
Line sub query ( The result set has one row and more columns )
Table sub query ( The result set is usually multi row and multi column )
[](() One 、where or having Back
1. Scalar subquery ( Single line sub query )
2. Column query ( Multi line sub query )
3. Line sub query ( Multiple rows and columns )
characteristic :
1. Subqueries in parentheses
2. Subqueries are usually placed on the right side of the condition
3. Scalar subquery , Usually with a single line operator (>,<,= wait ) Use
Column query , Generally, this multi line operator is used (in,any/some,all) Use
4. The execution of the subquery takes precedence over the execution of the main query , The conditions of the main query use the results of the sub query
*/
[](()1. Scalar subquery
# Case study 1、 Whose pay ratio is Abel high ?
#① Inquire about Abel The salary of
SELECT salary
FROM employees
WHERE last_name
=‘Abel’;
#② Search for employee information , Satisfy salary>① result
SELECT *
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name
=‘Abel’
);
# Case study 2: return job_id And 141 Same as employee No ,salary Than 143 Employees with more employees full name ,job_id And wages
SELECT last_name
,job_id
,salary
FROM employees
WHERE job_id=(
SELECT job_id
FROM employees
WHERE employee_id
=141
)AND salary>(
SELECT salary
FROM employees
WHERE employee_id
=143
);
# Case study 3: Return the name of the employee with the lowest salary in the company ,job_id
and salary
SELECT last_name
,job_id
,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
# Case study 4、 The minimum wage is greater than 50 The Department of minimum wage id And its minimum wage
SELECTdepartment_id
,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id
=50
);
[](()2. Column query ( Multi line sub query )
# Case study 1、 return location_id
yes 1400 or 1700 The names of all employees in the Department
SELECT last_na **《 A big factory Java Analysis of interview questions + Back end development learning notes + The latest architecture explanation video + Practical project source code handout 》 Free open source Prestige search official account 【 Advanced programming 】** me
FROM employees
WHERE department_id
IN(
SELECT DISTINCT department_id
FROM departments
WHERE location_id
IN (1400,1700)
);
# Case study 2、 Return to other types of work job_id
by ‘IT_PROG’ Employees of any type of work with low wages : Job number , full name ,job_id
as well as salary
SELECT employee_id
,last_name
,job_id
,salary
FROM employees
WHERE salary < ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id
=‘IT_PROG’
)
AND job_id
<>‘IT_PROG’;
# Case study 3、 Return to other types of work job_id
by ‘IT_PROG’ Of all low paid employees in the type of work : Job number , full name ,job_id
as well as salary
SELECT employee_id
,last_name
,job_id
,salary
FROM employees
WHERE salary < ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id
=‘IT_PROG’
)
AND job_id
<>‘IT_PROG’;
perhaps
SELECT employee_id
,last_name
,job_id
,salary
FROM employees
WHERE salary < (
SELECT MIN(salary
)
FROM employees
WHERE job_id
=‘IT_PROG’
)
AND job_id
<>‘IT_PROG’;
[](()3. Line sub query
( The result set has one row and multiple columns or multiple rows and multiple columns )( When using this method, all information can be represented by a judgment symbol , This method can also be obtained by other subqueries )
# Case study 、 Query the information of the employee with the lowest employee number and the highest salary
SELECT *
FROM employees
WHERE (employee_id
,salary
)=(
SELECT MIN(employee_id
),MAX(salary)
FROM employees
);
[](() Two 、 Put it in SELECT Behind ( Only scalar subqueries are supported ( That is, one row, one column ))
# Case study 、 Query the number of employees in each department
SELECT d.*,(
SELECT COUNT(*)
FROM employees
e
WHERE e.department_id
=d.department_id
) Number
FROM departments
d;
# Case study 、 Check the employee number =102 The name of your department
SELECT department_name
FROM departments
d,employees
e
WHERE d.department_id
=e.department_id
AND e.employee_id
=102;
[](() 3、 ... and 、 Put it in FROM Behind ( This means that the result of the query is used as a table ( The alias is required ))
# Case study 、 Query the average salary grade of each department
SELECT biao.*,j.grade_level
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
)biao
INNER JOIN job_grades
j
ON biao.ag BETWEEN j.lowest_sal
AND highest_sal
;
[](() Four 、EXISTS Back ( Correlation subquery )
边栏推荐
- SQL 注入绕过(二)
- Jenkins - Pipeline syntax
- Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 2)
- Using redis bitmap to realize personnel online monitoring
- [Yocto RM]8 - OpenEmbedded Kickstart (.wks) Reference
- How to handle computer security certificate errors
- Dynamic Host Configuration Protocol
- Numpy----np.tile()函数解析
- Take n multiple table names of a database as the values of a column in another table (the range can be a table in another database)
- LeetCode - Easy - 197
猜你喜欢
【永艺XY椅】试用体验
1382. balancing binary search tree - General method
Anonymous Mount & named mount
File transfer protocol --ftp
Cesium 获取屏幕所在经纬度范围
Jenkins - built in variable access
Intranet penetration with FRP
mysql面试百题集
Jenkins - groovy postbuild plug-in enriches build history information
How to use data-driven "customer lifecycle management" to improve lead conversion rate and customer satisfaction?
随机推荐
Cesium Color 颜色(赋值)随机颜色
Anonymous Mount & named mount
我今天忘带手机了
Cesium color color (assignment) random color
Appium automation test foundation - Supplement: app package name and appactivity
指南针股票开户是会有什么风险吗?指南针开户安全吗
Take n multiple table names of a database as the values of a column in another table (the range can be a table in another database)
系统管理员设置了系统策略,禁止进行此安装。解决方案
Prometeus 2.35.0 新特性
Deep parsing of kubernetes controller runtime
【sylixos】NEW_1 型字符驱动示例
1382. balancing binary search tree - General method
Jenkins - access the Jenkins user-defined parameter variable, and handle the variable value containing spaces
Cesium 抗锯齿(线,边框等)
Coscon'22 lecturer solicitation order
Low code solution - a low code solution for digital after-sales service covering the whole process of work order, maintenance and Finance
Fundamentals of scala (3): operators and process control
图灵机启动顺序
Jenkins - 訪問 Jenkins 自定義參數變量,處理變量值中含有空格
Jenkins - email notification plug-in