Bird
0
0

Consider a table orders with columns order_id (unique), customer_id, and coupon_code (nullable). Which query correctly counts the number of orders that used a coupon code?

hard📝 Application Q9 of 15
SQL - Aggregate Functions
Consider a table orders with columns order_id (unique), customer_id, and coupon_code (nullable). Which query correctly counts the number of orders that used a coupon code?
ASELECT COUNT(*) FROM orders WHERE coupon_code IS NOT NULL;
BBoth B and D
CSELECT COUNT(*) FROM orders WHERE coupon_code = NULL;
DSELECT COUNT(coupon_code) FROM orders;
Step-by-Step Solution
Solution:
  1. Step 1: Analyze COUNT(coupon_code)

    Counts rows where coupon_code is NOT NULL.
  2. Step 2: Analyze COUNT(*) with WHERE coupon_code IS NOT NULL

    Also counts rows where coupon_code is NOT NULL.
  3. Step 3: Check invalid option

    coupon_code = NULL is invalid; must use IS NULL.
  4. Final Answer:

    Both B and D -> Option B
  5. Quick Check:

    COUNT(column) and COUNT(*) with IS NOT NULL match [OK]
Quick Trick: COUNT(column) = COUNT(*) with WHERE column IS NOT NULL [OK]
Common Mistakes:
MISTAKES
  • Using = NULL instead of IS NULL
  • Thinking COUNT(column) and COUNT(*) differ here
  • Ignoring NULL filtering in WHERE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes