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.
select a.id_column, b.detail_column
from table_a as a
join table_b as b
on a.shared_id = b.shared_idSchema · Garden ShopTable · orders6 columns · 24 rows
One row per order. Unshipped orders have a null shipped_date.
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.
- 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.