THE CLOUD EXPEDITION! · Chapter 6

Spot the Difference

Marina bursts in, eyes blazing. 'The storm changed EVERYTHING — but which districts got hit hardest?' Chronos raises a feathered brow. 'Compare then and now. The data will confess what the storm tried to hide.' This is Version Comparison — diffing two snapshots of the same table to find what changed.

Your task

Compare the pre-storm census (version 1) with the current data. For each district, show <b>district</b>, <b>pop_before_storm</b>, <b>pop_after_storm</b>, and <b>population_change</b> (the difference). Biggest absolute changes first. Table: `dbquacks_living_lake.pond_census` (Ducklake — version 1 is the pre-storm snapshot, current version is post-storm).

Hint

Use a CTE to capture the version 1 snapshot, then JOIN it with the current table on district

Expected result

Rows with columns: district, pop_before_storm, pop_after_storm, and population_change — ranked by biggest absolute change

Tables

lake_visits (500 rows)

columntype
visit_idBIGINT
duck_idBIGINT
destination_pondVARCHAR
visit_dateDATE
purposeVARCHAR

pondville_events (50 rows)

columntype
event_idBIGINT
event_nameVARCHAR
event_dateDATE
organizer_idBIGINT
locationVARCHAR
attendee_countBIGINT

pondville_residents (100 rows)

columntype
duck_idBIGINT
nameVARCHAR
speciesVARCHAR
home_pondVARCHAR
ageBIGINT
occupationVARCHAR
joined_dateDATE

dbquacks_living_lake.pond_census

A table in a MotherDuck share; its columns are not listed here.

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

This page as markdown: https://dbquacks.com/motherduck/16.md

Start level 16 →

DuckDB docs for this lesson

All levels

  1. 1. Connect DuckDB to MotherDuck
  2. 2. Query a MotherDuck Table
  3. 3. MotherDuck Hybrid Queries
  4. 4. DESCRIBE a MotherDuck Table
  5. 5. Join Local and Cloud Tables
  6. 6. Aggregate Cloud Data in MotherDuck
  7. 7. ATTACH a MotherDuck Share
  8. 8. Query Several MotherDuck Shares
  9. 9. CREATE DATABASE in MotherDuck
  10. 10. Copy Data into a MotherDuck Table
  11. 11. CREATE SHARE in MotherDuck
  12. 12. DuckDB read_csv from a URL
  13. 13. DuckDB read_parquet from a URL
  14. 14. Join a Remote CSV with Cloud Tables
  15. 15. DuckLake Time Travel
  16. 16. Compare DuckLake Snapshots
  17. 17. DuckLake Time Travel with Shares
  18. 18. Aggregate 100 Million Rows in MotherDuck
  19. 19. EXPLAIN a MotherDuck Hybrid Query
  20. 20. Combine Local, Cloud and DuckLake Data

More: every SQL tutorial lesson · practice SQL exercises online