Problem
Task
Return the customer name, order id, and total amount for orders that have a matching customer.
Schema
Table Schema
customers(id, name, email)
orders(id, customer_id, total_amount, ordered_at)
Input
Sample Data
customers
| id | name | |
|---|---|---|
| 1 | Alice | [email protected] |
| 2 | Bob | [email protected] |
orders
| id | customer_id | total_amount | ordered_at |
|---|---|---|---|
| 101 | 1 | 58.5 | 2026-03-02 |
| 102 | 2 | 104 | 2026-03-07 |
Output
Expected Output
| name | order_id | total_amount |
|---|---|---|
| Alice | 101 | 58.5 |
| Bob | 102 | 104 |
Answer
Check Your Solution
Show Answer and Explanation
Correct Answer
SELECT c.name, o.id AS order_id, o.total_amount
FROM customers AS c
INNER JOIN orders AS o
ON c.id = o.customer_id;
Explanation
INNER JOIN combines rows when the join condition is true. Here, each order is connected to a customer through customers.id = orders.customer_id.
Common Mistakes
- Joining on c.id = o.id instead of c.id = o.customer_id.
- Leaving column names unqualified when both tables contain an id column.
- Using LEFT JOIN when the task asks only for orders with matching customers.
Concepts
Related Concepts
INNER JOIN
Aliases
Foreign Keys
JOIN
Relationships
Next practice