DQL-Subquery

Subquery

  • Meaning: The select statement that appears in other statements is called a subquery or internal query. The
    external query statement is called a main query or external query.

  • classification

    • According to the location of the subquery:
      • The select statement
        only supports standard quantum queries
        • from statement
          Support table subquery
        • Where or having (focus)
          scalar quantum query (single row)
          column subquery (multiple rows)
          row subquery (use less)

        • Table subquery after the exists statement (correlated subquery)
    • According to the number of rows and columns of the result set
      + standard quantum query (the result is only one row and one column)
      + column subquery (only one column and multiple rows)
      + row subquery (the result can have one row and one column)
      + table subquery (the result set is generally multiple rows and columns )

1. Subquery after where or having

  • Column subquery multi-row comparison operator

    1. IN / NOT IN: equal to any one in the list
    2. ANY / SOME: Comparison with a subquery list (similar to each value comparison is OR operation)
    3. ALL: Compare with every value in the subquery list (similar to every value comparison is AND operation)
  • Features

    1. Subqueries are placed in parentheses

    2. Subqueries are generally placed on the far right side of the condition

    3. Standard quantum query, usually with single-line operator

    <, > , <=, >=, =, !=

    1. Column subquery, generally used with multi-row operators

    in, any/some, all

    1. Sub-query execution priority and main query execution
  1. Standard quantum query (single-row subquery)

    • Case 1: Who has a higher salary than Abel

      SELECT 
          last_name, salary
      FROM
          employees
      WHERE
          salary > (
      		SELECT 
                  salary
              FROM
                  employees
              WHERE
                  last_name = 'Abel');
      
    • Case 2: Return the name, job_id and salary of the employee whose job_id is the same as employee 141, salary is more than employee 141

      Perform a single clause query

      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 3: Return the last_name, job_id and salary of the employee with the lowest salary

      Use group functions in subqueries

      SELECT 
          last_name, job_id, salary
      FROM
          employees
      WHERE
          salary = (SELECT 
                  MIN(salary)
              FROM
                  employees);
      
    • Case 4: Query the department ID and minimum wage of the minimum wage greater than the minimum wage of department 50

      HAVING statement in clause

      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 (multi-row subquery)

    • Case 1: Return the names of all employees in the department whose location_id is 1400 or 1700

      # 连接查询
      SELECT 
          d.location_id, e.last_name
      FROM
          departments d
              INNER JOIN
          employees e ON e.department_id = d.department_id
      WHERE
          d.location_id IN (1400 , 1700);
      # 列子查询
      SELECT 
          last_name
      FROM
          employees
      WHERE
          department_id IN (SELECT 
                  department_id
              FROM
                  departments
              WHERE
                  location_id IN (1400 , 1700));
      
    • Case 2: Return the employee ID, name, job_id, and salary of any employee whose salary is lower than the job_id of any department whose IT_PROG is

      Use the ANY operator in a multi-row subquery

      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 3: Return to the job number, name, job_id, and salary of all employees in the department whose job_id is lower than job_id is 'IT_PROG'

      Use the ALL operator in a multi-row subquery

      SELECT 
          employee_id, last_name, job_id, salary
      FROM
          employees
      WHERE
          salary < ALL (SELECT 
                  salary
              FROM
                  employees
              WHERE
                  job_id = 'IT_PROG')
              AND job_id <> 'IT_PROG';
      
  3. Row subquery (one row with multiple columns, multiple rows with multiple columns)

    • Case: Query the employee ID of the employee with the smallest employee ID and the highest salary. Name, salary, job_id

      SELECT 
          employee_id, last_name, salary, job_id
      FROM
          employees
      WHERE
          employee_id = (SELECT 
                  MIN(employee_id)
              FROM
                  employees)
              AND salary = (SELECT 
                  MAX(salary)
              FROM
                  employees);
      

Second, the subquery behind the select statement

select only supports standard quantum query

  • Case 1: Query the number of employees in each department

    # 子查询
    SELECT 
        d.department_id,
        (SELECT 
                COUNT(*)
            FROM
                employees e
            WHERE
                e.department_id = d.department_id) COUNT
    FROM
        departments d;
    # 分组查询
    SELECT 
        department_id, COUNT(last_name) COUNT
    FROM
        employees
    GROUP BY department_id;
    
  • Case 2: Query the department name with employee number equal to 102

    # 子查询版本
    SELECT 
        department_name
    FROM
        departments
    WHERE
        department_id = (SELECT 
                department_id
            FROM
                employees
            WHERE
                employee_id = 102);
    # 连接查询
    SELECT 
        d.department_name
    FROM
        departments d
            INNER JOIN
        employees e ON e.department_id = d.department_id
    WHERE
        e.employee_id = 102;
    

Three, the subquery behind from

  • Case 1: Query the salary level of the average salary of each department

    SELECT 
        A.department_id, round(A.AVG_SAL, 2), j.grade_level
    FROM
        (SELECT 
            AVG(salary) AVG_SAL, department_id
        FROM
            employees
        GROUP BY department_id) A
            INNER JOIN
        job_grades j ON A.AVG_SAL BETWEEN lowest_sal AND highest_sal;
    

4. Subqueries behind exists (correlated subqueries)

# 判断子查询里面有没有值
SELECT 
    EXISTS( SELECT 
            employee_id
        FROM
            employees); # 1 有
SELECT 
    EXISTS( SELECT 
            salary
        FROM
            employees
        WHERE
            salary = 30000); # 0 没有
  • Case: Query the department name of an employee

    # exists版本
    SELECT 
        department_name
    FROM
        departments d
    WHERE
        EXISTS( SELECT 
                *
            FROM
                employees e
            WHERE
                d.department_id = e.department_id);
    # in版本
    SELECT 
        department_name
    FROM
        departments d
    WHERE
        d.department_id IN (SELECT 
                department_id
            FROM
                employees e
            WHERE
                e.department_id = d.department_id);
    

Related tests

  1. Query the names and wages of employees in the same department as Zlotkey

    SELECT 
        last_name, salary
    FROM
        employees
    WHERE
        department_id = (SELECT 
                department_id
            FROM
                employees
            WHERE
                last_name = 'Zlotkey');
    
  2. Query the employee number, name and salary of employees whose salary is higher than the average salary of the company

    SELECT 
        employee_id, last_name, salary
    FROM
        employees
    WHERE
        salary > (SELECT 
                AVG(salary)
            FROM
                employees);
    
  3. Query the employee number and name of employees in each department whose salary is higher than the average salary of the department

    SELECT 
        employee_id, last_name
    FROM
        employees e
            INNER JOIN
        (SELECT 
            AVG(salary) av, department_id
        FROM
            employees
        GROUP BY department_id) A ON e.department_id = A.department_id
    WHERE
        e.salary > A.av;
    
  4. Query the employee number and name of employees whose name contains the letter u in the same department

    SELECT 
        employee_id, last_name
    FROM
        employees e
    WHERE
        e.department_id IN (SELECT DISTINCT
                department_id
            FROM
                employees
            WHERE
                last_name LIKE '%u%');
    
  5. Query the employee number of the employee working in the department whose location_id is 1700

    SELECT 
        employee_id
    FROM
        employees
    WHERE
        department_id IN (SELECT 
                department_id
            FROM
                departments
            WHERE
                location_id = 1700);
    
  6. Query the name and salary of the employee whose manager is K_ing

    SELECT 
        last_name, salary
    FROM
        employees
    WHERE
        manager_id IN (SELECT 
                employee_id
            FROM
                employees
            WHERE
                last_name = 'K_ing');
    
  7. Query the name of the employee with the highest salary, requiring first_name and last_name as a column

    SELECT 
        CONCAT(first_name, ' ', last_name)
    FROM
        employees
    WHERE
        salary = (SELECT 
                MAX(salary)
            FROM
                employees);

Guess you like

Origin www.cnblogs.com/klenkiven/p/12723738.html