Loading
0x40Lesson 5 of 6

Connect tables with JOIN

Combine related rows and learn what happens when a match is missing.

12 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Choose between an inner join and a left join for a question.

JOIN combines rows using a relationship between tables. INNER JOIN returns rows with matches on both sides. LEFT JOIN keeps every row from the left table and fills right-side columns with NULL when there is no match.

SQL example
1SELECT customers.name, orders.order_id, orders.total
2FROM customers
3INNER JOIN orders
4  ON orders.customer_id = customers.customer_id;
5
6SELECT customers.name, orders.order_id
7FROM customers
8LEFT JOIN orders
9  ON orders.customer_id = customers.customer_id;

Qualify column names with their table when names overlap or the query could be ambiguous. The ON condition states how rows relate; join behavior for duplicate matches can produce multiple result rows.

Key takeaways

  • Choose between an inner join and a left join for a question.

  • Use table and column names that make the query easy to read.

  • Check which rows a query affects before relying on its result.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write SQL queries

Write queries against small sample databases and run them locally in your browser. Each test starts from a fresh SQLite database.

Exercise 1

Include customers with no orders

+25 XP

Return every customer’s name and any matching order ID. Customers without orders should still appear. Sort by customer ID, then order ID.

  • All customers and orders
Choose the database engine used when you run tests.
Sample tables and rows

Each test rebuilds an in-memory database for the selected dialect before running your query.

sample-data.sql
CREATE TABLE customers (customer_id INTEGER PRIMARY KEY, name TEXT); CREATE TABLE orders (order_id INTEGER PRIMARY KEY, customer_id INTEGER, total REAL); INSERT INTO customers VALUES (1, 'Sam Lee'), (2, 'Ari Chen'), (3, 'Jo Diaz'); INSERT INTO orders VALUES (101, 1, 20), (102, 1, 15), (103, 3, 9);
query.sql
Loading editor…

Using SQLite 3 (sql.js), an embedded database. Queries run locally in a worker against a fresh in-memory database for each test.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: