← SQL workplace practice

Recursive structures · Original fictional data · Free files and full answers

Roll all descendant costs into every manager

Each node contributes its own cost once to itself and every ancestor. Return id,subtree_cost sorted id.

Use this fixed business case to build a checked CSV and a replayable SQL handoff. No account required. Scenario datasets are shared across related exercises; each exercise has its own decision and output contract.

Your output contract

  1. Each node contributes its own cost once to itself and every ancestor. Return id,subtree_cost sorted id.
  2. Return exactly the declared columns and order. Money uses integer cents; all time values are elapsed UTC minutes unless a field is explicitly a day index. NULL is unknown and differs from numeric zero.
  3. These are original fictional records and exercise policies. All source tables in this case are included in the downloadable pack.

Return columns exactly as id, subtree_cost. Sort order follows the task. Output has 10 rows; an empty CSV field means NULL.

Source data

nodes

10 source rows · Download nodes CSV

id INTEGERparent INTEGERcost INTEGER
1NULL5
2110
3120
423
524
638
742
8NULL6
989
1091

edges

8 source rows · Download edges CSV

src INTEGERdst INTEGERweight INTEGER
123
138
232
247
341
422
454
671

parts

6 source rows · Download parts CSV

parent TEXTchild TEXTqty INTEGER
KITFRAME1
KITWHEEL2
FRAMEBOLT4
FRAMEBAR2
WHEELBOLT3
WHEELRIM1
Field dictionary and schema
nodes:
  id INTEGER — Stable record or business key; retain its declared type.
  parent INTEGER — Exact integer measurement; use the task rule for units and ordering.
  cost INTEGER — Exact integer measurement; use the task rule for units and ordering.

edges:
  src INTEGER — Exact integer measurement; use the task rule for units and ordering.
  dst INTEGER — Exact integer measurement; use the task rule for units and ordering.
  weight INTEGER — Exact integer measurement; use the task rule for units and ordering.

parts:
  parent TEXT — Literal text label or key; comparisons use SQLite binary text semantics.
  child TEXT — Literal text label or key; comparisons use SQLite binary text semantics.
  qty INTEGER — Exact integer measurement; use the task rule for units and ordering.

Download complete SQLite setup

A method you can reuse

  1. Each node contributes its own cost once to itself and every ancestor. Return id,subtree_cost sorted id.
  2. Inspect the source grain, keys and ties before writing the query. Use the worked SQL and complete expected CSV to review any mismatch.

Watch for this wrong turn

Summing direct children only leaves out grandchildren and deeper costs.

Run and check in SQLite

Your SQL runs locally in the existing SQLite worker. The checker compares every column, row, type, value and row order with this fixed output. It accepts equivalent SELECT or WITH queries. Browser storage keeps your SQL on this device when available.

Complete worked SQL and expected output
WITH RECURSIVE h(root,id,cost) AS (SELECT id,id,cost FROM nodes UNION ALL SELECT h.root,n.id,n.cost FROM h JOIN nodes n ON n.parent=h.id) SELECT root id,SUM(cost) subtree_cost FROM h GROUP BY root ORDER BY root;
"id","subtree_cost"
"1","52"
"2","19"
"3","28"
"4","5"
"5","4"
"6","8"
"7","2"
"8","16"
"9","10"
"10","1"
Reference SQL · Complete expected CSV · Empty answer CSV

Choose another SQL task · Original data exception cases · Larger workplace projects