Common Table Expressions (CTEs) are a powerful feature in SQL that provides a temporary result set that can be referenced within a SELECT, INSERT, UPDATE, or DELETE statement.
A Common Table Expression (CTE) is a named temporary result set that exists within the scope of a single SQL query. CTEs simplify complex queries by breaking them into smaller, more readable components.
The general syntax for a CTE is as follows:
WITH cte_name (column1, column2, ...) AS (
SELECT columns
FROM table
WHERE conditions
)
SELECT columns
FROM cte_name;
A simple CTE is used to store a result set temporarily for reuse in subsequent queries.
Recursive CTEs are used to perform iterative operations, such as traversing hierarchical data.
Simplifying Complex Queries: Break a long query into smaller, logical steps.
Hierarchical Data Traversal: Analyze data with parent-child relationships, such as organizational charts or folder structures.
Aggregation and Ranking: Perform calculations like ranking or percentiles in an intuitive manner.
Reusing Subqueries: Avoid duplicating logic by defining reusable components.
Problem: Find employees with salaries above the average.
WITH AverageSalary AS (
SELECT AVG(salary) AS avg_salary
FROM employees
)
SELECT name, salary
FROM employees, AverageSalary
WHERE salary > avg_salary;
AverageSalary
calculates the average salary.Name | Salary |
---|---|
Alice | 75000 |
Bob | 90000 |
Problem: Rank employees by department based on salary.
WITH RankedEmployees AS (
SELECT
department_id,
name,
salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rank
FROM employees
)
SELECT department_id, name, salary, rank
FROM RankedEmployees
WHERE rank = 1;
RankedEmployees
ranks employees within each department.Department_ID | Name | Salary | Rank |
---|---|---|---|
1 | Alice | 90000 | 1 |
2 | John | 80000 | 1 |
Problem: Generate an organizational hierarchy.
Employee_ID | Name | Manager_ID |
---|---|---|
1 | Alice | NULL |
2 | Bob | 1 |
3 | Charlie | 2 |
4 | Dave | 2 |
WITH OrgHierarchy AS (
SELECT
Employee_ID,
Name,
Manager_ID,
1 AS Level
FROM employees
WHERE Manager_ID IS NULL
UNION ALL
SELECT
e.Employee_ID,
e.Name,
e.Manager_ID,
oh.Level + 1
FROM employees e
INNER JOIN OrgHierarchy oh
ON e.Manager_ID = oh.Employee_ID
)
SELECT *
FROM OrgHierarchy;
Employee_ID | Name | Manager_ID | Level |
---|---|---|---|
1 | Alice | NULL | 1 |
2 | Bob | 1 | 2 |
3 | Charlie | 2 | 3 |
4 | Dave | 2 | 3 |
Feature | CTE | Subquery | Temporary Table |
---|---|---|---|
Scope | Single query block | Single query block | Across multiple queries |
Readability | High | Moderate | Moderate |
Reusability | High | Low | High |
Performance Impact | Optimized by SQL engine | May execute repeatedly | Depends on implementation |
Scope: CTEs exist only within the query where they are defined.
Performance: In certain cases, CTEs may not outperform subqueries or temporary tables.
Database Support: Ensure your database supports CTEs.
Use Descriptive Names: Name CTEs and their columns meaningfully.
Limit Recursion Depth: Avoid infinite loops in recursive CTEs by specifying a MAXRECURSION
limit.
Optimize for Performance: Analyze execution plans to ensure efficient query execution.
Common Table Expressions (CTEs) are a valuable tool for simplifying and structuring SQL queries. Whether for breaking down complex queries, handling hierarchical data, or performing recursive operations, CTEs enhance readability and maintainability. Happy Coding!❤️