Common Table Expressions (CTEs) are a powerful feature in SQL that enhance query readability, maintainability, and efficiency. Understanding their benefits is essential for writing clean, reusable, and efficient SQL queries.
A Common Table Expression (CTE) is a temporary, named result set in SQL that you can define within a query. It simplifies complex queries by breaking them into smaller, more manageable parts.
WITH
keyword.
WITH cte_name (optional_column_list) AS (
query_definition
)
SELECT *
FROM cte_name;
cte_name
: Name of the CTE.query_definition
: SQL query that defines the CTE.CTEs allow you to break down complicated queries into smaller parts. Instead of nesting subqueries, you can define multiple CTEs for intermediate steps.
Suppose we want to find employees with salaries above the department average. The employees
table looks like this:
EmployeeID | Name | DepartmentID | Salary |
---|---|---|---|
1 | Alice | 1 | 70000 |
2 | Bob | 1 | 50000 |
3 | Charlie | 2 | 60000 |
4 | David | 2 | 90000 |
WITH DepartmentAverage AS (
SELECT DepartmentID, AVG(Salary) AS AvgSalary
FROM employees
GROUP BY DepartmentID
),
AboveAverageEmployees AS (
SELECT e.EmployeeID, e.Name, e.Salary, d.AvgSalary
FROM employees e
INNER JOIN DepartmentAverage d ON e.DepartmentID = d.DepartmentID
WHERE e.Salary > d.AvgSalary
)
SELECT Name, Salary, AvgSalary
FROM AboveAverageEmployees;
Name | Salary | AvgSalary |
---|---|---|
Alice | 70000 | 60000 |
David | 90000 | 75000 |
CTEs make SQL code more readable by replacing deeply nested subqueries with named result sets.
SELECT e.Name, e.Salary
FROM employees e
WHERE e.Salary > (
SELECT AVG(Salary)
FROM employees
WHERE DepartmentID = e.DepartmentID
);
WITH DepartmentAverage AS (
SELECT DepartmentID, AVG(Salary) AS AvgSalary
FROM employees
GROUP BY DepartmentID
)
SELECT e.Name, e.Salary
FROM employees e
INNER JOIN DepartmentAverage d ON e.DepartmentID = d.DepartmentID
WHERE e.Salary > d.AvgSalary;
The CTE version is easier to read and debug.
By naming each intermediate step in a query, CTEs make queries easier to maintain and modify. Changes can be made to specific parts without rewriting the entire query.
Recursive CTEs handle hierarchical or self-referential data effectively. For example, calculating an employee hierarchy or traversing a directory structure.
CTEs promote modular code by allowing you to reuse the same logic multiple times in a query.
WITH SalesData AS (
SELECT ProductID, SUM(Sales) AS TotalSales
FROM sales
GROUP BY ProductID
)
SELECT *
FROM SalesData
WHERE TotalSales > 10000;
SELECT ProductID
FROM SalesData
WHERE TotalSales BETWEEN 5000 AND 10000;
WITH SalesData AS (
SELECT ProductID, SUM(Sales) AS TotalSales
FROM sales
GROUP BY ProductID
),
HighSales AS (
SELECT ProductID, TotalSales
FROM SalesData
WHERE TotalSales > 5000
)
SELECT *
FROM HighSales;
CTEs can store intermediate results for complex data manipulations.
CTEs are a versatile and powerful SQL feature that simplifies complex queries, enhances readability, and supports modularity. By understanding their syntax, benefits, and limitations, you can effectively use CTEs to write clean and efficient SQL code. Happy Coding!❤️