AlgoMaster Logo

Exercise: Recursive CTEs

Last Updated: May 12, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Generate the integers 1 through 10 using a recursive CTE. Return one row per integer in column n.

SQL

Expected Query

Expected Output

Exercise 2

Medium

Find all sub-genres under Electronic (genre_id 2), including Electronic itself. Return only the name column ordered by genre_id.

SQL

Expected Query

Expected Output

Exercise 3

Medium

Generate every date from 2024-03-01 to 2024-03-04 using a recursive CTE, and show the number of streams on each date. Return columns stream_date and stream_count, ordered by stream_date.

SQL

Expected Query

Expected Output

Exercise 4

Hard

For every genre, return its name and the name of its top-level (root) ancestor. A root genre is its own root. Order by genre_id.

SQL

Expected Query

Expected Output