Database MySQL-- subqueries

Examples of file 1: https://files.cnblogs.com/files/Vera-y/myemployees.zip

Subquery: also known within the query appear in other statements select statement

The main query: also known outside the query, select statement nested inside another query

classification:

The number of different ranks according to the result set:

  Scalar (one-way) subquery: only one line of a result set

  Column (multi-line) subquery: The result set only over one row

  Row subquery: a row of the result set multiple columns

  Table subquery: The result set is typically rows and columns (row may also comprise a plurality of rows, etc.)

By location subquery appears:

  Back select: only supports standard quantum query

  from behind: Support table subqueries

  ♦  WHERE or later having: a scalar supports queries, column subquery (subquery is also possible (with less))

  Behind exists (also known as a correlated subquery): Table subqueries

A, where on the back or having

Features: 1) sub-query on the small brackets

   2) sub-query on the general condition of the right side

   3) scalar query, typically used with these single-line operator (> <> = <= = <>)

      Column subquery, generally with the use of multi-line operator (IN / NOT IN, ANY / SOME, ALL)

   Execution 4) sub-query precedence over the execution of the main query

Illegal use of standard quantum query:

  1) sub-query result set is not a row

  2) sub-query result set is empty

1. scalar query

Example 1. Whose wages higher than Abel?

SELECT last_name,salary
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name='Abel'
);

Example 2. Return the same job_id with 141 employees, salary of employees and more than 143 employee name, job_id and wages

SELECT salary,last_name,job_id
FROM employees
WHERE salary > (
SELECT salary
FROM employees
WHERE employee_id='143'
)AND job_id = (
SELECT job_id
FROM employees
WHERE employee_id='141'
);

Example 3. Query No. 50 is greater than the minimum wage sector minimum wage and the minimum wage department id

SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (
SELECT MIN(salary)
FROM employees
WHERE department_id=50
);

2. column subquery (single multiple-row subqueries)

Example 1. Return location_id department is 1400 or 1700 in the names of all employees

Last_name the SELECT
the FROM the Employees
the WHERE DEPARTMENT_ID the IN (# here may replace the IN outside the ANY =
the SELECT DEPARTMENT_ID the DISTINCT
the FROM Departments
the WHERE location_id the IN (1400,1700)
);

Example 2. Return IT_PROG other types of jobs for any of a low ratio of number of employees wages of employees job_id, name, job_id, 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';
# 或
SELECT employee_id, last_name,job_id,salary
FROM employees
WHERE salary<(
SELECT MAX(salary)
FROM employees
WHERE job_id = 'IT_PROG'
)AND job_id <> 'IT_PROG';

3. Return to other types of cases for IT_PROG all types of low-wage employees than the number of employees job_id, name, job_id, 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';
#或
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. subquery row (row of the result set multiple columns or rows and columns)

Example: Query number of employees and the highest minimum wage employee information

1) general solution  

  SELECT *
  FROM employees
  WHERE employee_id =(
  SELECT MIN(employee_id)
  FROM employees
  )AND salary=(
  SELECT MAX(salary)
  FROM employees
  );

2) row subquery solution

  SELECT *
  FROM employees
  WHERE (employee_id,salary)=(
  SELECT MIN(employee_id),MAX(salary)
  FROM employees
  );

Second, select on the back

1. Example # Query the number of employees the department table, staff table for each department

SELECT departments.*, (
  SELECT COUNT(*)
  FROM employees
  WHERE employees.department_id = departments.department_id
) AS 个数
FROM departments;

Example 2. Query employee number # = 102 sectors can be directly connected to name query, the scalar query

SELECT (# only one row (scalar query)
  the SELECT DEPARTMENT_NAME
  the FROM Departments
  the INNER the JOIN the Employees
  the ON departments.department_id = employees.department_id
  the WHERE employees.employee_id = 102
) the name of the AS sector;

Third, on the back from

Example, the query average salary for each department of the wage scale

Ag_dep the SELECT. *, Grade_level
the FROM (
  the SELECT AVG (salary) AS AG, department_id
  the FROM the Employees
  the GROUP BY department_id
) AS ag_dep # sub-query results to a table, attention must be surnamed
the INNER JOIN job_grades
ON ag_dep.ag the BETWEEN lowest_sal the AND highest_sal ;

Fourth, on the back exists (correlated subquery)

Syntax: select .. exists (complete sub-query)

Analyzing the results of the subquery has no value, if so returns a Boolean value, no Boolean Returns 0

Note: exists outside the query is executed first, and then sub-query filter

Example 1. Query department employees have

Related queries exists

  SELECT department_name
  FROM departments
  WHERE EXISTS(
    SELECT *
    FROM employees
    WHERE departments.department_id=employees.department_id
  );

Query with IN

  SELECT department_name
  FROM departments
  WHERE departments.department_id IN(
    SELECT department_id
    FROM employees
  );

 

Guess you like

Origin www.cnblogs.com/Vera-y/p/10945502.html