
A hierarchical query is a type of SQL query that handles hierarchical model data. These are useful for working with databases of graph-structured data, such as river networks, file system trees, or threaded comments. They are special cases of more general recursive fixed-point queries, which compute transitive closures. In standard SQL:1999 hierarchical queries are implemented by way of recursive common table expressions (CTEs). Unlike Oracle's earlier connect-by clause, recursive CTEs were designed with fixed-point semantics from the beginning. Recursive CTEs from the standard were relatively close to the existing implementation in IBM DB2 version 2. Recursive CTEs are also supported by Microsoft SQL Server (since SQL Server 2008 R2), Firebird 2.1, PostgreSQL 8.4+, SQLite 3.8.3+, IBM Informix version 11.50+, CUBRID, MariaDB 10.2+ and MySQL 8.0.1+. Tableau has documentation describing how to use recursive CTEs.
WITH top_customers AS (
SELECT customer_id, SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 1000
)
SELECT c.customer_name, t.total_spent
FROM top_customers t
JOIN customers c ON c.customer_id = t.customer_id
ORDER BY t.total_spent DESC;WITH RECURSIVE employee_hierarchy AS (
SELECT employee_id, manager_id, employee_name
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.employee_id, e.manager_id, e.employee_name
FROM employees e
INNER JOIN employee_hierarchy eh
ON e.manager_id = eh.employee_id
)
SELECT * FROM employee_hierarchy;When developers and analysts began working with increasingly complex SQL queries, they faced a recurring problem: readability and maintainability. Queries often turned into massive, deeply nested statements that were difficult to debug or modify. CTEs were introduced as an elegant solution to that problem. They make SQL cleaner, more modular, and easier to understand. A CTE, or Common Table Expression, is essentially a temporary result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. It behaves like a named subquery, but with one important advantage, it improves the structure and clarity of your SQL.
Basic Syntax and Example
A CTE starts with the WITH keyword, followed by the CTE name and an optional list of column aliases. You then define the query that produces the temporary result set.
- Example: The CTE `top_customers` calculates total spending per customer. The main query then joins this temporary set to the customers table.
“`sql WITH top_customers AS ( SELECT customer_id, SUM(amount) AS total_spent FROM orders GROUP BY customer_id HAVING SUM(amount) > 1000 ) SELECT c.customer_name, t.total_spent FROM top_customers t JOIN customers c ON c.customer_id = t.customer_id ORDER BY t.total_spent DESC; “`
Benefits of Using CTEs
- Improved readability: CTEs turn multi-step logic into a clear, sequential narrative.
- Reusability within a single query: You can reference the same CTE multiple times within a statement without rewriting the same subquery.
- Easier debugging and testing: Each CTE can be tested independently.
- Recursive capabilities: CTEs can be recursive, handling hierarchical or iterative data.
Recursive CTEs
Recursive CTEs are particularly powerful for handling hierarchical data, such as organizational charts or category trees. They consist of two parts: an anchor member (the base query) and a recursive member (the query that references the CTE itself).
- Example: This query retrieves all employees, starting with top-level managers, and recursively drills down the hierarchy.
“`sql WITH RECURSIVE employee_hierarchy AS ( SELECT employee_id, manager_id, employee_name FROM employees WHERE manager_id IS NULL UNION ALL SELECT e.employee_id, e.manager_id, e.employee_name FROM employees e INNER JOIN employee_hierarchy eh ON e.manager_id = eh.employee_id ) SELECT * FROM employee_hierarchy; “`
Comparing CTEs to Alternatives
Before CTEs became widely supported, developers relied on derived tables or temporary tables for similar logic. Temporary tables persist beyond the query that created them, consuming system resources and often requiring manual cleanup. Derived tables improve isolation but can quickly grow unwieldy within complex joins. CTEs bridge the gap, they’re transient and readable, yet powerful enough for multi-level transformations.
When Not to Use CTEs
While CTEs improve clarity, they’re not always the most efficient choice. In some database engines, a CTE acts as a logical barrier that prevents query optimizers from reusing computations or pushing filters down. This can result in slower execution than using an inline view or derived table. For performance-critical operations, testing execution plans remains essential.
Practical Use Cases
- Breaking down complex transformations: Use multiple CTEs in sequence, each performing one transformation, such as filtering, aggregation, or ranking.
- Data deduplication: Use a CTE to identify duplicates before deleting them from the base table.
- Ranking and windowing: Combine CTEs with functions like ROW_NUMBER() or RANK() for analytics workflows.
- Recursive reporting: For hierarchical relationships, projects, tasks, parts, or categories, recursive CTEs provide a natural and efficient approach.
Common Table Expressions represent more than a convenient syntax, they signify a philosophical shift toward more modular, readable SQL. They empower analysts and developers to focus on logical steps rather than tangled syntax. When used thoughtfully, CTEs can make the difference between an impenetrable query and one that clearly communicates intent.