当前位置:网站首页>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

One 、where or having Back

1. Scalar subquery

example 1: Whose pay ratio is Abel high ?

example 2: return job_id And 141 Same as employee No ,salary Than 143 Employees with more employees full name ,job_id And wages

example 3: Return to the lowest paid employees of the company last_name,job_id and salary

  example 4: The minimum wage is greater than 50 The Department of minimum wage id And its minimum wage

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 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

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

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

Two 、select Back

example 1: Query the number of employees in each department

3、 ... and 、from Back

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 .

原网站

版权声明
本文为[Yiyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210613315470.html