← All SQL interview questions

Walk the category tree and report its depth

HardRecursive CTEsAmazonMyntra

A catalogue team maintains categories as a parent-child tree in catalog_categories. Root categories have a NULL parent.

Using a recursive CTE, return category_id, category_name, depth (1 for roots, 2 for their children, and so on) and path — the names from the root down to that category joined with > .

Sample data

Table: catalog_categories

15 row(s) · showing first 8

category_idINTcategory_nameTEXTparent_idINT
1Home & KitchenNULL
2ApparelNULL
3Cookware1
4Storage1
5Pans3
6Pressure Cookers3
7Non-stick Pans5
8Cast Iron Pans5

Practising alone only goes so far — our live cohorts pair SQL depth with dashboards, agentic analytics projects and mock interviews.

Explore the program