Syllabus Lesson 70 of 239 · SQL & Databases
SQL & Databases

Joining Tables

Real data is split across tables to avoid repetition: a customers table and an orders table that references it by id. To answer "who ordered what" you JOIN them on the matching column:

SELECT customers.name, orders.item
FROM orders
JOIN customers ON customers.id = orders.customer_id;

An INNER JOIN (the default JOIN) keeps only rows that match on both sides. This is the single most important SQL skill for real applications.

Write orders_with_names(con) returning a list of (customer_name, item) tuples by joining orders to customers on the customer id, ordered by the order id. Press Run.

Your turn

Write orders_with_names(con) that JOINs orders to customers on customers.id = orders.customer_id and returns a list of (customer_name, item) tuples, ordered by orders.id.

Spotted a problem in this lesson? Report it

Code · runs in your browser
Output