How to Use a Common Table Expression (CTE) in SQL

Hero image for How to Use a Common Table Expression (CTE) in SQL
This is an external article made in collaboration with another source. Click the button below the view the whole article.

Common Table Expressions (CTEs) are my go-to tool when writing complex SQL queries. They can be thought of as working like sub-queries or temporary tables but with their own superpowers.

In this article, we’ll take a deep dive into what CTEs are and how they can simplify your queries, how recursive CTES can turn impossible PostgreSQL queries into possible, and how to use CTEs to create optimization fences to improve the performance of your queries.

How to Use a Common Table Expression (CTE) in SQL