LESSON 33 / 62 · Common Table ExpressionsCommon Table Expressions, WITH
Common Table Expression in SQL. Syntax of the WITH statement and examples of its use.
Common Table Expressions, operator WITH
Common Table Expressions is a temporary dataset that can be accessed in subsequent queries. The WITH operator is used to write a common table expressions.
-- Example of using the WITH clause
WITH Aeroflot_trips AS
(SELECT TRIP.* FROM Company
INNER JOIN Trip ON Trip.company = Company.id WHERE name = 'Aeroflot')
SELECT plane, COUNT(plane) AS amount FROM Aeroflot_trips GROUP BY plane;An expression with WITH is considered "temporary" because the result is not stored somewhere permanently in the database schema, but acts as a temporary representation that exists only for the duration of the query, that is, it is available only during the execution of a SELECT, INSERT, UPDATE, DELETE, or MERGE statement. It is only valid in the query it belongs to, allowing you to improve the structure of the query without polluting the global namespace.
WITH statement syntax
WITH cte_name [(column [, ...])] AS (subquery)
[, cte_name [(column [, ...])] AS (subquery)] ...- [ ]optional part
- ...can be repeated
The name of the common table expression. If you need several expressions, list them separated by commas.
Column names of the table expression, separated by commas. If you omit them, they are taken from the subquery.
A query whose result can be used in other parts of the SQL query by the name cte_name.
- column
- Optional
- subquery
- Subquery
Query examples
WITH Aeroflot_trips AS
(SELECT plane, town_from, town_to FROM Company
INNER JOIN Trip ON Trip.company = Company.id WHERE name = 'Aeroflot')
SELECT * FROM Aeroflot_trips;| plane | town_from | town_to |
|---|---|---|
| IL-86 | Moscow | Rostov |
| IL-86 | Rostov | Moscow |
- Similarly, we create a common table expression Aeroflot_trips, but with renamed columns
WITH Aeroflot_trips (aeroflot_plane, town_from, town_to) AS
(SELECT plane, town_from, town_to FROM Company
INNER JOIN Trip ON Trip.company = Company.id WHERE name = 'Aeroflot')
SELECT * FROM Aeroflot_trips;| aeroflot_plane | town_from | town_to |
|---|---|---|
| IL-86 | Moscow | Rostov |
| IL-86 | Rostov | Moscow |
- Using the WITH operator, we define several common table expressions
WITH Aeroflot_trips AS
(SELECT TRIP.* FROM Company
INNER JOIN Trip ON Trip.company = Company.id WHERE name = 'Aeroflot'),
Don_avia_trips AS
(SELECT TRIP.* FROM Company
INNER JOIN Trip ON Trip.company = Company.id WHERE name = 'Don_avia')
SELECT * FROM Don_avia_trips UNION SELECT * FROM Aeroflot_trips;| id | company | plane | town_from | town_to | time_out | time_in |
|---|---|---|---|---|---|---|
| 1181 | 1 | TU-134 | Rostov | Moscow | 1900-01-01T06:12:00.000Z | 1900-01-01T08:01:00.000Z |
| 1182 | 1 | TU-134 | Moscow | Rostov | 1900-01-01T12:35:00.000Z | 1900-01-01T14:30:00.000Z |
| 1187 | 1 | TU-134 | Rostov | Moscow | 1900-01-01T15:42:00.000Z | 1900-01-01T17:39:00.000Z |
| 1188 | 1 | TU-134 | Moscow | Rostov | 1900-01-01T22:50:00.000Z | 1900-01-02T00:48:00.000Z |
| 1195 | 1 | TU-154 | Rostov | Moscow | 1900-01-01T23:30:00.000Z | 1900-01-02T01:11:00.000Z |
| 1196 | 1 | TU-154 | Moscow | Rostov | 1900-01-01T04:00:00.000Z | 1900-01-01T05:45:00.000Z |
| 1145 | 2 | IL-86 | Moscow | Rostov | 1900-01-01T09:35:00.000Z | 1900-01-01T11:23:00.000Z |
| 1146 | 2 | IL-86 | Rostov | Moscow | 1900-01-01T17:55:00.000Z | 1900-01-01T20:01:00.000Z |
Working with recursion in CTE
CTEs can also be used to perform recursive queries, which allow iterative data processing, for example, working with hierarchical data structures such as manager-subordinate relationships.
Syntax of a recursive CTE
A recursive CTE consists of two parts separated by the UNION ALL operator:
- The initial dataset, which does not contain recursive references.
- The recursive part: a query that refers to the CTE to continue the recursion.
WITH RECURSIVE cte_name [(column [, ...])] AS (
-- Initial dataset
SELECT expression [, ...]
FROM table_name
[WHERE condition]
UNION ALL
-- Recursive part
SELECT expression [, ...]
FROM cte_name
INNER JOIN table_name ON join_condition
[WHERE condition]
)
SELECT * FROM cte_name;- [ ]optional part
- ...can be repeated
The name of the table expression. The recursive part refers to it to continue the recursion.
Column names of the table expression. Without the list, the names come from the initial part.
Columns and calculations that each part selects. Both parts select the same number of columns. The initial part sets the column types, and the recursive part's values must fit them. The recursive part often calculates something like level + 1.
How the table rows are linked to the rows found at the previous step, for example e.managerId = s.id.
Filters rows: in the initial part, which rows to start from; in the recursive part, which rows to add at the next step.
- column
- Optional
- table_name
- The table with the source data, for example Employees.
- condition
- Optional
Example: manager-subordinate hierarchy
Consider the Employees table, which contains employee IDs and their managers:
| id | name | managerId |
|---|---|---|
| 1 | John Smith | |
| 2 | Michael Johnson | 1 |
| 3 | Robert Williams | 1 |
| 4 | James Brown | 2 |
| 5 | David Jones | 2 |
| 6 | Richard Davis | 3 |
We need to find all subordinates of John Smith (id=1) at all hierarchy levels.
WITH RECURSIVE Subordinates AS (
-- Initial dataset
SELECT id, name, managerId
FROM Employees
WHERE managerId = 1
UNION ALL
-- Recursive part: subordinates of subordinates
SELECT e.id, e.name, e.managerId
FROM Employees e
INNER JOIN Subordinates s ON e.managerId = s.id
)
SELECT * FROM Subordinates;| id | name | managerId |
|---|---|---|
| 2 | Michael Johnson | 1 |
| 3 | Robert Williams | 1 |
| 4 | James Brown | 2 |
| 5 | David Jones | 2 |
| 6 | Richard Davis | 3 |
Steps for executing a recursive CTE
- Initial dataset: selects all employees whose managerId=1 (direct subordinates of John Smith).
- Recursive part: for each employee selected in the initial dataset, selects their subordinates (where managerId equals the id of the chosen employee).
- Union: unites the results of the initial dataset and the recursive parts using UNION ALL.
- Recursion: the process repeats for each new set of subordinates until all hierarchy levels are retrieved.
Conclusion
Common table expressions have been added to SQL to simplify complex long queries, especially those with multiple subqueries. Their main task is to improve readability, ease of writing queries and their further support. It does this by hiding large and complex queries in named expressions, which are then used in the main query.
Article contentWITH statement syntaxQuery examplesWorking with recursion in CTESyntax of a recursive CTEExample: manager-subordinate hierarchySteps for executing a recursive CTEConclusionEdit on GitHub
Python Academy
Practice
Solve it in the editor. The query runs against a sample database in your browser.
Write a query and run it to see the results.
Write a query and run it to see the results.