Curriculum
Subqueries are one of the most powerful SQL techniques used in Data Analytics, Business Intelligence, Reporting, and Database Management. A Subquery is a query nested inside another SQL query. Subqueries allow analysts to break complex problems into smaller steps and retrieve information that would otherwise be difficult to obtain using a single query.
In real-world business scenarios, Data Analysts often need to answer questions such as:
These types of analytical questions are commonly solved using Subqueries.
Organizations use Subqueries to:
Mastering Subqueries is essential for Data Analysts, Business Analysts, Database Administrators, and Business Intelligence professionals.
A Subquery is a query written inside another SQL query.
The inner query executes first.
The result is then used by the outer query.
Basic Structure:
SELECT column_name
FROM table_name
WHERE column_name =
(
SELECT column_name
FROM another_table
);
The inner query provides information that helps the outer query generate the final result.
Business analysis often requires comparing data against:
Subqueries make these comparisons possible.
Benefits:
Example:
Find employees earning more than the average salary.
Step 1:
Calculate average salary.
SELECT AVG(Salary)
FROM Employees;
Result:
65000
Step 2:
Find employees above that value.
SELECT EmployeeName,
Salary
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);
The inner query calculates the average salary.
The outer query returns employees earning more than that amount.
The major types of Subqueries include:
Each type serves different analytical purposes.
Single-Row Subqueries return one value.
Example:
SELECT EmployeeName
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);
The inner query returns a single average salary value.
Applications:
Example:
SELECT ProductName,
Revenue
FROM Products
WHERE Revenue =
(
SELECT MAX(Revenue)
FROM Products
);
Result:
Highest revenue product.
Applications:
Example:
SELECT EmployeeName,
Salary
FROM Employees
WHERE Salary =
(
SELECT MIN(Salary)
FROM Employees
);
Applications:
Multiple-Row Subqueries return multiple values.
They are commonly used with:
operators.
Example:
Find customers who placed orders.
SELECT CustomerName
FROM Customers
WHERE CustomerID IN
(
SELECT CustomerID
FROM Orders
);
Result:
Only customers who have placed orders.
Applications:
Example:
Find customers without orders.
SELECT CustomerName
FROM Customers
WHERE CustomerID NOT IN
(
SELECT CustomerID
FROM Orders
);
Result:
Customers who never placed an order.
Applications:
Subqueries can appear inside SELECT.
Example:
SELECT ProductName,
(
SELECT AVG(Revenue)
FROM Products
) AS AverageRevenue
FROM Products;
Applications:
Comparative reporting.
Benefits:
Provides reference metrics.
Subqueries can also be used as temporary tables.
Example:
SELECT *
FROM
(
SELECT ProductName,
Revenue
FROM Products
) AS ProductData;
Benefits:
Improves query organization.
Applications:
Advanced reporting.
Correlated Subqueries depend on values from the outer query.
Example:
SELECT EmployeeName,
Salary
FROM Employees E
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
WHERE Department = E.Department
);
Result:
Employees earning above their department average.
Applications:
Correlated Subqueries are powerful but can be slower than standard queries.
Nested Subqueries contain multiple levels of queries.
Example:
SELECT CustomerName
FROM Customers
WHERE CustomerID IN
(
SELECT CustomerID
FROM Orders
WHERE ProductID IN
(
SELECT ProductID
FROM Products
WHERE Category = 'Electronics'
)
);
Applications:
Advanced customer analysis.
Benefits:
Highly flexible filtering.
Subqueries often work alongside Aggregate Functions.
Example:
SELECT ProductName,
Revenue
FROM Products
WHERE Revenue >
(
SELECT AVG(Revenue)
FROM Products
);
Result:
Products performing above average.
Applications:
Business performance analysis.
Example:
SELECT ProductName,
Revenue
FROM Sales
WHERE Revenue >
(
SELECT AVG(Revenue)
FROM Sales
);
Benefits:
Identify high-performing products.
Applications:
Revenue analysis.
Example:
SELECT CustomerName
FROM Customers
WHERE CustomerID IN
(
SELECT CustomerID
FROM Orders
);
Benefits:
Customer segmentation.
Applications:
Marketing analysis.
Example:
SELECT Department,
Budget
FROM Departments
WHERE Budget >
(
SELECT AVG(Budget)
FROM Departments
);
Benefits:
Budget evaluation.
Applications:
Financial planning.
Example:
SELECT EmployeeName,
Salary
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);
Benefits:
Compensation analysis.
Applications:
Workforce reporting.
Business Intelligence systems use Subqueries for:
Examples:
Subqueries support advanced business insights.
Both Subqueries and Joins can solve similar problems.
Benefits:
Benefits:
Experienced analysts understand when each approach is appropriate.
Incorrect:
WHERE Salary =
(
SELECT Salary
FROM Employees
)
May return multiple rows.
Can impact performance.
May affect results.
Simpler solutions may exist.
Understanding these issues improves SQL efficiency.
Improve readability.
Test separately.
Improve performance.
Maintain simplicity.
Reduce execution time.
Ensure accuracy.
These practices improve SQL quality and maintainability.
Data Analysts use Subqueries for:
Subqueries are a core analytical SQL technique.
Organizations benefit through:
Subqueries enable analysts to answer complex business questions efficiently.
After completing this lesson, you will be able to:
Subqueries are SQL queries written inside another SQL query.
They help solve complex analytical and reporting problems.
A Single-Row Subquery returns one value.
A Multiple-Row Subquery returns multiple values.
A Correlated Subquery depends on values from the outer query.
Yes. They are commonly used together.
Not always. The best approach depends on the problem and performance requirements.
They support advanced analysis, KPI reporting, and business intelligence workflows.
Want to master SQL, reporting, and Data Analytics?
WhatsApp us