DB Quacks SQL Tutorial · Chapter 5

WITH Great Power: The CTE Symphony

Maestro duck appears with a conductors baton. Welcome to Query Symphony Orchestra! Complex analysis needs multiple movements working harmoniously. First find ducks older than 3, then show only frequent pond visitors (ducks who visited ponds more than 2 times). Use CTEs!

Your task

Create a multi-step analysis using CTEs - first find older ducks from the ducks table, then filter by visit frequency from 'data/pond_visits.csv'. You will be using Common Table Expressions to break down complex logic.

Hint

Look for a way to create temporary named result sets that you can reference in your main query.

Expected result

A table showing older duck names who are frequent pond visitors (more than 2 visits).

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/pond_visits.csv (40 rows)

columntype
visit_idBIGINT
duck_idBIGINT
pond_idBIGINT
visit_dateDATE
duration_minutesBIGINT

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

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

Start level 27 →

DuckDB docs for this lesson