Start learning free

LESSON 56 OF 188 · BACKEND: SQL

Join Two Tables in Python

Practice

An order doesn't store its customer's name, only the customer's id, in customer_id. The name lives in the customers table. A JOIN reads from two tables at once, and ON gives the rule that says which rows belong together.

For example, if the shop had a reviews table with a product_id column, this query would show each review's stars next to the product's name:

SELECT reviews.stars, products.name
FROM reviews
JOIN products
ON reviews.product_id = products.id

When a query uses two tables, write each column as table.column: both tables can have a column with the same name, like id. And don't forget ON: without a rule, SQL pairs every row of one table with every row of the other, so 7 orders and 5 customers give 35 rows.

Your query lists each order's id and date with the name of the customer who placed it. The check prints:

id | ordered | name
1 | 2026-03-02 | Ana Pop
2 | 2026-03-15 | Ben Ortiz
3 | 2026-04-05 | Ana Pop
4 | 2026-04-18 | Chloe Ng
5 | 2026-05-07 | Dan Weber
6 | 2026-05-21 | Ben Ortiz
7 | 2026-05-30 | Chloe Ng
(7 rows)

Your task: select orders.id, orders.ordered and customers.name from orders, join customers, and match the rows with ON: the order's customer_id equals the customer's id.


Assignment
Follow the Coding Tutorial to write the query one clause per line: SELECT, FROM, JOIN, then ON.


Hint
Run the code after the JOIN line, before you add ON: 35 rows means every order is paired with every customer. The ON rule brings it down to 7.


STEP 1 OF 4
    Ctrl + Enter to check

    Sql Inner Join in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free