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_name | type | description |
|---|---|---|
| product_id | INTEGER | Unique product ID |
| name | TEXT | Product name |
| category | TEXT | Product category |
| price | DECIMAL | Product price |
| stock_qty | INTEGER | Units in stock |
| created_at | TIMESTAMP | When the product was created |
Sample Data (for context)
| product_id | name | category | price |
|---|---|---|---|
| 101 | Wireless Mouse | Accessories | 24.99 |
| 102 | Mechanical Keyboard | Accessories | 89.00 |
| 103 | 27-inch Monitor | Displays | 229.99 |
| 104 | USB-C Hub | Accessories | 34.50 |
| 105 | Laptop Stand | Workspace | 39.99 |
Your Task
Write a SQL query that returns one number:
- The total count of products in the
productstable
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_productsrenames the output column so the result is easy to understand.- This is a standard pattern used in analytics, reporting, and debugging.
Common Mistakes
- Counting a nullable column
SELECT COUNT(stock_qty) FROM products; -- may undercount if NULL values exist - Forgetting the alias
- The query works, but the column name may be unclear.
- Expecting multiple rows
- Aggregate functions like
COUNT()return a single result unless used withGROUP BY.
- Aggregate functions like
Optional Extension (Mini Bonus)
Try answering:
- How many products are in the
Accessoriescategory? - 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.