当前位置:网站首页>Mysql database foundation: sub query
Mysql database foundation: sub query
2022-06-21 06:34:00 【Yiyi】
【MySQL Database foundation :8. Subquery 】
author :zhenyi
special column :mysql database 1
brief introduction :MySQL Basic column , fit MySQL Learning and reading in the basic stage .
Documents used in the article , Contact the author for ! If it helps you , Pay attention , Ongoing update , Thank you for your support . If there is a problem or a mistake , Welcome to point out your personal letter and contact me , thank you .
Catalog
example 1: Whose pay ratio is Abel high ?
example 3: Return to the lowest paid employees of the company last_name,job_id and salary
2. Column query ( Multi line sub query )
example 1: return location_id yes 1400 or 1700 The names of all employees in the Department
example 1: Query the employee information with the lowest employee number and the highest salary
example 1: Query the number of employees in each department
example 1: Query the average wage level of each department
Four 、exists Back ( Correlation subquery )
example 1: Check the Department name of the employee
Subquery meaning :
In other statements select sentence , Called subquery or inner query
External query statements , Called main query or external query
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 column and multiple rows )
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 ( More columns, more lines )
characteristic :
① Subqueries in parentheses
② Subqueries are usually placed on the right side of the condition
③ Scalar subquery , Usually used with a single line operator
> < >= <= = <>
Column query , Usually used with multiline operators
in、any/some、all
④ 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
example 1: Whose pay ratio is Abel high ?
SELECT *
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
SELECT *
FROM employees
WHERE salary >(
SELECT salary
FROM employees
WHERE last_name='Abel'
);The query results are as follows :

example 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
);
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
);The query results are as follows :
example 3: Return to the lowest paid employees of the company last_name,job_id and salary
SELECT last_name,job_id,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
SELECT last_name,job_id,salary
FROM employees
WHERE salary =(
SELECT MIN(salary)
FROM employees
);The query results are as follows :

example 4: The minimum wage is greater than 50 The Department of minimum wage id And its minimum wage
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
);
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id=50
);The query results are as follows :

Illegal use of scalar subqueries
SELECT MIN(salary),department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT salary
FROM employees
WHERE department_id = 250
);
2. Column query ( Multi line sub query )
example 1: return location_id yes 1400 or 1700 The names of all employees in the Department
SELECT last_name
FROM employees
WHERE department_id <>ALL(
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
);
SELECT last_name
FROM employees
WHERE department_id <>ALL(
SELECT DISTINCT department_id
FROM departments
WHERE location_id IN(1400,1700)
);The query results are as follows :

example 2: Go back to other jobs job_id by ‘IT_PROG’ The employee number of any employee with a low wage 、 full name 、job_id as well as salary
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
or
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MAX(salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id ='IT_PROG'
)AND job_id<>'IT_PROG';The query results are as follows :

example 3: Return to other departments job_id by ‘IT_PROG’ Employee number of all employees in the Department with low salary 、 full name 、job_id as well as salary
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<ALL(
SELECT DISTINCT salary
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
or
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MIN( salary)
FROM employees
WHERE job_id = 'IT_PROG'
) AND job_id<>'IT_PROG';
SELECT last_name,employee_id,job_id,salary
FROM employees
WHERE salary<(
SELECT MIN(salary)
FROM employees
WHERE job_id='IT_PROG'
) AND job_id <>'IT_PROG';The query results are as follows :

3、 Line sub query ( The result set has one row and multiple columns or multiple rows and multiple columns )
example 1: Query the employee information with the lowest employee number and the highest salary
SELECT *
FROM employees
WHERE employee_id=(
SELECT MIN(employee_id)
FROM employees
)AND salary=(
SELECT MAX(salary)
FROM employees
);
SELECT *
FROM employees
WHERE employee_id=(
SELECT MIN(employee_id)
FROM employees
) AND salary=(
SELECT MAX(salary)
FROM employees
);The query results are as follows :

Two 、select Back
Only scalar subqueries are supported
example 1: Query the number of employees in each department
SELECT d.*,(SELECT COUNT(*) FROM employees AS e WHERE e.department_id=d.department_id) AS Number
FROM departments AS d;
SELECT d.*,(SELECT COUNT(*) FROM employees AS e WHERE e.department_id=d.department_id) AS Number
FROM departments AS d;The query results are as follows :

3、 ... and 、from Back
Use the subquery results as a table , The alias is required
example 1: Query the average wage level of each department
① Query the average salary of each department
SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
SELECT * FROM job_grades;
#② Connect ① The result set and job_grades surface , Screening criteria average wage between lowest_sal and highest_sal
SELECT ag_dep.*,g.`grade_level`
FROM (
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id
) ag_dep
INNER JOIN job_grades g
ON ag_dep.ag BETWEEN lowest_sal AND highest_sal;
Four 、exists Back ( Correlation subquery )
grammar :
exists( Complete query statement )
result :
1 or 0
SELECT EXISTS(SELECT employee_id FROM employees WHERE salary=300000);
example 1: Check the Department name of the employee
SELECT department_name
FROM departments d
WHERE EXISTS(
SELECT *
FROM employees e
WHERE d.`department_id`=e.`department_id`
);
SELECT department_name
FROM departments AS d
WHERE EXISTS(
SELECT *
FROM employees AS e
WHERE d.department_id=e.department_id
);The query results are as follows :

If it helps you , Pay attention , Ongoing update , Thank you for your support . If there is a problem or a mistake , Welcome to contact me , thank you .
边栏推荐
猜你喜欢

出现ConcurrentModificationException这个异常报错,怎么处理?

智能需要身体吗

Unity隐藏目录和隐藏文件

FPGA - 7系列 FPGA SelectIO -06- 逻辑资源之ODELAY
![[data mining] final review Chapter 3](/img/9a/68c3ce12b0f55e9b26b6f054e2ffe7.png)
[data mining] final review Chapter 3

NOP法破解简易登录系统

TypeError: iter() returned non-iterator of type ‘xxx‘

FPGA - 7 Series FPGA selectio -04- ideay and ideayctrl of logical resources

TypeError: iter() returned non-iterator of type ‘xxx‘

Pycharm的快捷键Button 4 Click是什么?
随机推荐
docker 安装mysql
【MySQL】数据库多表操作通关教程(外键约束、多表联合查询)
FPGA - 7 Series FPGA selectio -03- ILOGIC of logic resources
数字信号处理-07-DDS IP应用实例
Aurora8b10b IP usage-05-transceiver test application example
Aurora8B10B IP使用 -01- 简介与端口描述
Recursively establish a chained binary tree, complete the traversal of the first, middle and last order and other functions (with source code)
WordPress pseudo original tool - update website one click pseudo original publishing software
回答问题:你认为AGI应该采用什么思路?
Functions and methods in Scala
Niuke-top101-bm26
Docker installing MySQL
如何通过JDBC访问MySQL数据库?手把手实现登录界面(图解+完整代码)
delphi10 ftp文件名乱码问题
nametuple的源码为什么要使用.replace(‘,‘, ‘ ‘).split()而不是.split(‘,‘)
正则表达式基础
scikit-learn中的Scaler
How powerful are spectral graph neural networks
FPGA - 7系列 FPGA SelectIO -01- 简介与DCI技术简介
【毕业季】纸短情长,浅谈大二以前的学习经历
