Joins · Problem 42 of 118

Orders with Customer Names

EasyJoinsShop data

Return order_id, first_name and last_name of the customer for orders 1 to 8, sorted by order_id.

Hint 1

The two tables are linked by customer_id: orders.customer_id = customers.customer_id.

Hint 2

Give the tables short aliases (o, c) so the query stays readable.

Reference solution

SELECT o.order_id, c.first_name, c.last_name
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
WHERE o.order_id BETWEEN 1 AND 8
ORDER BY o.order_id;

A join pairs rows of two tables where the ON condition is true. Here each order is paired with the one customer whose customer_id matches. Without a proper ON condition the database pairs every row with every row (a cross join) and the result explodes: 8 orders times 420 customers is 3,360 rows. If a join result is suddenly much bigger than you expected, check the ON.