AlgoMaster Logo

Exercise: Hierarchical Queries and Tree Traversal

Last Updated: May 12, 2026

1 min read

For this exercise, we will work with below data:

SQL

Exercise 1

Easy

Write a query that returns all root genres (those with no parent). Show name ordered alphabetically.

SQL

Expected Query

Expected Output

Exercise 2

Medium

Write a query that returns all leaf genres (those with no children). Show name ordered alphabetically.

SQL

Expected Query

Expected Output

Exercise 3

Hard

Write a query that finds every artist who reports up to "Luna Wave" (directly or through intermediate managers). Show artist name and the depth from Luna Wave (direct reports are depth 1). Order by depth, then name.

SQL

Expected Query

Expected Output