runtime
103 / 139

task Recursive CTEs · medium

Predict: a recursive team path

Predict each sample team’s depth below the top level.

what to return

Columns
team_iddepth in this order
Rows
in the order asked
Watch for
Use only the sample rows shown. NULL is not zero or empty text. Keep duplicate rows.
Skill
Reason about the exact result of a fixed SQL query. Refresher: Recursive CTEs →

data you can query

team_sample3 columns · 5 rows
team_idteam_nameparent_team_id
49InfrastructureNULL
50PlatformNULL
51API50
52Data51
53Messaging52
team_id
49
team_name
Infrastructure
parent_team_id
NULL
team_id
50
team_name
Platform
parent_team_id
NULL
team_id
51
team_name
API
parent_team_id
50
team_id
52
team_name
Data
parent_team_id
51
team_id
53
team_name
Messaging
parent_team_id
52

all 5 rows

predict.sql
WITH RECURSIVE t(team_id, depth) AS (
  SELECT team_id, 0
  FROM team_sample
  WHERE parent_team_id IS NULL
  UNION ALL
  SELECT s.team_id, t.depth + 1
  FROM team_sample s
  JOIN t ON s.parent_team_id = t.team_id
)
SELECT team_id, depth
FROM t
ORDER BY team_id
prediction.json

Enter the rows you think the query returns, column order team_id, depth. JSON array of rows — numbers bare, text in quotes, null for NULL. Example: [ [ 1, null ] ]. Keep ORDER BY order.

 
Output

Hints and the answer are always available. Using them counts the question as solved with help.

Predict: a recursive team path — SQL practice · Runtime