Curriculum
Subqueries and Views are advanced SQL concepts that help Business Analysts, Data Analysts, Business Intelligence Professionals, and Data Scientists perform complex data analysis and simplify reporting. As organizations grow, databases become larger and more complex. Analysts often need to answer sophisticated business questions that cannot be solved using simple SQL queries alone.
Subqueries allow one query to be placed inside another query, enabling advanced filtering, calculations, and data retrieval. Views provide virtual tables that simplify reporting and improve query reusability. Together, Subqueries and Views make SQL more powerful, efficient, and suitable for enterprise-level Business Analytics.
In this lesson, you will learn the fundamentals of Subqueries and Views, types of subqueries, view creation, business applications, performance considerations, best practices, and real-world examples.
Subqueries and Views begin with understanding subqueries.
A Subquery is a query nested inside another SQL query.
A subquery executes first, and its result is used by the outer query.
General Structure:
SELECT column_name
FROM table_name
WHERE column_name = (
SELECT column_name
FROM another_table
);
Subqueries help solve complex analytical problems efficiently.
Organizations use subqueries because they:
Subqueries are frequently used in business reporting and analytics.
| EmployeeID | EmployeeName | Salary |
|---|---|---|
| 101 | Rahul | 50000 |
| 102 | Priya | 70000 |
| 103 | Amit | 60000 |
Suppose we want to find employees earning more than the average salary.
This is an ideal use case for a subquery.
Query:
SELECT EmployeeName, Salary
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);
Result:
| EmployeeName | Salary |
|---|---|
| Priya | 70000 |
The subquery calculates the average salary first.
The outer query retrieves employees earning above that value.
Execution process:
Execute the inner query.
Return a result.
Pass the result to the outer query.
Execute the outer query.
This sequence allows dynamic analysis.
Subqueries can be categorized based on the results they return.
Returns one value.
Example:
SELECT *
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);
Single-row subqueries are very common in reporting.
Returns multiple values.
Example:
SELECT *
FROM Customers
WHERE City IN
(
SELECT City
FROM Branches
);
The inner query returns multiple cities.
The outer query retrieves matching customers.
A correlated subquery depends on the outer query.
Example:
SELECT EmployeeName, Salary
FROM Employees E
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
WHERE DepartmentID = E.DepartmentID
);
The inner query executes for each row processed by the outer query.
Correlated subqueries support advanced analysis.
Subqueries are commonly used inside WHERE conditions.
Example:
SELECT ProductName
FROM Products
WHERE ProductID IN
(
SELECT ProductID
FROM Sales
);
This retrieves products that have been sold.
Business analysts frequently use this approach.
Subqueries can also be used as temporary tables.
Example:
SELECT *
FROM
(
SELECT ProductID,
SUM(Revenue) AS TotalRevenue
FROM Sales
GROUP BY ProductID
) AS SalesSummary;
Benefits include:
This technique is useful for complex reporting.
Subqueries can generate calculated values.
Example:
SELECT EmployeeName,
Salary,
(
SELECT AVG(Salary)
FROM Employees
) AS AverageSalary
FROM Employees;
This displays employee salaries alongside the company average.
Such comparisons are common in HR Analytics.
EXISTS checks whether a subquery returns records.
Example:
SELECT CustomerName
FROM Customers C
WHERE EXISTS
(
SELECT 1
FROM Orders O
WHERE O.CustomerID = C.CustomerID
);
This retrieves customers who have placed orders.
EXISTS is often used for customer analysis.
NOT EXISTS identifies missing relationships.
Example:
SELECT CustomerName
FROM Customers C
WHERE NOT EXISTS
(
SELECT 1
FROM Orders O
WHERE O.CustomerID = C.CustomerID
);
This retrieves customers without orders.
Business applications include:
| Feature | IN | EXISTS |
|---|---|---|
| Compares Values | Yes | No |
| Checks Record Existence | No | Yes |
| Small Data Sets | Efficient | Efficient |
| Large Data Sets | Moderate | Better |
Understanding both approaches improves query optimization.
Subqueries support many analytical tasks.
Identify:
Identify:
Analyze:
Analyze:
Subqueries provide flexibility in business reporting.
A View is a virtual table created from a SQL query.
Unlike physical tables:
Views simplify reporting and improve reusability.
Organizations use views because they:
Views are widely used in Business Intelligence systems.
Syntax:
CREATE VIEW SalesSummary AS
SELECT ProductID,
SUM(Revenue) AS TotalRevenue
FROM Sales
GROUP BY ProductID;
The view can then be queried like a regular table.
Example:
SELECT *
FROM SalesSummary;
Views simplify reporting workflows.
Once created:
SELECT *
FROM SalesSummary;
Users do not need to rewrite the underlying query.
This improves efficiency.
Some views support updates.
Example:
CREATE OR REPLACE VIEW SalesSummary AS
SELECT ProductID,
SUM(Revenue) AS TotalRevenue,
COUNT(*) AS TotalOrders
FROM Sales
GROUP BY ProductID;
Organizations often update views as reporting requirements change.
To remove a view:
DROP VIEW SalesSummary;
This deletes the virtual table definition.
Source data remains unaffected.
Reduce complexity.
Restrict access to sensitive data.
Use the same logic repeatedly.
Ensure standard calculations.
Views improve enterprise reporting environments.
Business Intelligence platforms frequently use views.
Examples:
Views provide clean and standardized datasets for dashboards.
Many organizations create reporting views specifically for analytics teams.
| Feature | Subqueries | Views |
|---|---|---|
| Temporary | Yes | No |
| Reusable | No | Yes |
| Complexity Reduction | Moderate | High |
| Reporting Support | Good | Excellent |
| Security Benefits | Limited | Strong |
Both techniques are valuable for Business Analytics.
Use proper formatting.
Reduce duplication.
Improve maintainability.
Support collaboration.
Ensure accurate reporting.
These practices improve SQL quality.
May reduce performance.
Can complicate database management.
May slow reporting.
Reduce readability.
Analysts should follow structured design practices.
A retail company wants to identify products performing above average.
The analyst:
Management uses the view in Power BI dashboards and monthly reports.
This demonstrates the practical value of Subqueries and Views in Business Analytics.
After completing this lesson, you will be able to:
A subquery is a query nested inside another SQL query.
They help perform advanced filtering, calculations, and data analysis.
A correlated subquery references values from the outer query.
A view is a virtual table created from a SQL query.
Views simplify reporting, improve security, and support reusable business logic.
No. Standard views display data dynamically from underlying tables.
Views are generally better for repeated reporting, while subqueries are useful for one-time analytical tasks.
WhatsApp us