Bird
0
0

You want to find duplicate entries in a table orders with columns order_id, product_id, and customer_id. Which query efficiently finds product_id values ordered by more than one customer?

hard📝 Application Q8 of 15
SQL - Advanced Query Patterns
You want to find duplicate entries in a table orders with columns order_id, product_id, and customer_id. Which query efficiently finds product_id values ordered by more than one customer?
ASELECT product_id FROM orders GROUP BY product_id HAVING COUNT(DISTINCT customer_id) > 1;
BSELECT product_id FROM orders WHERE COUNT(customer_id) > 1;
CSELECT product_id FROM orders GROUP BY product_id HAVING COUNT(customer_id) > 1;
DSELECT product_id FROM orders GROUP BY customer_id HAVING COUNT(product_id) > 1;
Step-by-Step Solution
Solution:
  1. Step 1: Understand the requirement

    We want product_ids ordered by more than one distinct customer.
  2. Step 2: Use COUNT(DISTINCT customer_id) with GROUP BY product_id

    This counts unique customers per product; HAVING > 1 filters duplicates.
  3. Final Answer:

    SELECT product_id FROM orders GROUP BY product_id HAVING COUNT(DISTINCT customer_id) > 1; -> Option A
  4. Quick Check:

    COUNT(DISTINCT) finds unique customers per product [OK]
Quick Trick: Use COUNT(DISTINCT column) to count unique values in groups [OK]
Common Mistakes:
  • Using WHERE with COUNT
  • Counting without DISTINCT
  • Grouping by wrong column

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes