当前位置:网站首页>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 )
边栏推荐
- [Yocto RM]8 - OpenEmbedded Kickstart (.wks) Reference
- SQL 注入绕过(三)
- 架构高可靠性应用知识图谱 ----- 微服务架构图谱
- ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)
- Cesium anti aliasing (lines, borders, etc.)
- Embedded must learn, detailed explanation of hardware resource interface -- Based on arm am335x development board (Part 1)
- Redis~Geospatial(地理空间)、Hyperloglog(基数统计)
- The interviewer asked: can you simulate the new operator of JS
- 1382. balancing binary search tree - General method
- Jenkins - Pipeline 概念及创建方式
猜你喜欢
技术人员如何成为技术领域专家
Jenkins - 訪問 Jenkins 自定義參數變量,處理變量值中含有空格
Cesium 多边形增加文字标签(polygon 加 label)多边形中心点偏移问题解决
Jenkins - groovy postbuild plug-in enriches build history information
Jenkins - Pipeline syntax
Appium automation test foundation ADB common commands (I)
SQL 注入绕过(二)
Appium自动化测试基础— 补充:App的包名(appPackage)和启动名(appActivity)
Jenkins - email notification plug-in
原理图合并中的技巧
随机推荐
Keil “st-link usb communication error“解决方法
Locust 性能测试 - 参数化,并发循环数据采样不重复
JS 随机数(随机数 小数)
指南针股票开户是会有什么风险吗?指南针开户安全吗
原理图合并中的技巧
fiddle如何使用代理
Jenkins - 访问 Jenkins 自定义参数变量,处理变量值中含有空格
低代码DSL里面在数仓中的实践
Appium automation test foundation ADB common commands (I)
SQL 注入绕过(四)
文件傳輸協議--FTP
Cesium 点击获取经纬度(二维坐标)
【sylixos】i2c设备驱动创建和使用
数据治理与数据标准
类的初始化与回调的用法
CVPR22收录论文|基于标签关系树的层级残差多粒度分类网络
Intranet penetration with FRP
架构高可靠性应用知识图谱 ----- 微服务架构图谱
Cvpr22 collected papers | hierarchical residual multi granularity classification network based on label relation tree
ScheduledThreadPoolExecutor源码解读(二)