SQL practice: Recursive CTEs: Queries That Call Themselves

Chapter 36 · 9 questions · included in full access

  1. Question 1 · easy

    Use a recursive CTE to produce the numbers 1 to 10, in a single column named n.

    Solve it in your browser
  2. Question 2 · easy

    Produce every date in January 2024 as a column named day, using a recursive CTE.

    Solve it in your browser
  3. Question 3 · medium

    Walk the employee hierarchy from the top down. Show employee_name and level, where the people with no manager are level 1. Order by level, then name.

    Solve it in your browser
  4. Question 4 · medium

    Show each employee with a breadcrumb path from their top-level manager, like "Rajesh Kumar > Anita Sharma", in a column named path. Order by path.

    Solve it in your browser
  5. Question 5 · medium

    Count how many people sit at each level of the hierarchy. Return level and people, ordered by level.

    Solve it in your browser
  6. Question 6 · medium

    List everyone who reports to Rajesh Kumar, directly or indirectly. Show employee_name, ordered by name. Do not include Rajesh himself.

    Solve it in your browser
  7. Question 7 · medium

    Starting at 1, keep doubling until the value passes 1000. Return the values in a column named n, in order.

    Solve it in your browser
  8. Question 8 · hard

    For every branch manager, list their team: show manager_name, employee_name and level, ordered by manager_name then employee_name. A manager appears as level 1 with their own name in both columns.

    Solve it in your browser
  9. Question 9 · hard

    A recursive CTE that walks a hierarchy has no WHERE clause in its recursive step, and the data contains a cycle: A manages B, and B manages A. What happens?

    Solve it in your browser

These questions are part of full access.

The first chapters of every track are free. Full access is a one-time payment.

See pricing