DATASCI 350 - Data Science Computing

Lecture 22 - SQL Revision with DuckDB

Danilo Freire

Department of Data and Decision Sciences
Emory University

Hello again! 😊

Brief recap πŸ“š

Parallel computing fundamentals

  • Last class: parallel and scalable computing
    • map, joblib and concurrent.futures
    • Parallelism divides the time, but not the Big O growth rate
    • Amdahl’s law: the serial share caps the speed-up
    • Dask: lazy arrays and DataFrames bigger than RAM
    • Parquet stores each column separately, so engines read only the columns you name
  • Some engines use all your cores with no setup
  • DuckDB is one, and today we use it to revise SQL
  • All queries run on the WDI panel from Lecture 19
  • Download it here or use the path ../lecture-19/data/wdi_panel.parquet

Lecture overview

1. Why SQL and why DuckDB

  • You describe the answer, the engine plans the work
  • import duckdb is the whole setup

2. First queries

  • SELECT, WHERE, ORDER BY, LIMIT
  • IN, BETWEEN, LIKE, and the order a query runs in

3. Aggregating

  • COUNT, AVG, GROUP BY, HAVING
  • What COUNT does when values are missing

4. NULLs, CASE and windows

  • IS NULL, COALESCE, income bands with CASE
  • Window functions: a group mean and a rank on every row

5. Joins

  • Primary keys, foreign keys, and the ON clause
  • INNER, LEFT, FULL OUTER on two tiny tables

6. DuckDB and pandas

  • Querying a pandas DataFrame by name
  • Handing a result back with .df()

Why SQL, why DuckDB πŸ¦†

Why SQL

DuckDB’s SQL introduction: https://duckdb.org/docs/stable/sql/introduction

Why DuckDB for a revision class

  • Zero setup: pip install duckdb, then import duckdb
  • In-process: it runs inside Python, like SQLite, with no server, user or password, unlike PostgreSQL
  • Reads data by name: a Parquet or CSV path, or a pandas DataFrame, goes straight in FROM
  • Columnar and parallel, so the same GROUP BY works on 200 million rows
  • Version used here: DuckDB 1.5.5. Every query runs unchanged on 2.0

First queries

What a table is

  • A table is a grid: rows are observations, columns are variables
  • Each column has one type. Ours are VARCHAR (text), SMALLINT (whole numbers) and DOUBLE (decimals)
  • Other common types: BOOLEAN (true or false), DATE (a day) and TIMESTAMP (a day and a time)
  • Each cell holds one value, or NULL
  • The WDI panel: 217 countries, 8 indicators, 1990 to 2023
  • value holds every number, and indicator says what it measures
  • Filter on indicator first, or you mix dollars with years of life
  • In pandas: a table is a DataFrame, a column a Series, and there is no index

Six rows of the panel:

country iso3 indicator year value
Brazil BRA gdp_per_capita 2019 8771.44
Brazil BRA gdp_per_capita 2020 8435.01
Brazil BRA internet_users_pct 2019 73.91
Brazil BRA internet_users_pct 2020 81.34
Brazil BRA life_expectancy 2019 75.81
Brazil BRA life_expectancy 2020 74.51

No two rows share the same country, indicator and year. Together they identify a row: the idea behind a primary key

Querying a Parquet file

  • The WDI panel is a Parquet file with 59,024 rows
  • Download it here or use ../lecture-19/data/wdi_panel.parquet
  • duckdb.sql() runs a query string and returns a result you can print
  • The path goes in single quotes: single quotes mean text, double quotes a column name
  • No read_parquet, connection or fetchall() needed
  • The column types under the names come from the file
import duckdb

duckdb.sql("""
    SELECT *
    FROM '../lecture-19/data/wdi_panel.parquet'
    LIMIT 5
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ country β”‚  iso3   β”‚   indicator    β”‚ year  β”‚      value       β”‚
β”‚ varchar β”‚ varchar β”‚    varchar     β”‚ int16 β”‚      double      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Aruba   β”‚ ABW     β”‚ co2_per_capita β”‚  1990 β”‚ 3.20303411789078 β”‚
β”‚ Aruba   β”‚ ABW     β”‚ co2_per_capita β”‚  1991 β”‚ 3.43571688721622 β”‚
β”‚ Aruba   β”‚ ABW     β”‚ co2_per_capita β”‚  1992 β”‚  3.6345192377364 β”‚
β”‚ Aruba   β”‚ ABW     β”‚ co2_per_capita β”‚  1993 β”‚ 3.41453484426953 β”‚
β”‚ Aruba   β”‚ ABW     β”‚ co2_per_capita β”‚  1994 β”‚ 3.68322701204975 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Creating a view

  • A view is a saved query that behaves like a table
  • CREATE OR REPLACE VIEW wdi AS ... names it, so later queries say FROM wdi
  • A view holds no data: DuckDB re-reads the file each time
  • It disappears when Python restarts
  • OR REPLACE overwrites an old view, so the cell is safe to re-run
  • COUNT(DISTINCT country) counts the different values in a column
duckdb.sql("""
    CREATE OR REPLACE VIEW wdi AS
    SELECT * FROM '../lecture-19/data/wdi_panel.parquet'
""")

duckdb.sql("""
    SELECT COUNT(*) AS n_rows,
           COUNT(DISTINCT country) AS countries,
           COUNT(DISTINCT indicator) AS indicators
    FROM wdi
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ n_rows β”‚ countries β”‚ indicators β”‚
β”‚ int64  β”‚   int64   β”‚   int64    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  59024 β”‚       217 β”‚          8 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

DESCRIBE and SUMMARIZE

  • Run both before writing a query, to learn the column names
  • DESCRIBE lists the columns and their types
  • country, iso3 and indicator are VARCHAR, so filter them with single quotes
  • SUMMARIZE adds min, max, count and the share of missing values
  • value is 11.06% missing. country and year have no gaps
  • value mixes all eight indicators, so its min (0% internet use in 1990) and max (India’s population in 2023) mean little
  • Its result is a table, so we can SELECT from it
  • In pandas: df.dtypes and df.describe()
duckdb.sql("DESCRIBE SELECT * FROM wdi")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ column_name β”‚ column_type β”‚  null   β”‚   key   β”‚ default β”‚  extra  β”‚
β”‚   varchar   β”‚   varchar   β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ country     β”‚ VARCHAR     β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚
β”‚ iso3        β”‚ VARCHAR     β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚
β”‚ indicator   β”‚ VARCHAR     β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚
β”‚ year        β”‚ SMALLINT    β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚
β”‚ value       β”‚ DOUBLE      β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
duckdb.sql("""
    SELECT column_name, min, max, null_percentage
    FROM (SUMMARIZE SELECT country, year, value FROM wdi)
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ column_name β”‚     min     β”‚     max      β”‚ null_percentage β”‚
β”‚   varchar   β”‚   varchar   β”‚   varchar    β”‚  decimal(9,2)   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ country     β”‚ Afghanistan β”‚ Zimbabwe     β”‚            0.00 β”‚
β”‚ year        β”‚ 1990        β”‚ 2023         β”‚            0.00 β”‚
β”‚ value       β”‚ 0.0         β”‚ 1438069596.0 β”‚           11.06 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

SELECT, aliases, ORDER BY, LIMIT

  • Upper case is a convention: keywords and column names ignore case, so select and Country work too
  • SELECT lists the columns you want, and * means all of them
  • It can compute new columns, like ROUND(value, 0)
  • AS gives a column a new name, called an alias
  • ORDER BY value DESC sorts high to low. ASC (low to high) is the default
  • LIMIT keeps the first rows, like .head() in pandas
duckdb.sql("""
    SELECT country, year,
           ROUND(value, 0) AS gdp_pc
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND year = 2020
    ORDER BY value DESC
    LIMIT 5
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   country   β”‚ year  β”‚  gdp_pc  β”‚
β”‚   varchar   β”‚ int16 β”‚  double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Monaco      β”‚  2020 β”‚ 161263.0 β”‚
β”‚ Luxembourg  β”‚  2020 β”‚ 105274.0 β”‚
β”‚ Bermuda     β”‚  2020 β”‚  98846.0 β”‚
β”‚ Isle of Man β”‚  2020 β”‚  86595.0 β”‚
β”‚ Switzerland β”‚  2020 β”‚  86294.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

WHERE with AND, OR and NOT

  • WHERE keeps rows where the condition is true, like boolean indexing in pandas
  • Comparisons: =, <> (not equal), <, <=, >, >=
  • AND needs both sides true, OR needs either
  • NOT flips a condition: NOT country = 'Japan' would keep only Brazil
  • Text matches are case-sensitive
  • The brackets matter, because AND runs before OR
duckdb.sql("""
    SELECT country, year,
           ROUND(value, 0) AS gdp_pc
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND (year = 2000 OR year = 2020)
      AND (country = 'Brazil' OR country = 'Japan')
    ORDER BY country, year
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ country β”‚ year  β”‚ gdp_pc  β”‚
β”‚ varchar β”‚ int16 β”‚ double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brazil  β”‚  2000 β”‚  6818.0 β”‚
β”‚ Brazil  β”‚  2020 β”‚  8435.0 β”‚
β”‚ Japan   β”‚  2000 β”‚ 31779.0 β”‚
β”‚ Japan   β”‚  2020 β”‚ 35363.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Without the brackets, the query returns 496 rows: GDP for every country in 2000, all of Brazil’s 2020 rows, and every Japanese row

IN and BETWEEN

  • IN replaces a chain of ORs on one column
  • country IN ('Brazil', 'China') means country = 'Brazil' OR country = 'China'
  • BETWEEN includes both ends: 2018 to 2020 covers three years
  • The low end goes first. BETWEEN 2020 AND 2018 returns nothing
  • In pandas: .isin([...]) and .between(2018, 2020)
  • Six countries Γ— three years = 18 rows. LIMIT 6 shows Brazil and China
duckdb.sql("""
    SELECT country, year,
           ROUND(value, 0) AS gdp_pc
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND country IN ('Brazil', 'China', 'Germany',
                      'India', 'Japan', 'United States')
      AND year BETWEEN 2018 AND 2020
    ORDER BY country, year
    LIMIT 6
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ country β”‚ year  β”‚ gdp_pc  β”‚
β”‚ varchar β”‚ int16 β”‚ double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brazil  β”‚  2018 β”‚  8722.0 β”‚
β”‚ Brazil  β”‚  2019 β”‚  8771.0 β”‚
β”‚ Brazil  β”‚  2020 β”‚  8435.0 β”‚
β”‚ China   β”‚  2018 β”‚  9799.0 β”‚
β”‚ China   β”‚  2019 β”‚ 10356.0 β”‚
β”‚ China   β”‚  2020 β”‚ 10573.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LIKE and ILIKE

  • LIKE matches part of a text value
  • % means any characters, or none. _ means exactly one
  • 'United%' starts with United, '%States' ends with States, '%King%' contains King
  • DISTINCT removes duplicates. Without it, each country appears once per indicator and year
  • LIKE is case-sensitive, so 'united%' finds nothing. ILIKE ignores case
  • In pandas: .str.startswith() or .str.contains()
duckdb.sql("""
    SELECT DISTINCT country
    FROM wdi
    WHERE country LIKE 'United%'
    ORDER BY country
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       country        β”‚
β”‚       varchar        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United Arab Emirates β”‚
β”‚ United Kingdom       β”‚
β”‚ United States        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
duckdb.sql("""
    SELECT DISTINCT country
    FROM wdi
    WHERE country ILIKE 'united%'
    ORDER BY country
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       country        β”‚
β”‚       varchar        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United Arab Emirates β”‚
β”‚ United Kingdom       β”‚
β”‚ United States        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Every clause in one query

Clause What it does
FROM wdi picks the table
WHERE keeps 6 of 59,024 rows
SELECT picks 3 columns, rounds one
ORDER BY sorts by value
LIMIT keeps 3 rows
  • You have met every clause here
  • Read a query from FROM, because that is where the engine starts
  • Three rows survive: the United States (59,534), Germany (42,373) and Japan (35,363)
  • We build on this query for the rest of the lecture
duckdb.sql("""
    SELECT country, year, ROUND(value, 0) AS gdp_pc
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND country IN ('Brazil', 'China', 'Germany',
                      'India', 'Japan', 'United States')
      AND year = 2020
    ORDER BY value DESC
    LIMIT 3
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚ year  β”‚ gdp_pc  β”‚
β”‚    varchar    β”‚ int16 β”‚ double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United States β”‚  2020 β”‚ 59534.0 β”‚
β”‚ Germany       β”‚  2020 β”‚ 42373.0 β”‚
β”‚ Japan         β”‚  2020 β”‚ 35363.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

How the engine evaluates a query

  • You write SELECT first, but the engine runs it fifth
  • FROM finds the rows, WHERE filters them, GROUP BY makes groups, HAVING filters groups, then SELECT builds the output
  • ORDER BY sorts what SELECT produced, and LIMIT runs last of all
  • An aggregate cannot go in WHERE: the groups do not exist yet
  • pandas writes the steps in this order: df[mask].groupby(...).agg(...)

Aggregating

GROUP BY

  • An aggregate function turns many rows into one number: COUNT, SUM, AVG (mean), MIN, MAX
  • Without GROUP BY, the whole table is one group, so one row
  • GROUP BY country makes six groups, so six rows
  • Every column in SELECT must be in GROUP BY or inside an aggregate
    • SELECT country, value ... GROUP BY country fails: SQL will not pick one of 14 values
  • n is 14: the years 2010 to 2023
  • In pandas: .groupby("country").agg(...)
  • Two grouping columns give one row per pair: Appendix 03
duckdb.sql("""
    SELECT country,
           COUNT(value) AS n,
           ROUND(AVG(value), 0) AS mean_gdp
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND country IN ('Brazil', 'China', 'Germany',
                      'India', 'Japan', 'United States')
      AND year >= 2010
    GROUP BY country
    ORDER BY mean_gdp DESC
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚   n   β”‚ mean_gdp β”‚
β”‚    varchar    β”‚ int64 β”‚  double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United States β”‚    14 β”‚  58330.0 β”‚
β”‚ Germany       β”‚    14 β”‚  42448.0 β”‚
β”‚ Japan         β”‚    14 β”‚  35682.0 β”‚
β”‚ China         β”‚    14 β”‚   9017.0 β”‚
β”‚ Brazil        β”‚    14 β”‚   8923.0 β”‚
β”‚ India         β”‚    14 β”‚   1694.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

WHERE and HAVING

  • WHERE filters rows before grouping. HAVING filters groups after
  • WHERE cannot use an aggregate. HAVING can, because the groups exist by then
  • β€œWhich years count?” is about rows, so it goes in WHERE
  • β€œWhich countries qualify?” is about groups, so it goes in HAVING
  • HAVING AVG(value) > 30000 drops China (9,017), Brazil (8,923) and India (1,694)
  • ORDER BY can use the alias mean_gdp, because it runs after SELECT
duckdb.sql("""
    SELECT country,
           COUNT(value) AS n,
           ROUND(AVG(value), 0) AS mean_gdp
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND country IN ('Brazil', 'China', 'Germany',
                      'India', 'Japan', 'United States')
      AND year >= 2010
    GROUP BY country
    HAVING AVG(value) > 30000
    ORDER BY mean_gdp DESC
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚   n   β”‚ mean_gdp β”‚
β”‚    varchar    β”‚ int64 β”‚  double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United States β”‚    14 β”‚  58330.0 β”‚
β”‚ Germany       β”‚    14 β”‚  42448.0 β”‚
β”‚ Japan         β”‚    14 β”‚  35682.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Try it yourself: fix the query

  1. Run the query on the right
  2. Read the error message
  3. Fix one thing, then run it again
  4. Repeat until it works
  • There are five bugs: three easy, two harder
  • Errors and answers: Appendix 09
duckdb.sql("""
    SELCT country ROUND(AVG(value), 0) AS mean_gdp
    FROM wdi
    WHERE indicator = "gdp_per_capita"
      AND year => 2010
      AND AVG(value) > 30000
    GROUP BY country
    ORDER BY mean_gdp DESC
    LIMIT 5
""")

COUNT(*) and COUNT(value)

  • COUNT(*) counts rows, COUNT(value) only rows where value is present
  • The difference is the number of missing values
  • AVG, SUM, MIN and MAX skip missing values without warning
  • Missing GDP falls over time: 25 of 217 countries in 1990, 18 in 2000, 9 in 2020
  • In pandas: len(df) against df["value"].count()
  • Check this before you trust a mean
duckdb.sql("""
    SELECT year,
           COUNT(*) AS n_rows,
           COUNT(value) AS with_value,
           COUNT(*) - COUNT(value) AS missing
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND year IN (1990, 2000, 2020)
    GROUP BY year
    ORDER BY year
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ year  β”‚ n_rows β”‚ with_value β”‚ missing β”‚
β”‚ int16 β”‚ int64  β”‚   int64    β”‚  int64  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  1990 β”‚    217 β”‚        192 β”‚      25 β”‚
β”‚  2000 β”‚    217 β”‚        199 β”‚      18 β”‚
β”‚  2020 β”‚    217 β”‚        208 β”‚       9 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

NULLs, CASE and windows

Missing values

  • NULL means β€œunknown”, not zero or an empty string
  • Any comparison with NULL gives NULL, so NULL = NULL never matches
  • Test with IS NULL or IS NOT NULL
  • COALESCE(a, b) returns a, or b if a is NULL
  • Yemen has no 2020 GDP figure, so it shows 0 here
  • Filling with 0 is a choice, and a mean over this column would be wrong
  • COALESCE fills a missing value. A missing row needs a LEFT JOIN
  • In pandas, NULL is NaN, and .fillna(0) does this
duckdb.sql("""
    SELECT country,
           ROUND(COALESCE(value, 0), 0) AS gdp_filled
    FROM wdi
    WHERE indicator = 'gdp_per_capita' AND year = 2020
      AND country IN ('Brazil', 'China', 'Germany',
                      'India', 'Japan', 'United States',
                      'Yemen, Rep.')
    ORDER BY country
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚ gdp_filled β”‚
β”‚    varchar    β”‚   double   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brazil        β”‚     8435.0 β”‚
β”‚ China         β”‚    10573.0 β”‚
β”‚ Germany       β”‚    42373.0 β”‚
β”‚ India         β”‚     1807.0 β”‚
β”‚ Japan         β”‚    35363.0 β”‚
β”‚ United States β”‚    59534.0 β”‚
β”‚ Yemen, Rep.   β”‚        0.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

CASE

  • CASE is an if-else ladder inside SELECT
  • WHEN gives a condition, and THEN the value if it holds
  • Tests run top to bottom, and the first true one wins
  • ELSE catches the rest, and END closes the ladder
  • Without ELSE, unmatched rows get NULL
  • The cut-offs are the World Bank’s FY24 bands, set for GNI per capita, so here they are a rough guide
  • Without the IS NULL line, Yemen would fall to ELSE as β€œLow income”
  • The result is a new column you can group, join or export
duckdb.sql("""
    SELECT country, ROUND(value, 0) AS gdp_pc,
        CASE
            WHEN value IS NULL  THEN 'No data'
            WHEN value >= 13846 THEN 'High income'
            WHEN value >= 4466  THEN 'Upper middle'
            WHEN value >= 1136  THEN 'Lower middle'
            ELSE 'Low income'
        END AS income_band
    FROM wdi
    WHERE indicator = 'gdp_per_capita' AND year = 2020
      AND country IN ('Brazil', 'China', 'Germany', 'India',
                      'Japan', 'United States', 'Yemen, Rep.')
    ORDER BY value DESC NULLS LAST
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚ gdp_pc  β”‚ income_band  β”‚
β”‚    varchar    β”‚ double  β”‚   varchar    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United States β”‚ 59534.0 β”‚ High income  β”‚
β”‚ Germany       β”‚ 42373.0 β”‚ High income  β”‚
β”‚ Japan         β”‚ 35363.0 β”‚ High income  β”‚
β”‚ China         β”‚ 10573.0 β”‚ Upper middle β”‚
β”‚ Brazil        β”‚  8435.0 β”‚ Upper middle β”‚
β”‚ India         β”‚  1807.0 β”‚ Lower middle β”‚
β”‚ Yemen, Rep.   β”‚    NULL β”‚ No data      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Window functions

  • A window function keeps every row and adds a group number beside it
  • OVER turns an aggregate into a window function
  • PARTITION BY year makes one window, a set of rows, per year
  • GROUP BY year would collapse these 12 rows into 2. Here, no rows are lost
  • year_mean repeats on each row of its year: 20,885 in 2000, 26,347 in 2020
  • In pandas: .groupby("year")["value"].transform("mean")
duckdb.sql("""
    SELECT country, year, ROUND(value, 0) AS gdp_pc,
           ROUND(AVG(value) OVER (PARTITION BY year), 0)
               AS year_mean
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND country IN ('Brazil', 'China', 'Germany',
                      'India', 'Japan', 'United States')
      AND year IN (2000, 2020)
    ORDER BY year, value DESC
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚ year  β”‚ gdp_pc  β”‚ year_mean β”‚
β”‚    varchar    β”‚ int16 β”‚ double  β”‚  double   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United States β”‚  2000 β”‚ 48616.0 β”‚   20885.0 β”‚
β”‚ Germany       β”‚  2000 β”‚ 35104.0 β”‚   20885.0 β”‚
β”‚ Japan         β”‚  2000 β”‚ 31779.0 β”‚   20885.0 β”‚
β”‚ Brazil        β”‚  2000 β”‚  6818.0 β”‚   20885.0 β”‚
β”‚ China         β”‚  2000 β”‚  2237.0 β”‚   20885.0 β”‚
β”‚ India         β”‚  2000 β”‚   757.0 β”‚   20885.0 β”‚
β”‚ United States β”‚  2020 β”‚ 59534.0 β”‚   26347.0 β”‚
β”‚ Germany       β”‚  2020 β”‚ 42373.0 β”‚   26347.0 β”‚
β”‚ Japan         β”‚  2020 β”‚ 35363.0 β”‚   26347.0 β”‚
β”‚ China         β”‚  2020 β”‚ 10573.0 β”‚   26347.0 β”‚
β”‚ Brazil        β”‚  2020 β”‚  8435.0 β”‚   26347.0 β”‚
β”‚ India         β”‚  2020 β”‚  1807.0 β”‚   26347.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  12 rows                           4 columns

Ranking with a window

  • RANK() numbers rows by the ORDER BY inside OVER
  • The inner ORDER BY sets the ranks, the outer one sorts the printed table
  • No PARTITION BY, so one window covers the whole result. Add PARTITION BY year to rank within each year
  • Ties share a rank and skip the next: 1, 2, 2, 4
  • There are variants that handle ties differently. RANK() is the one you’ll use most
  • The United States ranks 1 (59,534), and India 6 (1,807)
  • WHERE runs first, so only these six countries are ranked
duckdb.sql("""
    SELECT country, ROUND(value, 0) AS gdp_pc,
           RANK() OVER (ORDER BY value DESC) AS rank_2020
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND country IN ('Brazil', 'China', 'Germany',
                      'India', 'Japan', 'United States')
      AND year = 2020
    ORDER BY rank_2020
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚ gdp_pc  β”‚ rank_2020 β”‚
β”‚    varchar    β”‚ double  β”‚   int64   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ United States β”‚ 59534.0 β”‚         1 β”‚
β”‚ Germany       β”‚ 42373.0 β”‚         2 β”‚
β”‚ Japan         β”‚ 35363.0 β”‚         3 β”‚
β”‚ China         β”‚ 10573.0 β”‚         4 β”‚
β”‚ Brazil        β”‚  8435.0 β”‚         5 β”‚
β”‚ India         β”‚  1807.0 β”‚         6 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Joins

Two small tables

  • A table stores its data, unlike the wdi view
  • CREATE TABLE ... AS SELECT builds gdp2020 from a query
  • CREATE TABLE (col TYPE, ...) declares empty columns
  • INSERT INTO ... VALUES adds rows
  • A primary key identifies each row, with no duplicates or NULLs: iso3 in regions
  • A foreign key points at another table’s primary key. iso3 in gdp2020 plays that role
  • We never declare it, so JPN can sit there with no partner
  • Six rows in gdp2020, seven in regions
  • Mismatches: JPN is not in regions. KOR and AUS are not in gdp2020
  • East Asia holds two codes, which matters in the exercise
duckdb.sql("""
    CREATE OR REPLACE TABLE gdp2020 AS
    SELECT country, iso3, ROUND(value, 0) AS gdp_per_capita
    FROM wdi
    WHERE indicator = 'gdp_per_capita' AND year = 2020
      AND iso3 IN ('USA','BRA','CHN','IND','DEU','JPN')
""")

duckdb.sql("""
    CREATE OR REPLACE TABLE regions
        (iso3 VARCHAR PRIMARY KEY, region VARCHAR)
""")

duckdb.sql("""
    INSERT INTO regions VALUES
        ('USA', 'North America'), ('BRA', 'Latin America'),
        ('CHN', 'East Asia'),     ('IND', 'South Asia'),
        ('DEU', 'Europe'),        ('KOR', 'East Asia'),
        ('AUS', 'Oceania')
""")

duckdb.sql("SELECT * FROM regions ORDER BY iso3")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  iso3   β”‚    region     β”‚
β”‚ varchar β”‚    varchar    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ AUS     β”‚ Oceania       β”‚
β”‚ BRA     β”‚ Latin America β”‚
β”‚ CHN     β”‚ East Asia     β”‚
β”‚ DEU     β”‚ Europe        β”‚
β”‚ IND     β”‚ South Asia    β”‚
β”‚ KOR     β”‚ East Asia     β”‚
β”‚ USA     β”‚ North America β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

INNER JOIN

  • A JOIN combines two tables: each matching pair of rows becomes one output row
  • ON says which columns must match: iso3 on both sides
  • AS g and AS r are table aliases. g.country means β€œcountry from gdp2020”, so prefix every column this way
  • The join type goes before JOIN. An INNER JOIN, the default, keeps only the codes in both tables
  • Six rows on the left, seven on the right, five out
  • Japan drops out (JPN is not in regions), and so do Korea and Australia (not in gdp2020)
  • An inner join drops rows without warning, so check your row count after a join
  • In pandas: pd.merge(gdp2020, regions, on="iso3"), where how="inner" is the default
duckdb.sql("""
    SELECT g.country, r.region, g.gdp_per_capita
    FROM gdp2020 AS g
    INNER JOIN regions AS r ON g.iso3 = r.iso3
    ORDER BY g.country
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚    region     β”‚ gdp_per_capita β”‚
β”‚    varchar    β”‚    varchar    β”‚     double     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brazil        β”‚ Latin America β”‚         8435.0 β”‚
β”‚ China         β”‚ East Asia     β”‚        10573.0 β”‚
β”‚ Germany       β”‚ Europe        β”‚        42373.0 β”‚
β”‚ India         β”‚ South Asia    β”‚         1807.0 β”‚
β”‚ United States β”‚ North America β”‚        59534.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

LEFT JOIN

  • A LEFT JOIN keeps every row of the left table, the one in FROM (gdp2020 here)
  • Six rows: Japan is back, with NULL for region
  • That NULL means β€œno partner found”. Japan’s 35,363 is unchanged
  • Korea and Australia are still missing: they exist only on the right
  • The join you will use most: it adds labels without losing rows
  • Swap the tables and you get a different answer, as in the exercise
  • In pandas: pd.merge(..., how="left")
duckdb.sql("""
    SELECT g.country, r.region, g.gdp_per_capita
    FROM gdp2020 AS g
    LEFT JOIN regions AS r ON g.iso3 = r.iso3
    ORDER BY g.country
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚    region     β”‚ gdp_per_capita β”‚
β”‚    varchar    β”‚    varchar    β”‚     double     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brazil        β”‚ Latin America β”‚         8435.0 β”‚
β”‚ China         β”‚ East Asia     β”‚        10573.0 β”‚
β”‚ Germany       β”‚ Europe        β”‚        42373.0 β”‚
β”‚ India         β”‚ South Asia    β”‚         1807.0 β”‚
β”‚ Japan         β”‚ NULL          β”‚        35363.0 β”‚
β”‚ United States β”‚ North America β”‚        59534.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

FULL OUTER JOIN

  • A FULL OUTER JOIN keeps every row from both tables, with NULLs where there is no partner
  • Eight rows: five pairs, plus Japan, Korea and Australia
  • Korea and Australia have NULL country and gdp_per_capita. Japan has NULL region columns
  • On these tables: 5 rows from INNER, 6 from LEFT, 8 from FULL OUTER
  • To choose a join, ask which table must survive whole, and what to do with rows that have no partner
  • All three joins side by side: Appendix 05
duckdb.sql("""
    SELECT g.country, r.iso3 AS region_iso3,
           r.region, g.gdp_per_capita
    FROM gdp2020 AS g
    FULL OUTER JOIN regions AS r ON g.iso3 = r.iso3
    ORDER BY r.region NULLS LAST, r.iso3
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚ region_iso3 β”‚    region     β”‚ gdp_per_capita β”‚
β”‚    varchar    β”‚   varchar   β”‚    varchar    β”‚     double     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ China         β”‚ CHN         β”‚ East Asia     β”‚        10573.0 β”‚
β”‚ NULL          β”‚ KOR         β”‚ East Asia     β”‚           NULL β”‚
β”‚ Germany       β”‚ DEU         β”‚ Europe        β”‚        42373.0 β”‚
β”‚ Brazil        β”‚ BRA         β”‚ Latin America β”‚         8435.0 β”‚
β”‚ United States β”‚ USA         β”‚ North America β”‚        59534.0 β”‚
β”‚ NULL          β”‚ AUS         β”‚ Oceania       β”‚           NULL β”‚
β”‚ India         β”‚ IND         β”‚ South Asia    β”‚         1807.0 β”‚
β”‚ Japan         β”‚ NULL        β”‚ NULL          β”‚        35363.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Try it yourself: predict the rows

How many rows does each query return? Which rows get a NULL?

regions

iso3 region
AUS Oceania
BRA Latin America
CHN East Asia
DEU Europe
IND South Asia
KOR East Asia
USA North America

gdp2020

iso3 country
BRA Brazil
CHN China
DEU Germany
IND India
JPN Japan
USA United States
-- Query 1
SELECT r.region, g.country
FROM regions AS r
INNER JOIN gdp2020 AS g ON g.iso3 = r.iso3
-- Query 2
SELECT r.iso3, r.region, g.gdp_per_capita
FROM regions AS r
LEFT JOIN gdp2020 AS g ON g.iso3 = r.iso3
-- Query 3
SELECT g.country, r.region
FROM gdp2020 AS g
INNER JOIN regions AS r ON g.iso3 = r.iso3
WHERE r.region = 'East Asia'

Answers: Appendix 10

DuckDB and pandas 🐼

Querying a DataFrame by name

  • DuckDB finds df among your Python variables and reads it in memory
  • SELECT COUNT(*) FROM df gives 59,024, like the Parquet file
  • The same SELECT works on a file, a view or a DataFrame
  • It works with Polars DataFrames too
  • You can join two DataFrames in one query (right)
import pandas as pd

df = pd.read_parquet(
    "../lecture-19/data/wdi_panel.parquet"
)

duckdb.sql("SELECT COUNT(*) AS n FROM df")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”
β”‚   n   β”‚
β”‚ int64 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 59024 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”˜
regions_df = pd.DataFrame({
    "iso3": ["BRA", "JPN"],
    "region": ["Latin America", "East Asia"]
})

duckdb.sql("""
    SELECT r.iso3, r.region, ROUND(d.value, 0) AS gdp_pc
    FROM df AS d
    JOIN regions_df AS r ON d.iso3 = r.iso3
    WHERE d.indicator = 'gdp_per_capita' AND d.year = 2020
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  iso3   β”‚    region     β”‚ gdp_pc  β”‚
β”‚ varchar β”‚    varchar    β”‚ double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ BRA     β”‚ Latin America β”‚  8435.0 β”‚
β”‚ JPN     β”‚ East Asia     β”‚ 35363.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Handing the result back to pandas

  • .df() turns a DuckDB result into a pandas DataFrame
  • Without it, you get a DuckDB result that prints as a text table
  • .pl() gives a Polars DataFrame, and .to_arrow_table() an Arrow table
  • SQL returns 18 rows, and pivot reshapes them into 6 rows by 3 columns
  • DuckDB has PIVOT too, but reshaping reads more easily in pandas
  • After .df(), it is ordinary pandas: .plot(), .to_csv(), seaborn, statsmodels
  • SQL or pandas? Appendix 06
long = duckdb.sql("""
    SELECT country, indicator,
           ROUND(value, 1) AS value
    FROM df
    WHERE year = 2020
      AND iso3 IN ('USA', 'BRA', 'CHN',
                   'IND', 'DEU', 'JPN')
      AND indicator IN ('gdp_per_capita',
                        'life_expectancy',
                        'internet_users_pct')
""").df()

long.pivot(index="country", columns="indicator",
           values="value")
indicator gdp_per_capita internet_users_pct life_expectancy
country
Brazil 8435.0 81.3 74.5
China 10573.4 70.1 78.0
Germany 42372.9 89.8 81.0
India 1806.5 43.4 70.2
Japan 35363.3 90.2 84.6
United States 59533.6 90.3 77.0

Summary

What we learned today

  • SQL is declarative: you describe the answer and the engine plans the work
  • DuckDB needs one import and reads a Parquet file or a pandas DataFrame by name
  • A view names a query. DESCRIBE and SUMMARIZE show what is in it
  • WHERE filters rows, GROUP BY forms groups, HAVING filters groups. No aggregate in WHERE
  • COUNT(*) minus COUNT(value) is the number of missing values
  • A NULL means unknown. COALESCE fills one in, and a missing row needs a join instead
  • CASE builds a category column from an if-else ladder
  • A window function keeps every row: AVG ... OVER (PARTITION BY year) adds the year mean, RANK() a position
  • Joins: 5 rows from INNER, 6 from LEFT, 8 from FULL OUTER on the same two tables
  • On 200 million rows, DuckDB runs the same query in 0.53 seconds: Appendix 07

Practice at home: GROUP BY Joins

Thank you very much! 😊

Appendix πŸ“š

Practice at home: GDP by region

How rich is each region, and how much data is it based on?

  1. Use the gdp2020 and regions tables from the joins section
  2. Report one row per region
  3. Count how many of that region’s countries have a 2020 GDP figure
  4. Compute the mean GDP per capita of that region
  5. Keep every region, including the one with no GDP data at all
  6. Sort by mean GDP, highest first
  7. Send the region with no data to the bottom of the list

What to look for:

  • Which table goes on the left if every region must survive?
  • East Asia has two codes but one GDP figure, so COUNT(*) and COUNT(g.country) differ
  • Oceania has no GDP figure at all
  • Six regions come back

Solution: Appendix 01

Appendix 01: Solution to the exercise

  • regions goes on the left, because every region must survive
  • East Asia has two rows in regions, but COUNT(g.country) is 1: Korea’s row has a NULL country
  • COUNT(*) would count that row and report 2
  • Oceania has no GDP figure: its count is 0 and its mean is NULL
  • NULLS LAST sends Oceania to the bottom
  • Six regions, from North America (59,534) down to South Asia (1,807), then Oceania
  • Codes that appear in only one table? Appendix 04

Back to the exercise

duckdb.sql("""
    SELECT r.region,
           COUNT(g.country) AS countries_with_gdp,
           ROUND(AVG(g.gdp_per_capita), 0) AS mean_gdp
    FROM regions AS r
    LEFT JOIN gdp2020 AS g ON g.iso3 = r.iso3
    GROUP BY r.region
    ORDER BY mean_gdp DESC NULLS LAST
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    region     β”‚ countries_with_gdp β”‚ mean_gdp β”‚
β”‚    varchar    β”‚       int64        β”‚  double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ North America β”‚                  1 β”‚  59534.0 β”‚
β”‚ Europe        β”‚                  1 β”‚  42373.0 β”‚
β”‚ East Asia     β”‚                  1 β”‚  10573.0 β”‚
β”‚ Latin America β”‚                  1 β”‚   8435.0 β”‚
β”‚ South Asia    β”‚                  1 β”‚   1807.0 β”‚
β”‚ Oceania       β”‚                  0 β”‚     NULL β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Appendix 02: GROUP BY and HAVING practice

Which countries have had the highest life expectancy since 2010?

  1. Create the wdi view over the panel
  2. Keep only the life_expectancy rows
  3. Keep only the years from 2010 onwards
  4. Compute the mean of value for each country
  5. Keep the countries whose mean is above 82
  6. Sort from highest to lowest
  7. Return the top five, rounded to two decimals
  • WHERE drops rows, HAVING AVG(value) > 82 tests the group
  • ORDER BY names mean_life_exp, the alias created in SELECT
  • Hong Kong and Japan are the only places in the top five with more than a million people
duckdb.sql("""
    SELECT country,
           ROUND(AVG(value), 2) AS mean_life_exp
    FROM wdi
    WHERE indicator = 'life_expectancy'
      AND year >= 2010
    GROUP BY country
    HAVING AVG(value) > 82
    ORDER BY mean_life_exp DESC
    LIMIT 5
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       country        β”‚ mean_life_exp β”‚
β”‚       varchar        β”‚    double     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Monaco               β”‚         85.52 β”‚
β”‚ San Marino           β”‚         84.42 β”‚
β”‚ Hong Kong SAR, China β”‚         84.34 β”‚
β”‚ Andorra              β”‚          83.8 β”‚
β”‚ Japan                β”‚         83.78 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Appendix 03: Grouping by two columns

  • Name two columns and you get one row for each pair that appears in the filtered rows
  • Two countries and two indicators give four rows here
  • Combinations with no rows never appear. SQL does not invent empty groups
  • Japan’s mean life expectancy since 2010 is nine years above Brazil’s. Its CO2 per person is almost four times higher
  • A mean over years like this weights every year equally. Say so when you report it
duckdb.sql("""
    SELECT country, indicator,
           ROUND(AVG(value), 1) AS mean_value
    FROM wdi
    WHERE year >= 2010
      AND country IN ('Brazil', 'Japan')
      AND indicator IN ('life_expectancy',
                        'co2_per_capita')
    GROUP BY country, indicator
    ORDER BY country, indicator
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ country β”‚    indicator    β”‚ mean_value β”‚
β”‚ varchar β”‚     varchar     β”‚   double   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brazil  β”‚ co2_per_capita  β”‚        2.4 β”‚
β”‚ Brazil  β”‚ life_expectancy β”‚       74.8 β”‚
β”‚ Japan   β”‚ co2_per_capita  β”‚        9.3 β”‚
β”‚ Japan   β”‚ life_expectancy β”‚       83.8 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Appendix 04: Anti-joins and UNION

  • An anti-join asks which rows on the left have no partner on the right
  • NOT EXISTS runs a small query for each row and keeps the row when it returns nothing
  • Japan is the only such row here, because JPN is missing from regions
  • A join adds columns. UNION stacks rows from two results that have the same columns
  • UNION removes duplicate rows, UNION ALL keeps them and is faster
  • The tagged result has five rows: Germany, Japan and the United States rich, Brazil and India poor
  • China falls in neither band, so it appears in no row at all
duckdb.sql("""
    SELECT g.country, g.iso3
    FROM gdp2020 AS g
    WHERE NOT EXISTS (
        SELECT 1 FROM regions AS r
        WHERE r.iso3 = g.iso3
    )
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ country β”‚  iso3   β”‚
β”‚ varchar β”‚ varchar β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Japan   β”‚ JPN     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
duckdb.sql("""
    SELECT country, 'rich' AS tag FROM gdp2020
    WHERE gdp_per_capita > 30000
    UNION
    SELECT country, 'poor' AS tag FROM gdp2020
    WHERE gdp_per_capita < 10000
    ORDER BY country
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚   tag   β”‚
β”‚    varchar    β”‚ varchar β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Brazil        β”‚ poor    β”‚
β”‚ Germany       β”‚ rich    β”‚
β”‚ India         β”‚ poor    β”‚
β”‚ Japan         β”‚ rich    β”‚
β”‚ United States β”‚ rich    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Appendix 05: The three joins side by side

Back to FULL OUTER JOIN

Appendix 06: When SQL beats a DataFrame

Reach for SQL when:

  • You are joining two or more tables
  • You are filtering and aggregating a file that is larger than your RAM
  • The question is a GROUP BY with a couple of conditions
  • Someone else has to read the query. A colleague who has never seen pandas can still read your SELECT
  • The data already lives in a database

Stay in pandas when:

  • You need row-by-row logic that has no set-based version
  • You are plotting, or feeding a model
  • You want many small inspectable steps, each one a variable you can look at
  • The reshaping is the hard part, as in the pivot in the DuckDB and pandas section

Want more practice? The course website has a full self-study DuckDB SQL tutorial covering everything on these slides in more depth

Appendix 07: DuckDB on 200 million rows

  • Task: mean value by indicator and decade, on wdi_big.parquet, a synthetic panel of 200,681,600 rows
  • DuckDB: 0.53 s. Dask: 5.39 s. pandas: 9.65 s
  • pandas loads every column into RAM. DuckDB reads only the three columns the query uses
  • Why: columnar storage, vectorised execution, and all cores by default
  • MacBook Pro, Apple M5, 10 cores, 24 GB RAM, macOS 26.6
  • Your seconds will differ, but the ranking usually holds
  • Polars is in the same range as DuckDB (Tutorial 07)
  • The query and scripts: Appendix 08

Back to the summary

Appendix 08: The benchmark query

  • The DuckDB version is the query you already know, with a file path in FROM and a GROUP BY at the end
  • The data is wdi_big.parquet, a synthetic panel of 200,681,600 rows built by cloning the small panel many times
  • The file is not committed, and the script that rebuilds it is in data/make_big_parquet.py
  • The timings were measured once and saved to data/benchmark_results.csv. The deck reads that file rather than re-running anything
  • pandas loads the whole thing and calls groupby, Dask uses a parallel DataFrame, and DuckDB runs SQL straight over the file
import duckdb

duckdb.sql("""
    SELECT indicator,
           (year // 10) * 10 AS decade,
           AVG(value) AS mean_value
    FROM 'data/wdi_big.parquet'
    WHERE year >= 1990
    GROUP BY indicator, decade
""").df()

Appendix 09: Fixing the query

  1. syntax error at or near "SELCT": write SELECT
  2. syntax error at or near "(": add a comma after country
  3. syntax error at or near "=>": write >=
  4. column "gdp_per_capita" not found: use single quotes, because double quotes mean a column name
  5. WHERE clause cannot contain aggregates: move AVG(value) > 30000 to HAVING, after GROUP BY
  • Bugs 1 to 3 are parser errors: the grammar is wrong
  • Bugs 4 and 5 are binder errors: the grammar is fine, but the meaning is not
  • Bug 2’s error points at (, one word after the missing comma
duckdb.sql("""
    SELECT country, ROUND(AVG(value), 0) AS mean_gdp
    FROM wdi
    WHERE indicator = 'gdp_per_capita'
      AND year >= 2010
    GROUP BY country
    HAVING AVG(value) > 30000
    ORDER BY mean_gdp DESC
    LIMIT 5
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    country    β”‚ mean_gdp β”‚
β”‚    varchar    β”‚  double  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Monaco        β”‚ 173083.0 β”‚
β”‚ Liechtenstein β”‚ 167187.0 β”‚
β”‚ Bermuda       β”‚ 107724.0 β”‚
β”‚ Luxembourg    β”‚ 106545.0 β”‚
β”‚ Switzerland   β”‚  86948.0 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Appendix 10: Predicting the rows

  • Query 1: 5 rows. An inner join keeps the same rows whichever table comes first
  • Query 2: 7 rows. Every region survives, and KOR and AUS get NULL GDP
  • Query 3: 1 row. East Asia has two codes, but KOR is not in gdp2020
# Query 1: INNER JOIN, regions on the left
duckdb.sql("""
    SELECT r.region, g.country
    FROM regions AS r
    INNER JOIN gdp2020 AS g ON g.iso3 = r.iso3
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    region     β”‚    country    β”‚
β”‚    varchar    β”‚    varchar    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ North America β”‚ United States β”‚
β”‚ Latin America β”‚ Brazil        β”‚
β”‚ East Asia     β”‚ China         β”‚
β”‚ South Asia    β”‚ India         β”‚
β”‚ Europe        β”‚ Germany       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
# Query 2: LEFT JOIN, regions on the left
duckdb.sql("""
    SELECT r.iso3, r.region, g.gdp_per_capita
    FROM regions AS r
    LEFT JOIN gdp2020 AS g ON g.iso3 = r.iso3
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  iso3   β”‚    region     β”‚ gdp_per_capita β”‚
β”‚ varchar β”‚    varchar    β”‚     double     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ USA     β”‚ North America β”‚        59534.0 β”‚
β”‚ BRA     β”‚ Latin America β”‚         8435.0 β”‚
β”‚ CHN     β”‚ East Asia     β”‚        10573.0 β”‚
β”‚ IND     β”‚ South Asia    β”‚         1807.0 β”‚
β”‚ DEU     β”‚ Europe        β”‚        42373.0 β”‚
β”‚ KOR     β”‚ East Asia     β”‚           NULL β”‚
β”‚ AUS     β”‚ Oceania       β”‚           NULL β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
# Query 3: INNER JOIN, East Asia only
duckdb.sql("""
    SELECT g.country, r.region
    FROM gdp2020 AS g
    INNER JOIN regions AS r ON g.iso3 = r.iso3
    WHERE r.region = 'East Asia'
""")
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ country β”‚  region   β”‚
β”‚ varchar β”‚  varchar  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ China   β”‚ East Asia β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜