Bird
0
0

How would you modify this query to count only unique customers who made purchases over $500?

hard📝 Application Q9 of 15
SQL - Advanced Query Patterns
How would you modify this query to count only unique customers who made purchases over $500?
SELECT store, COUNT(CASE WHEN purchase_amount > 500 THEN customer_id END) AS high_value_customers FROM sales GROUP BY store;
AUse COUNT(CASE WHEN purchase_amount > 500 THEN 1 END) AS high_value_customers
BUse SUM(CASE WHEN purchase_amount > 500 THEN 1 ELSE 0 END) AS high_value_customers
CUse COUNT(DISTINCT customer_id) AS high_value_customers
DUse COUNT(DISTINCT CASE WHEN purchase_amount > 500 THEN customer_id END) AS high_value_customers
Step-by-Step Solution
Solution:
  1. Step 1: Understand requirement for unique customers

    Counting unique customers requires COUNT DISTINCT on customer_id.
  2. Step 2: Apply conditional aggregation with DISTINCT

    Use COUNT(DISTINCT CASE WHEN condition THEN customer_id END) to count unique customers meeting condition.
  3. Final Answer:

    Use COUNT(DISTINCT CASE WHEN purchase_amount > 500 THEN customer_id END) AS high_value_customers -> Option D
  4. Quick Check:

    COUNT DISTINCT with CASE filters unique conditional values [OK]
Quick Trick: Combine COUNT DISTINCT with CASE for unique conditional counts [OK]
Common Mistakes:
  • Using SUM or COUNT without DISTINCT for unique counts
  • Ignoring condition inside CASE
  • Counting all customers regardless of purchase amount

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes