Module 3 · SQL: Getting Data Out of Databases · Lesson 10 of 51
Joining tables
Why data is split across tables, INNER, LEFT, RIGHT and FULL joins, join keys, finding unmatched rows, and the duplicate-row trap.
In this lesson · about 35 minutes
- Explain why databases split data across tables
- Choose between INNER and LEFT JOIN for a question
- Find rows with no match, and avoid double-counting
Join syntax
SELECT m.name, o.order_id, o.total
FROM members AS m
LEFT JOIN app_orders AS o
ON m.member_id = o.member_id;
AS m gives a table a short alias. ON says which columns must match: the join key.
| Join | Keeps | Use it for |
|---|---|---|
| INNER | Only rows that match on both sides | "Orders with their member's details" |
| LEFT | Every left row; NULLs where there's no match | "All members, and their orders if any"; finding members who never ordered |
| RIGHT | Every right row | Rare: swap the tables and use LEFT |
| FULL OUTER | Everything from both | Reconciling two lists |
Anti-join: to find members who never ordered, LEFT JOIN and keep the rows where the right side is NULL: WHERE o.order_id IS NULL.
Try it · data lab
Join two tables
Five loyalty members and six app orders. Switch the join type and see which rows survive, which get NULLs and which vanish.
members (left table)
| # | member_id | name | home_branch |
|---|---|---|---|
| 0 | 1 | Amara | Downtown |
| 1 | 2 | Bilal | Campus |
| 2 | 3 | Chen | Riverside |
| 3 | 4 | Dawit | Downtown |
| 4 | 5 | Elena | Airport |
5 rows × 3 columns
app_orders (right table)
| # | order_id | member_id | total |
|---|---|---|---|
| 0 | 501 | 1 | 8.5 |
| 1 | 502 | 3 | 4.75 |
| 2 | 503 | 1 | 12 |
| 3 | 504 | 2 | 3.25 |
| 4 | 505 | 7 | 9 |
| 5 | 506 | 3 | 6.5 |
6 rows × 3 columns
Rows in red have no partner in the other table.
SQL
SELECT m.member_id, m.name, o.order_id, o.total FROM members AS m INNER JOIN app_orders AS o ON m.member_id = o.member_id;
Result: 5 rows
| # | member_id | name | order_id | total |
|---|---|---|---|---|
| 0 | 1 | Amara | 501 | 8.5 |
| 1 | 1 | Amara | 503 | 12 |
| 2 | 2 | Bilal | 504 | 3.25 |
| 3 | 3 | Chen | 502 | 4.75 |
| 4 | 3 | Chen | 506 | 6.5 |
5 rows × 4 columns
In pandas the same join is members.merge(app_orders, on="member_id", how="inner").
Joins in SQLite
Exercise
Members who never ordered
Return the name of every member with no app orders, sorted alphabetically. Use a LEFT JOIN and IS NULL.
Code that runs before yours
import sqlite3
import pandas as pd
con = sqlite3.connect(":memory:")
pd.read_csv("kc_orders.csv").to_sql("orders", con, index=False)
q = lambda sql: pd.read_sql_query(sql, con)
pd.DataFrame({"member_id": [1, 2, 3, 4, 5], "name": ["Amara", "Bilal", "Chen", "Dawit", "Elena"],
"home_branch": ["Downtown", "Campus", "Riverside", "Downtown", "Airport"]}).to_sql("members", con, index=False)
pd.DataFrame({"order_id": [501, 502, 503, 504, 505, 506], "member_id": [1, 3, 1, 2, 7, 3],
"total": [8.5, 4.75, 12.0, 3.25, 9.0, 6.5]}).to_sql("app_orders", con, index=False)Exercise
App spend by home branch
Return each member home_branch and the total app spend of its members as spend, counting only matched orders (INNER JOIN). Sort by spend descending.
Code that runs before yours
import sqlite3
import pandas as pd
con = sqlite3.connect(":memory:")
pd.read_csv("kc_orders.csv").to_sql("orders", con, index=False)
q = lambda sql: pd.read_sql_query(sql, con)
pd.DataFrame({"member_id": [1, 2, 3, 4, 5], "name": ["Amara", "Bilal", "Chen", "Dawit", "Elena"],
"home_branch": ["Downtown", "Campus", "Riverside", "Downtown", "Airport"]}).to_sql("members", con, index=False)
pd.DataFrame({"order_id": [501, 502, 503, 504, 505, 506], "member_id": [1, 3, 1, 2, 7, 3],
"total": [8.5, 4.75, 12.0, 3.25, 9.0, 6.5]}).to_sql("app_orders", con, index=False)Check your understanding0 of 3 correct
Quiz score: 0 of 3
Exercises solved: 0 of 2
Written by Noviq Academy in its own words. Kettle & Crumb and all its data are made up for teaching; any resemblance to a real business is a coincidence. Python runs in your browser with Pyodide, so results can differ slightly from your own computer. Power BI, Tableau and other product names belong to their owners. Read-aloud uses your device's built-in voices.