SQL Subquery (original) (raw)

Last Updated : 11 Jun, 2026

A subquery in SQL is a query nested inside another SQL query. It allows complex filtering, aggregation and data manipulation by using the result of one query inside another. They are an essential tool when we need to perform operations like:

**Example: First, we create a demo SQL database and tables, on which we use the SQL Subqueries.

student-

Output

**Query:

SELECT * FROM Students
WHERE Score > ( SELECT AVG(Score) FROM Students );

**Output:

output-01

Output

**Syntax:

SELECT column_name
FROM table_name
WHERE column_name operator
(SELECT column_name
FROM table_name
WHERE condition);

**Note: Subqueries do not have a single fixed syntax, as they can be used in different clauses like SELECT, WHERE, FROM and HAVING

SQL Clauses for Subqueries

Clauses that can be used with subqueries are:

Types of Subqueries

Consider the following two tables for examples:

Screenshot-2025-11-14-142408

Employees Table

Screenshot-2025-11-14-142425

Departments Table

1. Single-Row Subquery

A single-row subquery is a subquery that returns only one value.

**Example:

SELECT * FROM Employees
WHERE Salary = (SELECT MAX(Salary) FROM Employees);

**Output:

Screenshot-2025-11-14-142445

Output

2. Multi-Row Subquery

A multi-row subquery is a subquery that returns more than one value.

**Example:

SELECT * FROM Employees
WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Location = 'New York');

**Output:

Screenshot-2025-11-14-142500

Output

A correlated subquery is a subquery that depends on the outer query for its values.

**Example:

SELECT e.Name, e.Salary
FROM Employees e
WHERE e.Salary > (SELECT AVG(Salary)
FROM Employees
WHERE DepartmentID = e.DepartmentID);

**Output:

Screenshot-2025-11-14-142525

Output

Examples of Using SQL Subqueries

These examples showcase how subqueries can be used for various operations like selecting, updating, deleting or inserting data, providing insights into their syntax and functionality. Through these examples, we will understand flexibility and importance of subqueries in simplifying complex database tasks.

Consider the following two tables:

stu-info

Student_Info

student-section

Student_Section

**Example 1: Fetching Data Using Subquery in WHERE Clause

This example demonstrates how to use a subquery inside the WHERE clause. The inner query retrieves roll numbers of students who belong to section 'A' and the outer query fetches their corresponding details (name, location and phone number) from the Student table.

**Query:

SELECT NAME, LOCATION, PHONE_NUMBER
FROM Student_Info
WHERE ROLL_NO IN (
SELECT ROLL_NO
FROM Student_Section
WHERE SECTION = 'A'
);

**Output:

sophia

Output

**Example 2: Using Subquery with DELETE

In this example, we use a subquery with DELETE to remove certain rows from the Student table. Instead of hardcoding roll numbers, the subquery finds them based on conditions.

**Query:

DELETE FROM Student_Info
WHERE ROLL_NO IN (
SELECT ROLL_NO FROM (
SELECT ROLL_NO FROM Student_Info WHERE ROLL_NO <= 101 OR ROLL_NO = 201
) AS temp
);

**Output:

filter_students

Output

**Example 3: Using Subquery with UPDATE

Subqueries can also be used with UPDATE. In this example, we update student names to "Geeks" if their location matches the result of a subquery.

**Query:

UPDATE Student_Info
SET NAME = 'Geeks'
WHERE LOCATION IN (
SELECT LOCATION
FROM Student_Info
WHERE LOCATION IN ('London', 'Berlin')
);

**Output:

geeks

Output

**Example 4: Simple Subquery in the FROM Clause

This example demonstrates using a subquery inside the FROM clause, where the subquery acts as a temporary (derived) table.

**Query:

SELECT NAME, PHONE_NUMBER
FROM (
SELECT NAME, PHONE_NUMBER, LOCATION
FROM Student_Info
WHERE LOCATION LIKE 'T%'
) AS subquery_table;

**Output:

Location

Output

**Example 5: Subquery with JOIN

We can also use subqueries along with JOIN to connect data across tables.

**Query:

SELECT s.NAME, s.LOCATION, ns.SECTION
FROM Student_Info s
INNER JOIN (
SELECT ROLL_NO, SECTION
FROM Student_Section
WHERE SECTION = 'A'
) ns
ON s.ROLL_NO = ns.ROLL_NO;

**Output:

Sydney

Output