Problem
Task
Return the number of orders whose discount amount is greater than zero.
Schema
Table Schema
orders(id, customer_id, total_amount, discount)
Input
Sample Data
| id | customer_id | total_amount | discount |
|---|---|---|---|
| 301 | 1 | 80 | 0 |
| 302 | 2 | 120 | 15 |
| 303 | 3 | 45 | 5 |
Output
Expected Output
| discounted_order_count |
|---|
| 2 |
Answer
Check Your Solution
Show Answer and Explanation
Correct Answer
SELECT COUNT(*) AS discounted_order_count
FROM orders
WHERE discount > 0;
Explanation
WHERE first keeps only discounted orders. COUNT(*) then counts the rows that remain.
Common Mistakes
- Counting all rows and forgetting the discount condition.
- Using SUM(discount) when the task asks for the number of orders.
- Using discount IS NOT NULL when zero still means no discount.
Concepts
Related Concepts
COUNT
Filtering Rows
Aggregate Functions
WHERE
Aggregate Function
Next practice