Skip to content
/Chapter 5 · Joins
Lesson 5.1·garden_shop
Lesson 5.1

Joining Customers to Orders in SQL

A join lets one result use columns from more than one table.

Real datasets are usually split into related tables. The orders table knows which customer placed an order, but the customer name lives incustomers.

A JOIN matches rows using a shared key. Here, both tables use customer_id.

Follow the key, then filter

Build the join by matching the key columns first, then add filters after the relationship is working. In this lesson, the join creates rows that contain both order details and customer details, so the state filter can come from the customer table.

Pattern
select a.id_column, b.detail_column
from table_a as a
join table_b as b
  on a.shared_id = b.shared_id
Schema · Garden ShopTable · orders6 columns · 24 rows
Table · orders

One row per order. Unshipped orders have a null shipped_date.

6 columns · 24 rows
order_id intcustomer_id intorder_date dateshipped_date datestatus textcoupon_code text
Your task

Join orders to customers. Return the order id, order date, customer first name, customer last name, and order status for orders from PA customers, newest first.

SQL Workbench
query.sqlgarden_shop · SQL engine loading
⌘↵ to run
·
Expected answer
  • Columns: order_id, order_date, first_name, last_name, status.
  • Rows: 9 Pennsylvania customer orders.
  • The first row should be order 19 for Theo Brandt.