View on GitHub

Notes

reference notes

Using subquery to solve a problm

Who has a salary greater than Abels? e

Subquery syntax

SELECT column_name(s)
FROM table_name
WHERE column_name operator
    (SELECT column_name 
    FROM table_name 
    WHERE condition);
SELECT last_name, salary
FROM employees
WHERE salary > (SELECT salary
                FROM employees
                WHERE last_name = 'Abel');

Rules for subqueries

Types of subqueries

Single-row subqueries

Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to

Executing Single-Row Subqueries

SELECT last_name, job_id, salary
FROM employees
WHERE job_id =
              (SELECT job_id
              FROM employees
              WHERE last_name = 'Taylor')
AND salary >
              (SELECT salary
              FROM employees
              WHERE last_name = 'Taylor');

Using group functions with single-row subqueries:

SELECT last_name, job_id, salary
FROM employees
WHERE salary >
              (SELECT AVG(salary)
              FROM employees
              WHERE job_id = 'IT_PROG');

HAVING Clause with Subqueries:

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

Multiple-row subqueries

Operator Meaning
IN Equal to any value in a list
ALL Must be preceded by a comparison operator(>, <, =, etc.). returns true if the comparison operator is true for all rows in the subquery.
ANY Must be preceded by a comparison operator(>, <, =, etc.). returns true if the comparison operator is true for any row in the subquery.

ANY:

SELECT last_name, salary
FROM employees
WHERE salary > ANY
              (SELECT salary
              FROM employees
              WHERE job_id = 'IT_PROG');

ALL:

SELECT last_name, salary
FROM employees
WHERE salary > ALL
              (SELECT salary
              FROM employees
              WHERE job_id = 'IT_PROG');

IN:

SELECT last_name, salary
FROM employees
WHERE salary > ANY
              (SELECT salary
              FROM employees
              WHERE job_id = 'IT_PROG');

just like using = ANY

EXISTS and NOT EXISTS

These operators simply test whether or not any values are returned by the subquery

If one or more values are returned, then values from the subquery are used to run the top-level query.

If no values are returned, the top-level query produces an empty set as the result.

EXISTS:

SELECT E1.EmployeeNumber, E1.Department, E1.DeptPhone
FROM EMPLOYEE AS E1
WHERE EXISTS
 (SELECT E2.Department
 FROM EMPLOYEE AS E2
 WHERE E1.Department = E2.Department
    AND E1.DeptPhone <> E2.DeptPhone);

NOT EXISTS:

SELECT E1.EmployeeNumber, E1.Department, E1.DeptPhone
FROM EMPLOYEE AS E1
WHERE NOT EXISTS
 (SELECT E2.Department
 FROM EMPLOYEE AS E2
 WHERE E1.Department = E2.Department
    AND E1.DeptPhone <> E2.DeptPhone);

NULL Values in a Subquery

SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);

Subquery returns no rows because one of the values returned by a subquery is Null.

Alternatively, a WHERE clause can be included in the subquery to display all employees who do not have any subordinates:

SELECT last_name FROM employees
WHERE employee_id NOT IN
    (SELECT manager_id
    FROM employees
    WHERE manager_id IS NOT NULL);