SolveWithSQL

SQL Beginner Challenge 22: Counting Rows with Conditions (COUNT + WHERE)

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_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price

Sample Data

product_idnamecategoryprice
101Wireless MouseAccessories24.99
102Mechanical KeyboardAccessories89.00
103MonitorDisplays229.99
104USB-C HubAccessories34.50
105Laptop StandWorkspace39.99
106WebcamAccessories59.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 WHERE clause
  • Do not use SELECT *

Hint (Optional)

Remember the order of operations in SQL:

  1. WHERE filters rows
  2. COUNT() 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 > 50 filters the table first
  • COUNT(*) counts the remaining rows
  • The database returns a single number

This pattern is extremely common in:

  • Analytics
  • Dashboards
  • Business reporting
  • KPI calculations

Common Mistakes

  1. Counting without filtering
    SELECT COUNT(*) FROM products; → Counts everything, not what was asked.
  2. Filtering after counting
    (SQL does not work this way.)
  3. Using COUNT(price) unnecessarily
    • COUNT(*) is clearer and safer here.

Optional Extension (Mini Bonus)

Try answering these with SQL:

  1. How many products cost less than or equal to 30?
  2. How many products are in the Accessories category?
  3. 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.