Difficulty: Beginner
Estimated time: 10–15 minutes
SQL concepts: COUNT(), WHERE, filtering before aggregation
Goal: Count only the rows that match a specific condition.
The Challenge
The product manager asks:
“How many products cost more than 50?”
This is a very common real-world question.
Instead of counting everything, you’re asked to count only a subset of the data.
Database Table
products
| column_name | type | description |
|---|---|---|
| product_id | INTEGER | Unique product ID |
| name | TEXT | Product name |
| category | TEXT | Product category |
| price | DECIMAL | Product price |
Sample Data
| product_id | name | category | price |
|---|---|---|---|
| 101 | Wireless Mouse | Accessories | 24.99 |
| 102 | Mechanical Keyboard | Accessories | 89.00 |
| 103 | Monitor | Displays | 229.99 |
| 104 | USB-C Hub | Accessories | 34.50 |
| 105 | Laptop Stand | Workspace | 39.99 |
| 106 | Webcam | Accessories | 59.99 |
✍️ Your Task
Write a SQL query that returns:
- The number of products
- Where the price is greater than 50
Expected Output
| count |
|---|
| 3 |
(Mechanical Keyboard, Monitor, Webcam)
✅ Constraints
- Use
COUNT() - Use a
WHEREclause - Do not use
SELECT *
Hint (Optional)
Remember the order of operations in SQL:
WHEREfilters rowsCOUNT()counts what remains
You are not counting all rows — only the filtered ones.
SELECT COUNT(*) AS count
FROM products
WHERE price > 50;
Explanation
WHERE price > 50filters the table firstCOUNT(*)counts the remaining rows- The database returns a single number
This pattern is extremely common in:
- Analytics
- Dashboards
- Business reporting
- KPI calculations
Common Mistakes
- Counting without filtering
SELECT COUNT(*) FROM products;→ Counts everything, not what was asked. - Filtering after counting
(SQL does not work this way.) - Using
COUNT(price)unnecessarilyCOUNT(*)is clearer and safer here.
Optional Extension (Mini Bonus)
Try answering these with SQL:
- How many products cost less than or equal to 30?
- How many products are in the Accessories category?
- How many products cost between 40 and 100?
Why this challenge matters
By completing this challenge, you’ve learned a key insight:
Filtering comes first. Aggregation comes second.
Once this clicks, many real-world SQL problems become much easier.
Next Challenge
Beginner Challenge #23: Counting Values Per Group with GROUP BY
🔗 View reference solution on GitHub
(After you’ve tried the challenge)
Want more practical SQL challenges?
Subscribe to the Solve With SQL newsletter and get new problems delivered to your inbox.