SQL | Subquery - GeeksforGeeks

30 Apr, 2020
In SQL a Subquery can be simply defined as a query within another query. In other words we can say that a Subquery is a query that is embedded in WHERE clause of another SQL query.
Important rules for Subqueries:
You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause, FROM clause.Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression operator. It could be equality operator or comparison operator such as =, >, =, <= and Like operator.
A subquery is a query within another query. The outer query is called as main query and inner query is called as subquery.
The subquery generally executes first, and its output is used to complete the query condition for the main or outer query.
Subquery must be enclosed in parentheses.
Subqueries are on the right side of the comparison operator.
ORDER BY command cannot be used in a Subquery. GROUPBY command can be used to perform same function as ORDER BY command.
Use single-row operators with singlerow Subqueries. Use multiple-row operators with multiple-row Subqueries.
Syntax:There is not any general syntax for Subqueries. However, Subqueries are seen to be used most frequently with SELECT statement as shown below:
SELECT column_name
FROM table_name
WHERE column_name expression operator 
    ( SELECT COLUMN_NAME  from TABLE_NAME   WHERE ... );

Sample Table:
DATABASE
STUDENT
Sample Queries
:
To display NAME, LOCATION, PHONE_NUMBER of the students from DATABASE table whose section is ASelect NAME, LOCATION, PHONE_NUMBER from DATABASE 
WHERE ROLL_NO IN
(SELECT ROLL_NO from STUDENT where SECTION=’A’); 
Explanation : First subquery executes “ SELECT ROLL_NO from STUDENT where SECTION=’A’ ” returns ROLL_NO from STUDENT table whose SECTION is ‘A’.Then outer-query executes it and return the NAME, LOCATION, PHONE_NUMBER from the DATABASE table of the student whose ROLL_NO is returned from inner subquery.Output:NAMEROLL_NOLOCATIONPHONE_NUMBERRavi104Salem8989898989Raj102Coimbatore8877665544
Select NAME, LOCATION, PHONE_NUMBER from DATABASE 
WHERE ROLL_NO IN
(SELECT ROLL_NO from STUDENT where SECTION=’A’); 

Explanation : First subquery executes “ SELECT ROLL_NO from STUDENT where SECTION=’A’ ” returns ROLL_NO from STUDENT table whose SECTION is ‘A’.Then outer-query executes it and return the NAME, LOCATION, PHONE_NUMBER from the DATABASE table of the student whose ROLL_NO is returned from inner subquery.Output:
Insert Query Example:Table1: Student1NAMEROLL_NOLOCATIONPHONE_NUMBERRam101chennai9988773344Raju102coimbatore9090909090Ravi103salem8989898989Table2: Student2NAMEROLL_NOLOCATIONPHONE_NUMBERRaj111chennai8787878787Sai112mumbai6565656565Sri113coimbatore7878787878To insert Student2 into Student1 table:INSERT INTO Student1  SELECT * FROM Student2;
Output:NAMEROLL_NOLOCATIONPHONE_NUMBERRam101chennai9988773344Raju102coimbatore9090909090Ravi103salem8989898989Raj111chennai8787878787Sai112mumbai6565656565Sri113coimbatore7878787878
Table1: Student1
Table2: Student2
To insert Student2 into Student1 table:
INSERT INTO Student1  SELECT * FROM Student2;

Output:
To delete students from Student2 table whose rollno is same as that in Student1 table and having location as chennaiDELETE FROM Student2 
WHERE ROLL_NO IN ( SELECT ROLL_NO 
                   FROM Student1 
                   WHERE LOCATION = ’chennai’);
Output:1 row delete successfully.
Display Student2 table:NAMEROLL_NOLOCATIONPHONE_NUMBERSai112mumbai6565656565Sri113coimbatore7878787878
DELETE FROM Student2 
WHERE ROLL_NO IN ( SELECT ROLL_NO 
                   FROM Student1 
                   WHERE LOCATION = ’chennai’);

Output:
1 row delete successfully.

Display Student2 table:
To update name of the students to geeks in Student2 table whose location is same as Raju,Ravi in Student1 tableUPDATE Student2 
SET NAME=’geeks’ 
WHERE LOCATION IN ( SELECT LOCATION 
                    FROM Student1 
                    WHERE NAME IN (‘Raju’,’Ravi’));
Output:1 row updated successfully.
Display Student2 table:NAMEROLL_NOLOCATIONPHONE_NUMBERSai112mumbai6565656565geeks113coimbatore7878787878
UPDATE Student2 
SET NAME=’geeks’ 
WHERE LOCATION IN ( SELECT LOCATION 
                    FROM Student1 
                    WHERE NAME IN (‘Raju’,’Ravi’));

Output:
1 row updated successfully.

Display Student2 table:
This article is contributed by RanjaniRavi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
CTE in SQL
SQL Trigger | Student Database
How to Update Multiple Columns in Single Update Statement in SQL?
Difference between DDL and DML in DBMS
Difference between SQL and NoSQL
SQL | GROUP BY
SQL | Views
Difference between DELETE, DROP and TRUNCATE
MySQL | Group_CONCAT() Function
Difference between DELETE and TRUNCATE