internalsGet Pro

Module 02 · Lesson 8 of 12Pro

Recursive queries: hierarchies and graphs in SQL

Level
Advanced
Topic
SQL for data engineering
Reading time
18 min

When you finish this page you can

  1. Write a WITH RECURSIVE query from its three parts, an anchor, a step and a stop, and trace it round by round.
  2. Walk a category tree down and up, with a depth and a readable path for every category.
  3. Roll revenue up to every level of a tree of any depth, counting each sale once per ancestor.
  4. Stop cycles in bad tree data, and decide when to build a closure table in the pipeline instead of recursing in every report.
  1. Design: a first analytics pipeline for an online store30 min
  2. Join pitfalls: duplicated and missing rows16 min

Electronics sold ₹86,400 and the report shows nothing

It’s Tuesday morning, 10 November, at , an online store in India. Every night Meera, its data engineer, loads the day’s orders from the store’s PostgreSQL database into DuckDB, where the reports read them.

Our biggest sale ever · Black Friday

Flat 50% Off

Pro

Continue reading

Unlock every course, now and later

  • System Design, live
  • AI Engineering, live
  • Data Engineering, live
  • Machine Learning Engineering, live
  • Inference Engineering, coming soon
  • AI Research, coming soon
  • Performance Engineering, coming soon
  • Agent & Harness Engineering, coming soon
  • LLM Fine-tuning, coming soon
  • LLM Pre-training & Post-training, coming soon
  • Reinforcement Learning, coming soon
  • AI Infrastructure & Platform Engineering, coming soon
  • AI Evaluation & Safety, coming soon
  • Forward Deployed Engineering, coming soon
  • Cloud Computing, coming soon
  • DevOps, coming soon

50% off · Annual and Lifetime

BLACKFRIDAY50Applied at checkout

Choose a plan