← 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

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

Explore the program