Mission M35 · AI · 20 min

Ask AI for SQL — verify the count

Request from your manager

The loyalty manager asked an AI assistant: "How many loyalty members bought something in 2025?" It answered with SELECT COUNT(*) FROM orders o JOIN customers c ON c.customer_id = o.customer_id WHERE c.loyalty_member = 1 AND o.order_date >= '2025-01-01' — and got 3,344. That seems like a lot for the program. Check it.

Data

Download the practice file(s):

Steps

  1. Run the AI query exactly as written and confirm it returns 3,344.
  2. Ask yourself what one row represents after the join: one order, not one customer.
  3. Check the total: SELECT COUNT(*) FROM customers WHERE loyalty_member = 1. Can 3,344 people come from that?
  4. Fix the query with COUNT(DISTINCT o.customer_id) and an explicit upper date bound.
  5. Tell the AI what was wrong and ask it to explain the difference — a good way to learn and to test it.

Check your answer

How many distinct loyalty members placed at least one order in 2025?

Show solution

COUNT(*) counted orders (3,344). Counting distinct customers gives 800 loyalty members who bought in 2025.

SELECT COUNT(DISTINCT o.customer_id) AS loyalty_buyers_2025
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
WHERE c.loyalty_member = 1
  AND o.order_date BETWEEN '2025-01-01' AND '2025-12-31';

Answer: 800 rows