A SQL Subquery (also called an Inner Query or Nested Query) is a query written inside another SQL query.
The inner query executes first, and its result is then used by the outer query.
Subqueries make SQL more powerful by allowing you to retrieve data based on the result of another query. SQL Subqueries practice questions with solutions help to understand the concepts.
For example, suppose you want to find students who scored more than the average marks.
Instead of calculating the average manually, SQL can do it automatically using a subquery.
SELECT student_name,
marks
FROM students
WHERE marks >
(
SELECT AVG(marks)
FROM students
);
This query first calculates the average marks and then displays only students whose marks are higher than that average.
Why Use SQL Subqueries?
Subqueries help you:
- Retrieve dynamic values
- Avoid writing multiple queries
- Perform advanced filtering
- Build analytical reports
- Solve interview-level SQL problems
- Improve query flexibility
They are commonly used in:
- Banking Systems
- HR Management
- Sales Reports
- E-commerce Applications
- Data Analytics
- Business Intelligence
Types of SQL Subqueries
| Type | Description |
|---|---|
| Single-row Subquery | Returns only one value |
| Multiple-row Subquery | Returns multiple values |
| Correlated Subquery | Executes once for every row of the outer query |
| Nested Subquery | Contains another subquery inside it |
Sample Table Used Throughout This Chapter
students
| student_id | student_name | course | marks |
|---|---|---|---|
| 101 | Rahul | Python | 88 |
| 102 | Amit | Java | 91 |
| 103 | Neha | SQL | 95 |
| 104 | Priya | Python | 84 |
| 105 | Rohit | Java | 90 |
| 106 | Ankit | Python | 82 |
| 107 | Sneha | SQL | 89 |
1. SQL Query to Display Students Scoring Above Average Marks
Problem Statement
The examination department wants to identify students whose marks are higher than the average marks of all students.
Write an SQL query using a subquery.
SQL Solution
SELECT student_name,
marks
FROM students
WHERE marks >
(
SELECT AVG(marks)
FROM students
);
Sample Output
| student_name | marks |
|---|---|
| Amit | 91 |
| Neha | 95 |
| Rohit | 90 |
Explanation
The subquery:
SELECT AVG(marks)
FROM students;
calculates the average marks.
The outer query then compares every student’s marks with this average and returns only those students who scored higher.
Concepts Covered
- Single-row Subquery
- AVG()
- WHERE Clause
2. SQL Query to Display Students Having the Highest Marks
Problem Statement
The principal wants to display the student(s) who scored the highest marks.
Write an SQL query using a subquery.
SQL Solution
SELECT student_name,
marks
FROM students
WHERE marks =
(
SELECT MAX(marks)
FROM students
);
Sample Output
| student_name | marks |
|---|---|
| Neha | 95 |
Explanation
The subquery finds the highest marks.
SELECT MAX(marks)
FROM students;
The outer query returns the student whose marks match this value.
If multiple students have the same highest marks, SQL returns all of them.
Concepts Covered
- MAX()
- Single-row Subquery
- WHERE Clause
3. SQL Query to Display Students Enrolled in the Same Course as Rahul
Problem Statement
The institute wants to display all students who are enrolled in the same course as Rahul.
Write an SQL query using a subquery.
SQL Solution
SELECT student_name,
course
FROM students
WHERE course =
(
SELECT course
FROM students
WHERE student_name = 'Rahul'
);
Sample Output
| student_name | course |
|---|---|
| Rahul | Python |
| Priya | Python |
| Ankit | Python |
Explanation
The subquery first determines Rahul’s course.
SELECT course
FROM students
WHERE student_name = 'Rahul';
The outer query then displays every student enrolled in that course.
This type of query is commonly used in:
- Student Management Systems
- HR Databases
- CRM Applications
Concepts Covered
- Single-row Subquery
- Dynamic Filtering
- WHERE Clause
4. SQL Query to Display Employees Earning More Than the Average Salary
Problem Statement
An HR manager wants to identify employees whose salary is higher than the company’s average salary.
Write an SQL query using a subquery.
Sample Table
employees
| employee_id | employee_name | department | salary |
|---|---|---|---|
| 101 | Aman | HR | 45000 |
| 102 | Riya | IT | 70000 |
| 103 | Vikas | Finance | 85000 |
| 104 | Neha | HR | 50000 |
| 105 | Karan | IT | 60000 |
| 106 | Simran | Marketing | 90000 |
SQL Solution
SELECT
employee_name,
department,
salary
FROM employees
WHERE salary >
(
SELECT AVG(salary)
FROM employees
);
Sample Output
| employee_name | department | salary |
|---|---|---|
| Riya | IT | 70000 |
| Vikas | Finance | 85000 |
| Simran | Marketing | 90000 |
Explanation
The subquery:
SELECT AVG(salary)
FROM employees;
calculates the average salary.
The outer query returns employees earning more than the average salary.
This type of query is commonly used in:
- HR Dashboards
- Payroll Reports
- Salary Analysis
- Performance Reviews
Concepts Covered
- AVG()
- Single-row Subquery
- Employee Database
5. SQL Query to Display Products Costing More Than the Average Product Price
Problem Statement
An e-commerce company wants to identify products that are more expensive than the average product price.
Write an SQL query using a subquery.
Sample Table
products
| product_id | product_name | category | price |
|---|---|---|---|
| 201 | Laptop | Electronics | 65000 |
| 202 | Mouse | Accessories | 700 |
| 203 | Keyboard | Accessories | 1200 |
| 204 | Monitor | Electronics | 15000 |
| 205 | Headphones | Accessories | 2500 |
| 206 | Printer | Electronics | 18000 |
SQL Solution
SELECT
product_name,
category,
price
FROM products
WHERE price >
(
SELECT AVG(price)
FROM products
);
Sample Output
| product_name | category | price |
|---|---|---|
| Laptop | Electronics | 65000 |
| Monitor | Electronics | 15000 |
| Printer | Electronics | 18000 |
Explanation
The subquery first computes the average price of all products.
SELECT AVG(price)
FROM products;
The outer query compares each product’s price with the average and returns only those products priced above it.
This query is useful for:
- Pricing Analysis
- Inventory Reports
- Product Segmentation
- Sales Dashboards
Concepts Covered
- AVG()
- Single-row Subquery
- Product Database
6. SQL Query to Display Employees Working in Departments Located in Delhi
Problem Statement
A company stores employee details and department details in separate tables.
The HR manager wants to display employees who work in departments located in Delhi.
Write an SQL query using a multi-row subquery.
Sample Tables
employees
| employee_id | employee_name | department_id |
|---|---|---|
| 101 | Aman | 1 |
| 102 | Riya | 2 |
| 103 | Vikas | 3 |
| 104 | Neha | 1 |
| 105 | Karan | 4 |
departments
| department_id | department_name | city |
|---|---|---|
| 1 | HR | Delhi |
| 2 | IT | Noida |
| 3 | Finance | Delhi |
| 4 | Marketing | Gurgaon |
SQL Solution
SELECT
employee_name
FROM employees
WHERE department_id IN
(
SELECT department_id
FROM departments
WHERE city = 'Delhi'
);
Sample Output
| employee_name |
|---|
| Aman |
| Vikas |
| Neha |
Explanation
The inner query returns all department IDs located in Delhi.
SELECT department_id
FROM departments
WHERE city='Delhi';
Result:
1
3
The outer query displays employees whose department_id is 1 or 3.
Concepts Covered
- Multi-row Subquery
- IN Operator
- WHERE Clause
7. SQL Query to Display Products Belonging to Premium Categories
Problem Statement
An online store wants to display products that belong to Premium categories.
Sample Tables
products
| product_id | product_name | category_id |
|---|---|---|
| 201 | Laptop | 1 |
| 202 | Keyboard | 2 |
| 203 | Gaming Chair | 3 |
| 204 | Monitor | 1 |
| 205 | Mouse | 2 |
categories
| category_id | category_type |
|---|---|
| 1 | Premium |
| 2 | Standard |
| 3 | Premium |
SQL Solution
SELECT
product_name
FROM products
WHERE category_id IN
(
SELECT category_id
FROM categories
WHERE category_type = 'Premium'
);
Sample Output
| product_name |
|---|
| Laptop |
| Gaming Chair |
| Monitor |
Explanation
The inner query finds all Premium category IDs.
The outer query returns products whose category belongs to those IDs.
Concepts Covered
- IN Operator
- Multi-row Subquery
- Product Database
8. SQL Query to Display Students Enrolled in Popular Courses
Problem Statement
A training institute marks some courses as Popular.
Display students enrolled in those courses.
Sample Tables
students
| student_id | student_name | course_id |
|---|---|---|
| 101 | Rahul | 1 |
| 102 | Neha | 2 |
| 103 | Amit | 3 |
| 104 | Sneha | 1 |
courses
| course_id | course_name | category |
|---|---|---|
| 1 | Python | Popular |
| 2 | Java | Regular |
| 3 | SQL | Popular |
SQL Solution
SELECT
student_name
FROM students
WHERE course_id IN
(
SELECT course_id
FROM courses
WHERE category = 'Popular'
);
Sample Output
| student_name |
|---|
| Rahul |
| Amit |
| Sneha |
Explanation
The subquery returns the IDs of Popular courses.
The outer query displays students enrolled in those courses.
Concepts Covered
- IN
- Multi-row Subquery
- Student Database
9. SQL Query to Display Customers Who Purchased Electronic Products
Problem Statement
An online shopping company wants to identify customers who purchased products from the Electronics category.
Sample Tables
orders
| order_id | customer_name | product_id |
|---|---|---|
| 5001 | Rahul | 101 |
| 5002 | Neha | 102 |
| 5003 | Amit | 103 |
products
| product_id | product_name | category |
|---|---|---|
| 101 | Laptop | Electronics |
| 102 | Keyboard | Accessories |
| 103 | Monitor | Electronics |
SQL Solution
SELECT
customer_name
FROM orders
WHERE product_id IN
(
SELECT product_id
FROM products
WHERE category = 'Electronics'
);
Sample Output
| customer_name |
|---|
| Rahul |
| Amit |
Explanation
The inner query identifies products that belong to the Electronics category.
The outer query displays customers who purchased those products.
Concepts Covered
- Multi-row Subquery
- IN
- E-commerce Database
10. SQL Query to Display Books Written by Award-Winning Authors
Problem Statement
A library wants to display books written by authors who have won literary awards.
Sample Tables
books
| book_id | book_title | author_id |
|---|---|---|
| 201 | Python Basics | 1 |
| 202 | Master SQL | 2 |
| 203 | Java Guide | 3 |
| 204 | Advanced SQL | 2 |
authors
| author_id | author_name | award_winner |
|---|---|---|
| 1 | Ravi Kumar | No |
| 2 | Anjali Sharma | Yes |
| 3 | Mohit Verma | No |
SQL Solution
SELECT
book_title
FROM books
WHERE author_id IN
(
SELECT author_id
FROM authors
WHERE award_winner = 'Yes'
);
Sample Output
| book_title |
|---|
| Master SQL |
| Advanced SQL |
Explanation
The subquery returns all award-winning author IDs.
The outer query displays books written by those authors.
Concepts Covered
- Multi-row Subquery
- IN Operator
- Library Database
11. SQL Query Using EXISTS to Display Customers Who Have Placed Orders
Problem Statement
An online shopping company wants to display only those customers who have placed at least one order.
Write an SQL query using the EXISTS operator.
Sample Tables
customers
| customer_id | customer_name |
|---|---|
| 1 | Rahul |
| 2 | Neha |
| 3 | Amit |
| 4 | Priya |
orders
| order_id | customer_id | amount |
|---|---|---|
| 5001 | 1 | 1200 |
| 5002 | 2 | 2500 |
| 5003 | 1 | 900 |
SQL Solution
SELECT
customer_name
FROM customers c
WHERE EXISTS
(
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);
Sample Output
| customer_name |
|---|
| Rahul |
| Neha |
Explanation
The EXISTS operator checks whether the subquery returns at least one row.
For each customer:
- If an order exists → the customer is displayed.
- If no order exists → the customer is ignored.
Unlike IN, EXISTS is often preferred for large datasets because it stops searching after finding the first matching record.
Concepts Covered
- EXISTS
- Correlated Subquery
- Customer Database
12. SQL Query Using NOT EXISTS to Display Customers Without Orders
Problem Statement
The marketing team wants to identify customers who have never placed an order.
Write an SQL query using NOT EXISTS.
SQL Solution
SELECT
customer_name
FROM customers c
WHERE NOT EXISTS
(
SELECT 1
FROM orders o
WHERE o.customer_id = c.customer_id
);
Sample Output
| customer_name |
|---|
| Amit |
| Priya |
Explanation
The NOT EXISTS operator returns rows where the subquery finds no matching records.
This query is commonly used for:
- Customer Retention Campaigns
- Inactive User Reports
- Email Marketing
- Sales Analysis
Concepts Covered
- NOT EXISTS
- Correlated Subquery
- Customer Analytics
13. SQL Correlated Subquery to Display Employees Earning More Than Their Department Average
Problem Statement
A company wants to identify employees whose salary is higher than the average salary of their own department.
Write an SQL query using a correlated subquery.
Sample Table
employees
| employee_id | employee_name | department | salary |
|---|---|---|---|
| 1 | Aman | HR | 45000 |
| 2 | Neha | HR | 60000 |
| 3 | Riya | IT | 70000 |
| 4 | Vikas | IT | 85000 |
| 5 | Karan | IT | 50000 |
SQL Solution
SELECT
e1.employee_name,
e1.department,
e1.salary
FROM employees e1
WHERE salary >
(
SELECT AVG(e2.salary)
FROM employees e2
WHERE e1.department = e2.department
);
Sample Output
| employee_name | department | salary |
|---|---|---|
| Neha | HR | 60000 |
| Vikas | IT | 85000 |
Explanation
This is a correlated subquery because the inner query depends on each row of the outer query.
For every employee:
- SQL calculates the average salary of that employee’s department.
- It compares the employee’s salary with the department average.
- Employees earning more than the average are returned.
Concepts Covered
- Correlated Subquery
- AVG()
- Department Analysis
14. SQL Subquery in the FROM Clause
Problem Statement
A company wants to calculate the average salary of employees whose salary is greater than ₹50,000.
Write an SQL query using a subquery in the FROM clause.
SQL Solution
SELECT
AVG(salary) AS average_salary
FROM
(
SELECT salary
FROM employees
WHERE salary > 50000
) AS high_salary;
Sample Output
| average_salary |
|---|
| 71666.67 |
Explanation
The inner query creates a temporary table named high_salary.
The outer query calculates the average salary from that temporary result.
This technique is useful when complex filtering needs to be performed before aggregation.
Concepts Covered
- FROM Subquery
- Aggregate Functions
- Temporary Result Set
15. SQL Subquery in the SELECT Clause
Problem Statement
The HR department wants to display every employee along with the overall average company salary.
Write an SQL query using a subquery in the SELECT clause.
SQL Solution
SELECT
employee_name,
salary,
(
SELECT AVG(salary)
FROM employees
) AS company_average_salary
FROM employees;
Sample Output
| employee_name | salary | company_average_salary |
|---|---|---|
| Aman | 45000 | 62000 |
| Neha | 60000 | 62000 |
| Riya | 70000 | 62000 |
| Vikas | 85000 | 62000 |
| Karan | 50000 | 62000 |
Explanation
The subquery calculates the overall average salary once.
That value is displayed alongside every employee record.
This approach is useful in:
- HR Reports
- Business Dashboards
- Salary Comparisons
- Performance Analysis
Concepts Covered
- SELECT Subquery
- Aggregate Functions
- Reporting Queries
Chapter Summary
In this chapter, you learned how to use SQL Subqueries to solve problems where one query depends on the result of another query.
A subquery is simply a query inside another SQL query. It helps you write dynamic, flexible, and powerful SQL statements without manually calculating intermediate values.
Throughout this chapter, you practiced:
- Single-row subqueries using aggregate functions
- Multi-row subqueries with the
INoperator - Correlated subqueries
- Using the
EXISTSoperator - Using the
NOT EXISTSoperator - Subqueries inside the
SELECTclause - Subqueries inside the
FROMclause
These techniques are widely used in real-world database applications for reporting, filtering, and business analytics.
Key Takeaways
- A subquery is a query inside another SQL query.
- The inner query executes before the outer query.
- Single-row subqueries return one value.
- Multi-row subqueries return multiple values.
- Use
INwhen the subquery returns multiple rows. - Use
EXISTSto check whether matching records exist. - Use
NOT EXISTSto find missing records. - Correlated subqueries execute once for each row of the outer query.
- Subqueries can be written inside the
SELECT,FROM, andWHEREclauses. - Subqueries are frequently used in SQL interviews and business reporting.
Frequently Asked Questions (FAQs)
1. What is a SQL Subquery?
A SQL subquery is a query written inside another SQL query.
Example:
SELECT student_name,
marks
FROM students
WHERE marks >
(
SELECT AVG(marks)
FROM students
);
2. What is the difference between a single-row subquery and a multi-row subquery?
| Single-row Subquery | Multi-row Subquery |
|---|---|
| Returns one value | Returns multiple values |
Often used with =, <, > | Often used with IN, ANY, ALL |
3. When should I use the IN operator with a subquery?
Use IN when the inner query returns multiple values.
Example:
SELECT employee_name
FROM employees
WHERE department_id IN
(
SELECT department_id
FROM departments
WHERE city = 'Delhi'
);
4. What is a correlated subquery?
A correlated subquery depends on the current row of the outer query and executes once for every row.
Example:
SELECT employee_name
FROM employees e1
WHERE salary >
(
SELECT AVG(e2.salary)
FROM employees e2
WHERE e1.department = e2.department
);
5. What is the difference between EXISTS and IN?
INcompares values returned by a subquery.EXISTSchecks whether the subquery returns at least one matching row.
EXISTS is generally more efficient when working with large datasets because it stops searching after finding the first match.
6. Can a subquery be written inside the SELECT clause?
Yes.
Example:
SELECT
employee_name,
salary,
(
SELECT AVG(salary)
FROM employees
) AS company_average
FROM employees;
7. Can a subquery be written inside the FROM clause?
Yes.
Example:
SELECT AVG(salary)
FROM
(
SELECT salary
FROM employees
WHERE salary > 50000
) AS high_salary;
8. Where are SQL subqueries used in real-world applications?
SQL subqueries are commonly used in:
- Banking Systems
- HR Management
- Inventory Management
- Student Portals
- CRM Software
- E-commerce Platforms
- Business Intelligence Dashboards
- Financial Reporting
- Sales Analytics
- Data Warehousing
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
