SQL Practice Problem #005

List customers with their orders

Return the customer name, order id, and total amount for orders that have a matching customer.

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

orders

idcustomer_idtotal_amountordered_at
101158.52026-03-02
10221042026-03-07

Output

Expected Output

nameorder_idtotal_amount
Alice10158.5
Bob102104

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

Related Problems