DB Quacks SQL Tutorial · Chapter 5

Duck Dynasty: The Recursive Family Saga

Dr. Stork returns with touching news! Families want reunion events at the festival! Grandpa Quackers (parent ID 1) has family scattered across generations but wants to reunite. We need to trace ALL his descendants through every generation for proper invitations!

Your task

Find all descendants of Grandpa Quackers (parent ID 1) through multiple generations using the 'data/duck_family.csv' file. You will be using recursive CTEs to traverse family relationships.

Hint

Look for a special type of CTE that can reference itself to follow relationships through multiple levels.

Expected result

A list of all descendant duck names of Grandpa Quackers (parent ID 1) through multiple generations.

Tables

duck_diaries (10 rows)

columntype
diary_idBIGINT
duck_idBIGINT
entry_dateDATE
diary_textVARCHAR
moodVARCHAR

duck_surveys (5000 rows)

columntype
survey_idBIGINT
duck_idBIGINT
ageBIGINT
favorite_foodVARCHAR
favorite_pondBIGINT
swimming_ratingBIGINT
migration_preferenceVARCHAR
social_behaviorVARCHAR
health_statusVARCHAR
activity_levelVARCHAR
survey_dateDATE
survey_locationBIGINT

ducks (15 rows)

columntype
idBIGINT
nameVARCHAR
ageBIGINT
speciesVARCHAR
birth_dateDATE
arrival_dateDATE

ponds (3 rows)

columntype
pond_idBIGINT
pond_nameVARCHAR
location_latDOUBLE
location_lngDOUBLE
capacityBIGINT
featuresVARCHAR[]
amenitiesVARCHAR[]

Files

data/duck_family.csv (10 rows)

columntype
duck_idBIGINT
parent_idBIGINT
relationshipVARCHAR

Database: https://dbquacks.com/data/databases/tutorial.duckdb

This page as markdown: https://dbquacks.com/tutorial/28.md

Start level 28 →

DuckDB docs for this lesson