SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

SQL SubQuery

The Sub-query in the SQL is the inner query placed or positioned inside another query, which is also known is the outer query. The inner query is embedded in the Where clause of the SQL Query.

There are certain rules for executing the Subqueries in the SQL are as follows:

  1. We can use SQL Sub-query along with SELECT Statement, UPDATE Statement, INSERT Statement and DELETE Statement in the SQL statement.
  2. When we use Subqueries in the SQL statement, the nested inner query will get executed first, then the outer query, which is the main query executed at the end.
  3. A comparison operator can also be used.
  4. The inner query is closed within the parenthesis, and the inner query is placed on the right side of the comparison operator.
  5. You cannot use the ORDER BY clause in the Sub-query but can use the GROUP BY clause.
  6. The Sub-query with FROM clause can be used, WHERE clause, and HAVING clause.

Let's understand the SQL Sub-Query with the help of examples.

Consider the already existing table, which has the following data:

Table Number 1: - D_Students

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845
202226Shwetali Bhagwat908085889080861
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881

Table Number 2: Department

Department_IdDepartment_Name
1Computer Engineering
2Information Technology
3Mechanical Engineering
4Automobile Engineering
5Civil Engineering
6Electrical Engineering
7Electronics and Telecommunication Engineering
8Chemical Engineering

1. Subquery with the SELECT Statement

The SELECT Statement is used with the subqueries to display the data from the tables.

The syntax of the Subquery with the SELECT statement is as follows:

SELECT * FROM Table_Name WHERE Column_Name Expression Operator (SELECT * FROM Table_Name);

Example 1: Execute a query to display the student's information where student department names are 'Computer Engineering', 'Information Technology', and 'Automobile Engineering'. 

SELECT * FROM D_Students WHERE Department_Id IN (SELECT Department_Id FROM Department WHERE Department_Name IN ('Computer Engineering', 'Information Technology','Automobile Engineering'));

In the above query example, we display the student's information where student department names are 'Computer Engineering', 'Information Technology', and 'Automobile Engineering'.

First, SELECT Department_Id FROM Department WHERE Department_Name IN ('Computer Engineering', 'Information Technology', 'Automobile Engineering'); gets executed, and the output of this inner query is

Department_Id
1
2
4

As the inner query output is calculated, the output is represented as input for the main query. The main query is executed as SELECT * FROM D_Students WHERE Department_Id IN (1, 2, 4);

The output of the above query is as follows:

SQL SubQuery
Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202115Axar Patel858082869284851
202223Anushka Sen707571748078751
202226Shwetali Bhagwat908085889080861
202230Mayur Jain808887909290881
202112Vaibhav Lokhande859092808582862
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202229Manthan Koli857584788280812
202114Sonali Patole959092889290914
202220Prajwal Lokhande808585757880814
202224Aakash Jain807572748580784
202227Mayuri Wagh808085808285824
SQL SubQuery

Example 2: Execute a query to display the student’s information where the student’s department name is ‘Computer Engineering’

SELECT * FROM D_Students WHERE Department_Id = (SELECT Department_Id FROM Department WHERE Department_Name = 'Computer Engineering');

We display the student's information where the department name is 'Computer Engineering' in the above query. The query execution is the same as explained in the above example.

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202115Axar Patel858082869284851
202223Anushka Sen707571748078751
202226Shwetali Bhagwat908085889080861
202230Mayur Jain808887909290881
SQL SubQuery

2. Subquery with the UPDATE Statement

The UPDATE statement is used with the subqueries to modify the data.

The Syntax of the Subquery with the UPDATE statement is as follows:

UPDATE Table_Name SET Column_Name = Values WHERE Column_Name Conditions operator (SELECT * FROM Table_Name WHERE Column_Name Conditions);

Example 1: Execute a query to modify the student's information where the student's first-semester percentage is greater than 85, and the department name is 'Computer Engineering'.

UPDATE D_Students SET DEpartment_Id = '6' WHERE First_Sem > 85 AND Department_Id = (SELECT Department_Id FROM Department WHERE Department_Name = 'Computer Engineering');

In the above query, we are modifying the student information of those students where the first-semester percentage is greater than 85, and the department name is 'Computer Engineering’.

First, SELECT Department_Id FROM Department WHERE Department_Name = 'Computer Engineering'; gets executed, and the output of this inner query is

Department_Id
1

As the inner query output is calculated, the output is represented as input for the main query. The main query is executed as UPDATE D_Students SET Department_Id = 6 WHERE First_Sem > 85 AND Department_Id = 1;

We will execute the SELECT statement to check whether the data is modified or not.

SELECT * FROM D_Students;

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL SubQuery

Example 2: Execute a query to modify the student's information where the student name starts with the letter 'A' and department names are 'Automobile Engineering' and 'Civil Engineering'.

UPDATE D_Students SET Department_Id = '7' WHERE Student_Name LIKE 'A%' AND Department_Id IN (SELECT Department_Id FROM Department WHERE Department_Name IN ('Automobile Engineering','Civil Engineering'));

In the above query, we modify those students' information where student_name starts with the letter 'A' and department names are 'Automobile Engineering' and 'Civil Engineering'. The query execution is the same as explained in the above example.

We will execute the SELECT statement to check whether the data is modified or not.

SELECT * FROM D_Students;

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592916
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485857
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580787
202225Akshay Agarwal858078889082847
202226Shwetali Bhagwat908085889080866
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL SubQuery

3. Subquery with the DELETE Statement

The DELETE statement is used with the subqueries to remove data from the table.

The Syntax of the Subquery with the DELETE statement is as follows:

DELETE FROM Table_Name WHERE Column_Name Conditions operator (SELECT * FROM Table_Name WHERE Column_Name Conditions);

Example: Execute a query to remove the student's information where department names are 'Civil Engineering’, ‘Electrical Engineering’, and ‘Electronics and Telecommunication Engineering’.

DELETE FROM D_Students WHERE Department_Id IN (SELECT Department_Id FROM Department WHERE Department_Name IN ('Civil Engineering', 'Electrical Engineering', 'Electronics and Telecommunication Engineering'));

In the above query, we removed the student's information from the D_Students table where department names are 'Civil Engineering’, ‘Electrical Engineering’, and ‘Electronics and Telecommunication Engineering’.

First, SELECT Department_Id FROM Department WHERE Department_Name IN ('Civil Engineering', 'Electrical Engineering', 'Electronics and Telecommunication Engineering'; gets executed, and the output of this inner query is

Department_Id
5
6
7

As the inner query output is calculated, the output is represented as input for the main query. The main query is executed as DELETE FROM D_Students WHERE Department_Id IN (5, 6, 7);

The main query removes the student’s information where department id is 5, 6, and 7 from the D_Students table.

We will execute the SELECT statement to check whether the data is removed or not.

SELECT * FROM D_Students;

The output of the above query is as follows:

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202227Mayuri Wagh808085808285824
202229Manthan Koli857584788280812
202230Mayur Jain808887909290881
SQL SubQuery