Skip to content

Join two tables and keep the unmatched rows visible

Draft: not yet validated

The code on this page was run inside MESA JupyterLab on 2026-11-16 (pandas 3.1.1, DuckDB 2.0.0) by the site's output check, in a terminal there, and gave the values shown. No person has yet followed the steps, so a menu or a screen may differ. Tell us what you find.

An ordinary join keeps only the rows that find a match. The rest leave without a message, and every count after the join is quietly smaller. A left join keeps them where you can see them.

You will: have a join in which a gauge with no position is still listed, and a count of how many gauges matched.

Time: 10 minutes. You need: the notebook from Load a file from your Data Store into a notebook, open in MESA JupyterLab, with its first cell run.

Steps

  1. Make a table of positions that is incomplete on purpose: only the gauges west of one line of longitude.

    import duckdb
    
    duckdb.sql("CREATE OR REPLACE VIEW west AS SELECT * FROM 'gauge_sites.csv' WHERE longitude < -104.73")
    duckdb.sql("SELECT count(*) AS n FROM west")
    

    You should see: four positions, for five gauges. In a notebook, DuckDB draws the result as a table, with each column's type under its name; the text below holds the same rows.

    ┌───────┐
    │   n   │
    │ int64 │
    ├───────┤
    │     4 │
    └───────┘
    
  2. Join the gauges to those positions in the ordinary way:

    duckdb.sql("""
        SELECT g.gauge_id, g.name, w.longitude, w.latitude
        FROM 'gauges.csv' AS g
        JOIN west AS w ON g.gauge_id = w.gauge_id
        ORDER BY g.gauge_id
    """)
    

    You should see: four rows and no warning. One gauge has gone, and nothing says so.

    ┌──────────┬───────────┬───────────┬──────────┐
    │ gauge_id │   name    │ longitude │ latitude │
    │ varchar  │  varchar  │  double   │  double  │
    ├──────────┼───────────┼───────────┼──────────┤
    │ G01      │ Boathouse │ -104.7612 │  47.1034 │
    │ G02      │ Orchard   │ -104.7391 │  47.1108 │
    │ G04      │ Depot     │ -104.7533 │  47.1175 │
    │ G05      │ Quarry    │ -104.7306 │  47.0962 │
    └──────────┴───────────┴───────────┴──────────┘
    
  3. Change JOIN to LEFT JOIN, which keeps every row of the table named first:

    duckdb.sql("""
        SELECT g.gauge_id, g.name, w.longitude, w.latitude
        FROM 'gauges.csv' AS g
        LEFT JOIN west AS w ON g.gauge_id = w.gauge_id
        ORDER BY g.gauge_id
    """)
    

    You should see: five rows. The gauge without a position is there, with NULL where its position would be.

    ┌──────────┬─────────────┬───────────┬──────────┐
    │ gauge_id │    name     │ longitude │ latitude │
    │ varchar  │   varchar   │  double   │  double  │
    ├──────────┼─────────────┼───────────┼──────────┤
    │ G01      │ Boathouse   │ -104.7612 │  47.1034 │
    │ G02      │ Orchard     │ -104.7391 │  47.1108 │
    │ G03      │ Observatory │      NULL │     NULL │
    │ G04      │ Depot       │ -104.7533 │  47.1175 │
    │ G05      │ Quarry      │ -104.7306 │  47.0962 │
    └──────────┴─────────────┴───────────┴──────────┘
    
  4. Count the matches, so the answer does not depend on reading every row. count(w.gauge_id) counts only the rows where a position was found:

    duckdb.sql("""
        SELECT count(*) AS gauges,
               count(w.gauge_id) AS matched,
               count(*) - count(w.gauge_id) AS unmatched
        FROM 'gauges.csv' AS g
        LEFT JOIN west AS w ON g.gauge_id = w.gauge_id
    """)
    

    You should see:

    ┌────────┬─────────┬───────────┐
    │ gauges │ matched │ unmatched │
    │ int64  │  int64  │   int64   │
    ├────────┼─────────┼───────────┤
    │      5 │       4 │         1 │
    └────────┴─────────┴───────────┘
    
  5. Run the same count against the complete file, 'gauge_sites.csv', in place of west:

    duckdb.sql("""
        SELECT count(*) AS gauges,
               count(s.gauge_id) AS matched,
               count(*) - count(s.gauge_id) AS unmatched
        FROM 'gauges.csv' AS g
        LEFT JOIN 'gauge_sites.csv' AS s ON g.gauge_id = s.gauge_id
    """)
    

    You should see: every gauge matched.

    ┌────────┬─────────┬───────────┐
    │ gauges │ matched │ unmatched │
    │ int64  │  int64  │   int64   │
    ├────────┼─────────┼───────────┤
    │      5 │       5 │         0 │
    └────────┴─────────┴───────────┘
    

Check it worked

In each count, matched plus unmatched equals gauges. You can say which gauge had no position in step 3, and that the complete file leaves none out.

If it didn't

Catalog Error: Table with name west does not exist! The kernel has restarted since step 1, and the view went with it. Run step 1 again, then repeat the step.

Next

Not yet validated by a person in MESA JupyterLab. Drafted with AI assistance on 2026-11-15; the code was run there on 2026-11-16 (pandas 3.1.1, DuckDB 2.0.0).

Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.