Common Table Expressions (CTEs) let you define named subqueries that make complex queries more readable. Let's start with a simple CTE.
Write a query using a CTE to count albums per artist, then select artists with more than 5 albums.
| Column | Type |
|---|---|
| ArtistId | INTEGER (Primary Key) |
| Name | TEXT |
| Column | Type |
|---|---|
| AlbumId | INTEGER (Primary Key) |
| Title | TEXT |
| ArtistId | INTEGER (Foreign Key → Artist.ArtistId) |
WITH cte_name AS (
SELECT ...
)
SELECT * FROM cte_name WHERE ...;
Ready to take your skills to the next level? Enroll in our comprehensive Data Analyst Career Track to master SQL, Python, Excel, and Power BI.
Run your code to see the result here.