Recursive structures · Original fictional data · Free files and full answers
Find a common reporting ancestor
For the fixed nodes 7 and 5 return their nearest common ancestor id. Distance is the number of parent edges; choose the common ancestor minimizing summed distance.
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
- For the fixed nodes 7 and 5 return their nearest common ancestor id. Distance is the number of parent edges; choose the common ancestor minimizing summed distance.
- 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.
- These are original fictional records and exercise policies. All source tables in this case are included in the downloadable pack.
Return columns exactly as ancestor_id. Sort order follows the task. Output has 1 rows; an empty CSV field means NULL.
Source data
nodes
10 source rows · Download nodes CSV
| id INTEGER | parent INTEGER | cost INTEGER |
|---|---|---|
| 1 | NULL | 5 |
| 2 | 1 | 10 |
| 3 | 1 | 20 |
| 4 | 2 | 3 |
| 5 | 2 | 4 |
| 6 | 3 | 8 |
| 7 | 4 | 2 |
| 8 | NULL | 6 |
| 9 | 8 | 9 |
| 10 | 9 | 1 |
edges
8 source rows · Download edges CSV
| src INTEGER | dst INTEGER | weight INTEGER |
|---|---|---|
| 1 | 2 | 3 |
| 1 | 3 | 8 |
| 2 | 3 | 2 |
| 2 | 4 | 7 |
| 3 | 4 | 1 |
| 4 | 2 | 2 |
| 4 | 5 | 4 |
| 6 | 7 | 1 |
parts
6 source rows · Download parts CSV
| parent TEXT | child TEXT | qty INTEGER |
|---|---|---|
| KIT | FRAME | 1 |
| KIT | WHEEL | 2 |
| FRAME | BOLT | 4 |
| FRAME | BAR | 2 |
| WHEEL | BOLT | 3 |
| WHEEL | RIM | 1 |
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.
A method you can reuse
- For the fixed nodes 7 and 5 return their nearest common ancestor id. Distance is the number of parent edges; choose the common ancestor minimizing summed distance.
- 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
The shared root is a common ancestor but may not be the nearest common ancestor.
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 a(id,d) AS (SELECT 7,0 UNION ALL SELECT n.parent,a.d+1 FROM a JOIN nodes n ON n.id=a.id WHERE n.parent IS NOT NULL), b(id,d) AS (SELECT 5,0 UNION ALL SELECT n.parent,b.d+1 FROM b JOIN nodes n ON n.id=b.id WHERE n.parent IS NOT NULL) SELECT a.id ancestor_id FROM a JOIN b USING(id) ORDER BY a.d+b.d LIMIT 1;
"ancestor_id" "2"Reference SQL · Complete expected CSV · Empty answer CSV
Choose another SQL task · Original data exception cases · Larger workplace projects