Bird
0
0

Given the table orders(order_id, customer_id, amount), what will this query return?

medium📝 query result Q13 of 15
SQL - GROUP BY and HAVING
Given the table orders(order_id, customer_id, amount), what will this query return?
SELECT customer_id, COUNT(*) AS order_count FROM orders WHERE amount > 50 GROUP BY customer_id HAVING order_count > 2;
ACustomers with total amount over 50
BAll customers with orders over 50 regardless of count
CSyntax error because alias can't be used in HAVING
DCustomers with more than 2 orders where each order amount is over 50
Step-by-Step Solution
Solution:
  1. Step 1: Understand alias usage in HAVING

    Many SQL databases allow using column aliases like order_count directly in HAVING clause, but standard SQL does not. However, most practical systems support it.
  2. Step 2: Identify correct HAVING syntax

    Using alias in HAVING is often allowed; thus, the query returns customers with more than 2 orders where each order amount is over 50.
  3. Final Answer:

    Customers with more than 2 orders where each order amount is over 50 -> Option D
  4. Quick Check:

    HAVING filters groups; alias usage depends on SQL dialect [OK]
Quick Trick: Use full aggregate in HAVING or alias depending on SQL dialect [OK]
Common Mistakes:
MISTAKES
  • Using alias in HAVING clause (may be allowed in some SQL dialects)
  • Confusing WHERE and HAVING filters
  • Assuming HAVING filters rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes