SQL Practice Problem #016

List emails of customers who ordered Gizmo products

Return each customer email once for customers who ordered at least one product in the Gizmo category.

Problem

Task

Return each customer email once for customers who ordered at least one product in the Gizmo category.

Schema

Table Schema

customers(id, name, email)
products(id, title, category)
orders(id, customer_id, product_id, ordered_at)

Input

Sample Data

customers

idnameemail
1Alicealice@example.com
2Bobbob@example.com

products

idtitlecategory
10Mini GizmoGizmo
11Desk MatOffice

orders

idcustomer_idproduct_idordered_at
4011102026-04-01
4021102026-04-05
4032112026-04-07

Output

Expected Output

email
alice@example.com

Answer

Check Your Solution

Show Answer and Explanation

Correct Answer

SELECT DISTINCT c.email
FROM orders AS o
INNER JOIN customers AS c
  ON o.customer_id = c.id
INNER JOIN products AS p
  ON o.product_id = p.id
WHERE p.category = 'Gizmo';

Explanation

The orders table connects customers and products. DISTINCT is needed because the same customer can order more than one Gizmo product.

Common Mistakes

  • Joining customers directly to products without using orders.
  • Forgetting DISTINCT and returning duplicate customer emails.
  • Filtering c.category even though category belongs to products.

Concepts

Related Concepts

INNER JOIN DISTINCT Foreign Keys JOIN Three Tables

Next practice

Related Problems