Nested Query with SELECT Statement

The basic syntax of SELECT Statement with Nested Query is as follows :
 SELECT column_name1, ..., column_nameN
     FROM table_name1
 WHERE value IN  (SELECT column_name FROM table_name2
                         WHERE condition);
For example consider the below table students :
 STUDENT_ID   STUDENT_NAME   MARKS     GRADE
 ---------- --------------- --------- -------
   101           John          89        A    
   102           Bruce         85        A    
   103           Kevin         91        A    
   104           Rick          37        C    
   105           Joe           95        A    
   106           Patrik        57        B    
   107           Alice         50        B    
   108           Peter         87        A    
Now we are writing A nested query to find out the students with A grade
 SQL> SELECT * FROM STUDENTS 
  WHERE STUDENT_ID IN (SELECT STUDENT_ID FROM STUDENTS WHERE MARKS >= 70);
And the result would be :
 STUDENT_ID    STUDENT_NAME     MARKS    GRADE
 ----------  ---------------- ---------  -------
   101          John              89        A    
   102          Bruce             85        A    
   103          Kevin             91        A    
   105          Joe               95        A    
   108          Peter             87        A    

Next Topic :

No comments:

Post a Comment