Skip to content

Joined Tables

You'll join two tables that belong together, see exactly which rows fall off the edge, group across the join, and save the answer as a view you can open next month.

About

One table on its own is a list, and a database starts when a second table joins to it. Orders and customers, trips and stations, transactions and categories, games and teams: every real question crosses a table boundary, and the join is how you cross it. This badge is a small relational setup in SQLite or DuckDB with real joins, a CTE that reads top to bottom, and a view. The trap is the join that silently drops rows, which is why you'll count the INNER and LEFT versions and go looking for the orphans. What counts is two or more related tables you loaded yourself and the queries you wrote against them. The Chinook and Northwind sample databases don't count.

At a glance

Per requirement
25 XP
Completion bonus
150 XP
Requirements
6
Takes
A weekend
Skill level
Beginner (done it a few times)
Cost
Free
Supplies
Nothing special
Where
At home
Other people
On your own
Locked for now
Earn two more Common badges in SQL & Databases to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Load two related tables with a shared key and write down which column links them. Public transit portals give you trips and stations. Your bank gives you transactions, and you can make a categories table yourself, with account details blurred in every screenshot. A sports site gives you games and teams. The key is the column that appears in both tables. Draw the two boxes and the line between them. Take a photo of the sketch and a screenshot of both tables loaded. Photo
  2. 2
    Write an INNER JOIN and a LEFT JOIN on the same tables and show that the row counts differ. Run COUNT(*) on each. The difference is the rows with no match in the second table, and an INNER JOIN drops them without a word, which is how a report ends up smaller than the data. Take a screenshot of both queries and both counts. Photo
  3. 3
    List the orphans and fix one. SELECT a.* FROM a LEFT JOIN b ON a.key = b.key WHERE b.key IS NULL gives you every row with no partner. It's usually a renamed station, a category nobody assigned, or a typo in the key. Fix one by adding the missing row to the second table or correcting the key, then rerun the counts. Take a screenshot of the orphan list and the counts after the fix. Photo
  4. 4
    Write a GROUP BY across the join to answer a question neither table could answer alone. Try total trips per station name rather than station id, spending per category label, or points per team city. Take a screenshot of the query and its results. Photo
  5. 5
    Write a CTE, using a WITH clause, that builds a result in two readable steps. The shape is WITH monthly AS (SELECT ... GROUP BY month) SELECT ... FROM monthly WHERE ..., and the alternative is a nested subquery nobody can read a week later. Take a screenshot of the query and the results. Photo
  6. 6
    Save your best query as a VIEW and open the view from a second tool. Start with CREATE VIEW monthly_by_category AS and your query. Then open the view in DB Browser's Browse Data tab, or export it to CSV and chart it in a spreadsheet. The view is the piece that makes next month's analysis one click. Take a screenshot of the view's data in the second tool. Photo