Skip to content
Related Articles

Related Articles

Improve Article

Nested Queries in SQL

  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021

Prerequisites : Basics of SQL

 

In nested queries, a query is written inside a query. The result of inner query is used in execution of outer query. We will use STUDENT, COURSE, STUDENT_COURSE tables for understanding nested queries.

 

STUDENT



S_IDS_NAMES_ADDRESSS_PHONES_AGE
S1RAMDELHI945512345118
S2RAMESHGURGAON965243154318
S3SUJITROHTAK915625313120
S4SURESHDELHI915676897118

 

COURSE

C_IDC_NAME
C1DSA
C2Programming
C3DBMS

 

STUDENT_COURSE

S_IDC_ID
S1C1
S1C3
S2C1
S3C2
S4C2
S4C3

 

There are mainly two types of nested queries:

  • Independent Nested Queries: In independent nested queries, query execution starts from innermost query to outermost queries. The execution of inner query is independent of outer query, but the result of inner query is used in execution of outer query. Various operators like IN, NOT IN, ANY, ALL etc are used in writing independent nested queries.

     



    IN: If we want to find out S_ID who are enrolled in C_NAME ‘DSA’ or ‘DBMS’, we can write it with the help of independent nested query and IN operator. From COURSE table, we can find out C_ID for C_NAME ‘DSA’ or DBMS’ and we can use these C_IDs for finding S_IDs from STUDENT_COURSE TABLE.

     

    STEP 1: Finding C_ID for C_NAME =’DSA’ or ‘DBMS’

    Select C_ID from COURSE where C_NAME = ‘DSA’ or C_NAME = ‘DBMS’

     

    STEP 2: Using C_ID of step 1 for finding S_ID

    Select S_ID from STUDENT_COURSE where C_ID IN

    (SELECT C_ID from COURSE where C_NAME = ‘DSA’ or C_NAME=’DBMS’);

     

    The inner query will return a set with members C1 and C3 and outer query will return those S_IDs for which C_ID is equal to any member of set (C1 and C3 in this case). So, it will return S1, S2 and S4.



     

    Note: If we want to find out names of STUDENTs who have either enrolled in ‘DSA’ or ‘DBMS’, it can be done as:

    Select S_NAME from STUDENT where S_ID IN

    (Select S_ID from STUDENT_COURSE where C_ID IN

    (SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));

     

    NOT IN: If we want to find out S_IDs of STUDENTs who have neither enrolled in ‘DSA’ nor in ‘DBMS’, it can be done as:

    Select S_ID from STUDENT where S_ID NOT IN

    (Select S_ID from STUDENT_COURSE where C_ID IN

    (SELECT C_ID from COURSE where C_NAME=’DSA’ or C_NAME=’DBMS’));

     

    The innermost query will return a set with members C1 and C3. Second inner query will return those S_IDs for which C_ID is equal to any member of set (C1 and C3 in this case) which are S1, S2 and S4. The outermost query will return those S_IDs where S_ID is not a member of set (S1, S2 and S4). So it will return S3.

 

  • Co-related Nested Queries: In co-related nested queries, the output of inner query depends on the row which is being currently executed in outer query. e.g.; If we want to find out S_NAME of STUDENTs who are enrolled in C_ID ‘C1’, it can be done with the help of co-related nested query as:

    Select S_NAME from STUDENT S where EXISTS

    ( select * from STUDENT_COURSE SC where S.S_ID=SC.S_ID and SC.C_ID=’C1’);

     

    For each row of STUDENT S, it will find the rows from STUDENT_COURSE where S.S_ID = SC.S_ID and SC.C_ID=’C1’. If for a S_ID from STUDENT S, atleast a row exists in STUDENT_COURSE SC with C_ID=’C1’, then inner query will return true and corresponding S_ID will be returned as output.

 

This article has been contributed by Sonal Tuteja.

 

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :