w3resource

SQL Nested subqueries

Nested subqueries

A subquery can be nested inside other subqueries. SQL has an ability to nest queries within one another. A subquery is a SELECT statement that is nested within another SELECT statement and which return intermediate results. SQL executes innermost subquery first, then next level. See the following examples :

Example -1 : Nested subqueries

If we want to retrieve that unique job_id and there average salary from the employees table which unique job_id have a salary is smaller than (the maximum of averages of min_salary of each unique job_id from the jobs table which job_id are in the list, picking from (the job_history table which is within the department_id 50 and 100)) the following SQL statement can be used :

Sample table: employees


Sample table: jobs


SQL Code:


-- Selecting job_id and the average salary from the employees table
SELECT job_id, AVG(salary) 
-- Grouping the results by job_id
FROM employees   
GROUP BY job_id   
-- Filtering the grouped results based on a condition
HAVING AVG(salary) <           
    (
        -- Selecting the maximum of the average of min_salary from the jobs table
        SELECT MAX(AVG(min_salary))            
        -- From the jobs table
        FROM jobs             
        -- Filtering jobs where the job_id is in the subquery result
        WHERE job_id IN                 
            (
                -- Selecting job_id from job_history table
                SELECT job_id 
                -- Filtering job_history where department_id is between 50 and 100
                FROM job_history                  
                WHERE department_id BETWEEN 50 AND 100
            )
        -- Grouping the results by job_id from the subquery
        GROUP BY job_id
    );

The above code is executed in Oracle 11g Express Edition.

Explanation:

  • This SQL query calculates the average salary for each job_id from the "employees" table.

  • It groups the results by job_id.

  • The HAVING clause filters the grouped results based on a condition.

  • The condition compares the average salary for each job_id with the maximum average of the minimum salary for jobs within specific department_ids.

  • The subquery selects the maximum of the average of min_salary from the "jobs" table, filtering it based on the job_ids present in the subquery result.

  • The subquery within the WHERE clause selects job_ids from the "job_history" table where the department_id falls between 50 and 100.

  • The main query then selects job_ids and their corresponding average salaries where the average salary is less than the maximum average of the minimum salary for jobs within specific department_ids.

or


-- Selecting job_id and the average salary from the employees table
SELECT job_id, AVG(salary) 
-- Grouping the results by job_id
FROM employees   
GROUP BY job_id   
-- Filtering the grouped results based on a condition
HAVING AVG(salary) <           
    (
        -- Selecting the maximum of the myavg column from the subquery
        SELECT MAX(myavg) 
        -- Selecting job_id and average minimum salary as myavg from the jobs table
        FROM (SELECT job_id, AVG(min_salary) as myavg
              -- From the jobs table
              FROM jobs             
              -- Filtering jobs where the job_id is in the subquery result
              WHERE job_id IN                 
                  (
                      -- Selecting job_id from job_history table
                      SELECT job_id 
                      -- Filtering job_history where department_id is between 50 and 100
                      FROM job_history                  
                      WHERE department_id BETWEEN 50 AND 100
                  )
              -- Grouping the results by job_id
              GROUP BY job_id) ss
    );

The above code is executed in PostgreSQL 9.3

Explanation:

  • This SQL query calculates the average salary for each job_id from the "employees" table.

  • It groups the results by job_id.

  • The HAVING clause filters the grouped results based on a condition.

  • The condition compares the average salary for each job_id with the maximum average of the minimum salary for jobs within specific department_ids.

  • The subquery selects the maximum value from the "myavg" column.

  • This subquery selects job_ids and their corresponding average minimum salaries, filtering it based on the job_ids present in the subquery result.

  • The subquery within the WHERE clause selects job_ids from the "job_history" table where the department_id falls between 50 and 100.

  • The main query then selects job_ids and their corresponding average salaries where the average salary is less than the maximum average of the minimum salary for jobs within specific department_ids.

Output

JOB_ID     AVG(SALARY)
---------- -----------
IT_PROG           5760
AC_ACCOUNT        8300
ST_MAN            7280
AD_ASST           4400
SH_CLERK          3215
FI_ACCOUNT        7920
PU_CLERK          2780
SA_REP            8350
MK_REP            6000
ST_CLERK          2785
HR_REP            6500

Let's break the example down into three parts and observes the results returned.

Atfirst the nested subquery as follows:

SQL Code:


-- Selecting job_id from the job_history table
SELECT job_id 
-- Filtering job_history records based on department_id
WHERE department_id      
-- Specifying the range for department_id filtering
BETWEEN 50 AND 100;

Explanation:

  • This SQL query retrieves job_ids from the "job_history" table.

  • It filters the records based on the department_id.

  • The BETWEEN operator is used to specify a range for department_id filtering, including values between 50 and 100.

  • The query returns job_ids associated with department_ids falling within the specified range.

This nested subquery retrieves the job_id(s) from job_history table which is within the department_id 50 and 100.

Here is the output.

Output:

JOB_ID
----------
ST_CLERK
ST_CLERK
IT_PROG
SA_REP
SA_MAN
AD_ASST
AC_ACCOUNT

Here is the pictorial representation of how the above output comes.

Sql subqueries with in

Now the subquery that receives output from the nested subquery stated above.


SELECT MAX(AVG(min_salary))
FROM jobs WHERE job_id 
IN(.....output from the nested subquery......)
GROUP BY job_id

The subquery internally works as follows:

SQL Code:


-- Selecting the maximum of the average of min_salary from the jobs table
SELECT MAX(AVG(min_salary))
-- From the jobs table
FROM jobs 
-- Filtering jobs based on job_id
WHERE job_id 
-- Specifying the list of job_ids for filtering
IN (
    'ST_CLERK', 'ST_CLERK', 'IT_PROG', 'SA_REP', 'SA_MAN', 'AD_ASST', 'AC_ACCOUNT'
)
-- Grouping the results by job_id
GROUP BY job_id;

Explanation:

  • This SQL query calculates the average minimum salary for each job_id from the "jobs" table.

  • It first filters the jobs based on specific job_ids listed.

  • The MAX() function is then applied to get the maximum value of the average minimum salary across all the job_ids.

  • The GROUP BY clause groups the results by job_id, allowing the calculation of the average minimum salary for each job_id separately.

  • The query returns the maximum value among these averages.

Here is the output:

Output:

MAX(AVG(MIN_SALARY))
--------------------
               10000

Here is the pictorial representation of how the above output returns.

Sql subqueries with in

Now the outer query that receives output from the subquery and which also receives the output from the nested subquery stated above.


SELECT job_id,AVG(salary) 
FROM employees   
GROUP BY job_id   
HAVING AVG(salary)<   
(.....output from the subquery(         
output from the nested subquery)......)

The outer query internally works as follows:

SQL Code:


-- Selecting job_id and the average salary from the employees table
SELECT job_id, AVG(salary) 
-- Grouping the results by job_id
FROM employees   
GROUP BY job_id   
-- Filtering the grouped results based on a condition
HAVING AVG(salary) < 10000;

Explanation:

  • This SQL query calculates the average salary for each job_id from the "employees" table.

  • It groups the results by job_id.

  • The HAVING clause filters the grouped results based on a condition.

  • The condition specifies that only those records should be selected where the average salary for each job_id is less than 10000.

  • The query returns job_ids and their corresponding average salaries where the average salary is less than 10000.

Output:

JOB_ID     AVG(SALARY)
---------- -----------
IT_PROG           5760
AC_ACCOUNT        8300
ST_MAN            7280
AD_ASST           4400
SH_CLERK          3215
FI_ACCOUNT        7920
PU_CLERK          2780
SA_REP            8350
MK_REP            6000
ST_CLERK          2785
HR_REP            6500

Example -2 : Nested subqueries

Here is an another nested subquery example.

Sample table : orders


Sample table : customer


Sample table : agents


SQL Code:


-- Selecting specific columns from the orders table
SELECT ord_num, ord_date, ord_amount, advance_amount
-- Filtering orders based on multiple conditions
FROM orders 
-- Specifying conditions for filtering
WHERE ord_amount > 2000 
-- Filtering based on order amount greater than 2000
AND ord_date < '01-SEP-08' 
-- Filtering based on order date before 01-SEP-08
AND ADVANCE_AMOUNT <         
    -- Comparing advance_amount with values from a subquery result
    ANY(
        SELECT OUTSTANDING_AMT		
        -- Selecting outstanding amounts from the CUSTOMER table
        FROM CUSTOMER	
        -- Specifying conditions for filtering customers
        WHERE GRADE = 3 
        -- Filtering based on customer grade being 3
        AND CUST_COUNTRY <> 'India'	
        -- Filtering based on customer country not being India
        AND opening_amt < 7000 
        -- Filtering based on opening amount less than 7000
        AND EXISTS		        
            -- Checking for existence of records in a subquery
            (
                SELECT * 				 
                -- Selecting all columns from the agents table
                FROM agents				  
                -- Specifying conditions for filtering agents
                WHERE commission < .12
                -- Filtering based on commission less than 0.12
            )
    );

Explanation:

  • This SQL query retrieves specific columns from the "orders" table.

  • It filters the orders based on multiple conditions:

    • Orders with ord_amount greater than 2000.

    • Orders with ord_date before 01-SEP-08.

    • Orders with advance_amount less than any of the values retrieved from a subquery.

  • The subquery retrieves outstanding amounts from the CUSTOMER table based on several conditions.

  • The conditions include GRADE being 3, CUST_COUNTRY not being 'India', and opening_amt less than 7000.

  • The subquery also includes an EXISTS clause to check for the existence of records in another subquery.

  • This inner subquery selects all columns from the agents table and filters them based on the commission being less than 0.12.

  • Overall, the query selects orders that meet the specified conditions on ord_amount, ord_date, advance_amount, and the results of the subquery.

Output:

   ORD_NUM ORD_DATE  ORD_AMOUNT ADVANCE_AMOUNT
---------- --------- ---------- --------------
    200130 30-JUL-08       2500            400
    200127 20-JUL-08       2500            400
    200110 15-APR-08       3000            500
    200105 18-JUL-08       2500            500
    200129 20-JUL-08       2500            500
    200108 15-FEB-08       4000            600
    200113 10-JUN-08       4000            600
    200106 20-APR-08       2500            700
    200109 30-JUL-08       3500            800
    200107 30-AUG-08       4500            900
    200101 15-JUL-08       3000           1000
    200128 20-JUL-08       3500           1500
    200114 15-AUG-08       3500           2000

Let's break the code and analyze what's going on in inner query. Here is the first code of inner query with output :

SQL Code:

SELECT *
FROM agents
WHERE commission<.12;

Output:

AGENT_CODE AGENT_NAME      WORKING_AREA      COMMISSION PHONE_NO        COUNTRY
---------- --------------- ----------------- ---------- --------------- ---------
A009       Benjamin        Hampshair                .11 008-22536178
A002       Mukesh          Mumbai                   .11 029-12358964

Here is the second code of inner query (including first one) with output :

SQL Code:


-- Selecting outstanding amounts from the CUSTOMER table
SELECT OUTSTANDING_AMT
-- Filtering customers based on multiple conditions
FROM CUSTOMER
-- Specifying conditions for filtering customers
WHERE GRADE = 3 
-- Filtering based on customer grade being 3
AND CUST_COUNTRY <> 'India' 
-- Filtering based on customer country not being India
AND opening_amt < 7000 
-- Filtering based on opening amount less than 7000
AND EXISTS(
    -- Checking for existence of records in a subquery
    SELECT * 
    -- Selecting all columns from the agents table
    FROM agents
    -- Specifying conditions for filtering agents
    WHERE commission < .12
    -- Filtering based on commission less than 0.12
);

Explanation:

  • This SQL query retrieves outstanding amounts from the CUSTOMER table.

  • It filters the customers based on multiple conditions:

    • Customers with GRADE equal to 3.

    • Customers with CUST_COUNTRY not equal to 'India'.

    • Customers with opening_amt less than 7000.

  • The query also includes an EXISTS clause to check for the existence of records in a subquery.

  • The subquery selects all columns from the agents table and filters them based on the commission being less than 0.12.

  • Overall, the query selects outstanding amounts from customers who meet the specified conditions on GRADE, CUST_COUNTRY, opening_amt, and the existence of agents with a commission less than 0.12.

Output:

OUTSTANDING_AMT
---------------
           6000
           3000
           5000

See our Model Database

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: Correlated subqueries using aliases
Next: Union



Follow us on Facebook and Twitter for latest update.