SQL Subquery

The Subquery or Inner query is an SQL query placed inside another SQL query. It is embedded in the HAVING or WHERE clause of the SQL statements.

Following are the important rules which must be followed by the SQL Subquery:

1. The SQL subqueries can be used with the following statements along with the SQL expression operators:

  • SELECT statement,
  • UPDATE statement,
  • INSERT statement, and
  • DELETE statement.

2. The subqueries in SQL are always enclosed in the parenthesis and placed on the right side of the SQL operators.

3. We cannot use the ORDER BY clause in the subquery. But, we can use the GROUP BY clause, which performs the same function as the ORDER BY clause.

4. If the subquery returns more than one record, we have to use the multiple value operators before the Subquery.

5. We can use the BETWEEN operator within the subquery but not with the subquery.

Subquery with SELECT statement

In SQL, inner queries or nested queries are used most frequently with the SELECT statement. The syntax of Subquery with the SELECT statement is described in the following block:

Examples of Subquery with the SELECT Statement

Example 1: This example uses the Greater than comparison operator with the Subquery.

Let's take the following table named Student_Details, which contains Student_RollNo., Stu_Name, Stu_Marks, and Stu_City column.

Student_RollNo.Stu_NameStu_MarksStu_City
1001Akhil85Agra
1002Balram78Delhi
1003Bheem87Gurgaon
1004Chetan95Noida
1005Diksha99Agra
1006Raman90Ghaziabad
1007Sheetal68Delhi

The following SQL query returns the record of those students whose marks are greater than the average of total marks:

Output:

Student_RollNo.Stu_NameStu_MarksStu_City
1003Bheem87Gurgaon
1004Chetan95Noida
1005Diksha99Agra
1006Raman90Ghaziabad

Example 2: This example uses the IN operator with the subquery.

Let's take the following two tables named Faculty_Details and Department tables. The Faculty_Details table contains ID, Name, Dept_ID, and address of faculties. And, the Department table contains the Dept_ID, Faculty_ID, and Dept_Name.

Faculty_IDNameDept_IDAddress
101Bheem1Gurgaon
102Chetan2Noida
103DikshaNULLAgra
104Raman4Ghaziabad
105Yatin3Noida
106AnujNULLAgra
107Rakes5Gurgaon
Dept_IDFaculty_IDDept_Name
1101BCA
2102B.Tech
3105BBA
4104MBA
5107MCA

Output:

Dept_IDFaculty_IDDept_Name
1101BCA
2102B.Tech
3105BBA
5107MCA

Subquery with the INSERT statement

We can also use the subqueries and nested queries with the INSERT statement in Structured Query Language. We can insert the results of the subquery into the table of the outer query. The syntax of Subquery with the INSERT statement is described in the following block:

Examples of Subquery with the INSERT Statement

Example1: This example inserts the record of one table into another table using subquery with WHERE clause.

Let's take Old_Employee and New_Employee tables. The Old_Employee and New_Employee table contain the same number of columns. But, both the tables contain different records.

Emp_IDEmp_NameEmp_SalaryAddress
1001Akhil50000Agra
1002Balram25000Delhi
1003Bheem45000Gurgaon
1004Chetan60000Noida
1005Diksha30000Agra
1006Raman50000Ghaziabad
1007Sheetal35000Delhi

Table: Old_Employee

Emp_IDEmp_NameEmp_SalaryAddress
1008Sumit50000Agra
1009Akash55000Delhi
1010Devansh65000Gurgaon

Table: New_Employee

The New_Employee contains the details of new employees. If you want to move the details of those employees whose salary is greater than 40000 from the Old_Employee table to the New_Employee table. Then for this issue, you have to type the following query in SQL:

Now, you can check the details of the updated New_Employee table by using the following SELECT query:

Output:

Emp_IDEmp_NameEmp_SalaryAddress
1008Sumit50000Agra
1009Akash55000Delhi
1010Devansh65000Gurgaon
1001Akhil50000Agra
1003Bheem45000Gurgaon
1004Chetan60000Noida
1006Raman50000Ghaziabad

Table: New_Employee

Example 2: This example describes how to use ANY operator with subquery in the INSERT Statement.

Here we have taken the New_Employee, old_Employee, and Department table.

The data of the New_Employee table is shown in the following table:

Emp_IDEmp_NameEmp_SalaryDept_ID
1008Sumit50000401

Table: New_Employee

The data of the old_Employee table is shown in the below table:

Emp_IDEmp_NameEmp_SalaryDept_ID
1001Akhil50000404
1002Balram25000403
1003Bheem45000405
1004Chetan60000402
1005Ram65000407
1006Shyam55500NULL
1007Shobhit60000NULL

Table: Old_Employee

The data of Department table is shown in the below table:
Dept_IDDept_NameEmp_ID
401Administration1008
402HR1004
403Testing1002
404Coding1001
405Sales1003
406MarketingNULL
407Accounting1005

INSERT INTO New_Employee

SELECT * FROM Old_Employee

WHERE Emp_ID = ANY( SELECT Emp_ID FROM Department WHERE Dept_ID = 407 OR Dept_ID = 406 );

Now, check the details of the New_Employee table by using the following SELECT statement:

Output:

Emp_IDEmp_NameEmp_SalaryDept_ID
1008Sumit50000401
1005Ram65000407

Subquery with the UPDATE statement

The subqueries and nested queries can be used with the UPDATE statement in Structured Query Language for updating the columns of the existing table. We can easily update one or more columns using a subquery with the UPDATE statement.

Syntax of Subquery with the UPDATE statement

Example of Subquery with the UPDATE statement

This example updates the record of one table using the IN operator with Subquery in the UPDATE statement.

Let's take an Employee_Details and Department table.

The data of the Employee_Details table is shown in the following table:

Emp_IDEmp_NameEmp_SalaryDept_ID
1001Akhil50000404
1002Balram25000403
1003Bheem45000405
1004Chetan60000402
1005Ram65000407
1006Shyam55500NULL
1007Shobhit60000NULL

Table: Employee_Details

The data of Department table is shown in the below table:

Dept_IDDept_NameEmp_IDDept_Grade
401Administration1008B
402HR1004A
403Testing1002A
404Coding1001B
405Sales1003A
406MarketingNULLC
407Accounting1005A

The following updates the salary of those employees whose Department Grade is A:

The following query will show the updated data of the Employee_Details table in the output:

Output:

Emp_IDEmp_NameEmp_SalaryDept_ID
1001Akhil50000404
1002Balram30000403
1003Bheem50000405
1004Chetan65000402
1005Ram70000407
1006Shyam55500NULL
1007Shobhit60000NULL

Table: Employee_Details

Subquery with the DELETE statement

We can easily delete one or more records from the SQL table using Subquery with the DELETE statement in Structured Query Language.

Syntax of Subquery with DELETE statement

Example of Subquery with DELETE statement

This example deletes the records from the table using the IN operator with Subquery in the DELETE statement.

Let's take an Employee_Details and Department table.

The data of the Employee_Details table is shown in the following table:

Emp_IDEmp_NameEmp_SalaryDept_ID
1001Akhil50000404
1002Balram25000403
1003Bheem45000405
1004Chetan60000402
1005Ram65000407
1006Shyam55500NULL
1007Shobhit60000NULL
1008Ankit48000401

Table: Employee_Details

The data of Department table is shown in the below table:

Dept_IDDept_NameEmp_IDDept_Grade
401Administration1008C
402HR1004A
403Testing1002C
404Coding1001B
405Sales1003A
406MarketingNULLC
407Accounting1005C

The following query deletes the record of those employees from the Employee_Details whose Department Grade is C:

The following query will show the updated data of the Employee_Details table in the output:

Output:

Emp_IDEmp_NameEmp_SalaryDept_ID
1001Akhil50000404
1003Bheem45000405
1004Chetan60000402
1006Shyam55500NULL
1007Shobhit60000NULL

Table: Employee_Details


Next TopicSQL View




Latest Courses