Nested subqueries, also known as subqueries within subqueries, provide a powerful way to solve complex problems by allowing hierarchical query execution.
Nested subqueries are SQL queries embedded inside another subquery or query. They allow breaking down complex problems into smaller, manageable parts. The innermost subquery is executed first, and its result is used by the outer query.
SELECT
, WHERE
, or FROM
clauses.
SELECT column1
FROM table1
WHERE column2 OPERATOR (
SELECT column3
FROM table2
WHERE column4 OPERATOR (
SELECT column5
FROM table3
)
);
Return a single value to the outer query.
Example: Find employees earning more than the average salary in their department:
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
WHERE department_id = (
SELECT department_id
FROM departments
WHERE name = 'HR'
)
);
Return multiple rows to the outer query, often used with IN
, ANY
, or ALL
.
Example: Find employees whose salaries are greater than any manager’s salary
SELECT name, salary
FROM employees
WHERE salary > ANY (
SELECT salary
FROM employees
WHERE role = 'Manager'
);
Contain more than two levels of subqueries.
Example: Find products sold in stores located in cities with populations greater than the average population of cities in each state
SELECT product_name
FROM products
WHERE store_id IN (
SELECT store_id
FROM stores
WHERE city_id IN (
SELECT city_id
FROM cities
WHERE population > (
SELECT AVG(population)
FROM cities
WHERE state_id = cities.state_id
)
)
);
Find employees with salaries higher than the company average:
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
);
Name | Salary |
---|---|
Alice | 75000 |
Bob | 90000 |
Find products with sales exceeding the average sales in their category
SELECT product_name
FROM products
WHERE sales > (
SELECT AVG(sales)
FROM products
WHERE category_id IN (
SELECT category_id
FROM categories
WHERE name = 'Electronics'
)
);
EXISTS
Check if a product has been sold in a region with above-average revenue:
SELECT product_name
FROM products p
WHERE EXISTS (
SELECT 1
FROM sales s
WHERE s.product_id = p.product_id
AND s.region_id IN (
SELECT region_id
FROM regions
WHERE revenue > (
SELECT AVG(revenue)
FROM regions
)
)
);
Product_Name |
---|
Laptop |
Smartphone |
Execution Overhead: Nested subqueries can significantly slow performance due to multiple executions.
Indexes: Indexing columns used in nested subqueries can improve speed.
Join Alternatives: Rewrite nested subqueries as joins for better optimization when possible.
EXISTS
, IN
, or ANY
as appropriate for clarity and performance.Multiple Rows Error: Ensure single-row subqueries return only one result to avoid errors.
Complexity: Overly nested queries can be difficult to debug and maintain.
Performance Issues: Large datasets and multiple levels of nesting can cause significant delays.
Nested subqueries are a robust tool for solving complex SQL problems. By encapsulating queries within queries, you can dynamically evaluate data relationships and perform hierarchical analysis. However, while powerful, nested subqueries must be used judiciously to avoid performance bottlenecks and maintain code clarity. Happy Coding!❤️