Skip to content
Related Articles

Related Articles

SQL | Subquery

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 24 Jul, 2022

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 when the subquery doesn’t have any co-relation with the main query, when there is a co-relation the parser takes the decision on the fly on which query to execute on precedence and uses the output of the subquery accordingly.
  • 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

NAMEROLL_NOLOCATIONPHONE_NUMBER
Ram101Chennai9988775566
Raj102Coimbatore8877665544
Sasi103Madurai7766553344
Ravi104Salem8989898989
Sumathi105Kanchipuram8989856868

STUDENT

NAMEROLL_NOSECTION
Ravi104A
Sumathi105B
Raj102A

Sample Queries

:

  • To display NAME, LOCATION, PHONE_NUMBER of the students from DATABASE table whose section is A
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:
NAMEROLL_NOLOCATIONPHONE_NUMBER
Ravi104Salem8989898989
Raj102Coimbatore8877665544
  • Insert Query Example:

Table1: Student1

NAMEROLL_NOLOCATIONPHONE_NUMBER 
Ram101chennai9988773344 
Raju102coimbatore9090909090 
Ravi103salem8989898989 

Table2: Student2

NAMEROLL_NOLOCATIONPHONE_NUMBER 
Raj111chennai8787878787 
Sai112mumbai6565656565 
Sri113coimbatore7878787878 
  • To insert Student2 into Student1 table:
INSERT INTO Student1  SELECT * FROM Student2;
  • Output:
NAMEROLL_NOLOCATIONPHONE_NUMBER 
Ram101chennai9988773344 
Raju102coimbatore9090909090 
Ravi103salem8989898989 
Raj111chennai8787878787 
Sai112mumbai6565656565 
Sri113coimbatore7878787878 
  • To delete students from Student2 table whose rollno is same as that in Student1 table and having location as chennai
DELETE FROM Student2 
WHERE ROLL_NO IN ( SELECT ROLL_NO 
                   FROM Student1 
                   WHERE LOCATION = ’chennai’);
  • Output:
1 row delete successfully.
  • Display Student2 table:
NAMEROLL_NOLOCATIONPHONE_NUMBER 
Sai112mumbai6565656565 
Sri113coimbatore7878787878 
  • To update name of the students to geeks in Student2 table whose location is same as Raju,Ravi in Student1 table
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:
NAMEROLL_NOLOCATIONPHONE_NUMBER 
Sai112mumbai6565656565 
geeks113coimbatore7878787878 

This article is contributed by RanjaniRavi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!