SQL Practice Problem #014

Count orders that used a discount

Return the number of orders whose discount amount is greater than zero.

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

idcustomer_idtotal_amountdiscount
3011800
302212015
3033455

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

Related Problems