SQL Beginner Challenge 4: Count Rows with COUNT()

Difficulty: Beginner
Estimated time: 5–10 minutes
SQL concepts: COUNT(), counting rows, aggregates
Goal: Learn how to count records reliably (a daily SQL task).

The Scenario

A product manager asks a simple question:

“How many products are in our catalog?”

You don’t need to list every product—just return the total number of rows in the products table.

Database Table

products

column_nametypedescription
product_idINTEGERUnique product ID
nameTEXTProduct name
categoryTEXTProduct category
priceDECIMALProduct price
stock_qtyINTEGERUnits in stock
created_atTIMESTAMPWhen the product was created

Sample Data (for context)

product_idnamecategoryprice
101Wireless MouseAccessories24.99
102Mechanical KeyboardAccessories89.00
10327-inch MonitorDisplays229.99
104USB-C HubAccessories34.50
105Laptop StandWorkspace39.99

Your Task

Write a SQL query that returns one number:

  • The total count of products in the products table

Expected Output

Your result should look like this:

total_products
5

(The exact number depends on the data in your table. With the sample data above, it would be 5.)

Constraints

  • Use COUNT() to count rows
  • Return a single row with a single column
  • Name the output column: total_products

SELECT
COUNT(*) AS total_products
FROM products;

Explanation

  • COUNT(*) counts every row in the table.
  • AS total_products renames the output column so the result is easy to understand.
  • This is a standard pattern used in analytics, reporting, and debugging.

Common Mistakes

  1. Counting a nullable column
    SELECT COUNT(stock_qty) FROM products; -- may undercount if NULL values exist
  2. Forgetting the alias
    • The query works, but the column name may be unclear.
  3. Expecting multiple rows
    • Aggregate functions like COUNT() return a single result unless used with GROUP BY.

Optional Extension (Mini Bonus)

Try answering:

  1. How many products are in the Accessories category?
  2. How many products have a price greater than 50?

(You can combine WHERE with COUNT().)

Next Challenge

Beginner Challenge #5: Remove Duplicates with DISTINCT

🔗 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.