sql tutorial · Beginner
Combine rows from related tables with INNER JOIN and LEFT JOIN, and use a LEFT JOIN to find rows with no match.
Good database design keeps each fact in one place. An order stores a customer_id, not the customer's name, so to see who placed each order you join orders to customers on that shared column.
Joins chain together. Here each order item gets its order date and product name. The short names after AS (aliases) keep things readable.
A plain JOIN (an inner join) drops rows that have no match. A LEFT JOIN keeps every row from the first table and fills the missing side with NULL.
Here every customer shows up, including the ones who have never ordered.
exercise Find the customers who have never placed an order. Show their first and last names. Hint: LEFT JOIN, then keep the rows where the order side is NULL.