Part 3: Mastering the COUNT() Function

Part 3: Mastering the COUNT() Function | SQL Challenges & Capstone
SQL Aggregation & Grouping Practice · Part 3 · Lecture Notes

Mastering the COUNT() Function

How to measure operational volume and business activity β€” counting total records, filtered subsets, and building the count metrics that power dashboards.

Part 3 of 742% Complete
COUNT(*)
COUNT(column)
WHERE Filter
Business Volume
8 Practice Questions

Learning Objectives

  • Use COUNT(*) to count the total number of rows in a table
  • Use COUNT(column_name) to count only non-NULL values in a specific column
  • Combine COUNT(*) with WHERE to count only records that meet a specific condition
  • Understand that COUNT() always returns a whole number and never requires ROUND()
Click any section header to open or close it

COUNT() counts the number of records inside a table or column. It is the most direct way to measure volume in SQL β€” how many customers, how many transactions, how many open tickets, how many active subscriptions. Like all aggregate functions, COUNT() returns a single value regardless of table size.

In industry, analysts use COUNT() to measure customer activity levels, calculate operational totals, count transactions by type, track support ticket volume, measure campaign reach, and produce the basic count metrics that appear in every business dashboard.

COUNT(*) vs COUNT(column)

COUNT(*) counts every row in the table β€” including rows where some columns are NULL. COUNT(column_name) counts only rows where that specific column is not NULL. For measuring total records, COUNT(*) is the standard choice.

Basic COUNT() Syntax
SELECT COUNT(column_name) FROM table_name; — Or to count all rows: SELECT COUNT(*) FROM table_name;

COUNT(*) is the universal record counter. It counts every row in the table unconditionally β€” it does not care whether specific columns have values or not. For measuring “how many X exist in this table?”, COUNT(*) is always the correct form.

When to Use COUNT(*) vs COUNT(column)

COUNT(*): “how many rows are in this table?” β€” includes NULLs, counts everything. COUNT(column_name): “how many rows have a value in this column?” β€” excludes NULLs. For general volume metrics, always use COUNT(*).

Count All Users
SELECT COUNT(*) AS total_users FROM users;
Count All Orders
SELECT COUNT(*) AS total_orders FROM orders;

Combining COUNT(*) with a WHERE clause counts only the rows that match the filter condition β€” the number of completed orders, the number of active subscriptions, the number of open tickets. This is the standard pattern for business volume reporting by status, type, or category.

WHERE Filters Before COUNT

The clause order is: FROM identifies the table, WHERE filters the rows, THEN COUNT(*) counts the filtered result. WHERE does not go inside COUNT() β€” it goes before it in the standard clause sequence.

Count Completed Orders Only
SELECT COUNT(*) AS completed_orders FROM orders WHERE order_status = ‘Completed’;
Count Active Subscriptions Only
SELECT COUNT(*) AS active_subscriptions FROM subscriptions WHERE status = ‘Active’;

COUNT() is the backbone of operational dashboards. Every KPI that reads “Total X” β€” total customers, total tickets, total campaigns β€” is a COUNT() query under the hood. Analysts run COUNT() queries at the start of any new analysis to understand the volume of data they are working with.

First Query on Any New Table

SELECT COUNT(*) FROM table_name is standard practice when working with an unfamiliar table. It tells the analyst immediately whether they are working with 8 rows or 8 million β€” and whether any WHERE filter has meaningfully narrowed the dataset.

Count All Subscriptions
SELECT COUNT(*) AS total_subscriptions FROM subscriptions;
Count Open Support Tickets
SELECT COUNT(*) AS open_tickets FROM support_tickets WHERE ticket_status = ‘Open’;

Work through each question below. Run your query first, then use Check Answer. A wrong answer reveals the correct SQL with a Copy button; a correct result earns a congratulations message.

Practice 1 Count the total number of users
AI Prompt
Write a MySQL query to count the total number of users.
SQL Answer
SELECT COUNT(*) AS total_users FROM users;
Practice 2 Count the total number of transactions
AI Prompt
Write a MySQL query to count the total number of transactions.
SQL Answer
SELECT COUNT(*) AS total_transactions FROM transactions;
Practice 3 Count the total number of products
AI Prompt
Write a MySQL query to count the total number of products.
SQL Answer
SELECT COUNT(*) AS total_products FROM products;
Practice 4 Count the total number of completed orders
AI Prompt
Write a MySQL query to count the total number of completed orders.
SQL Answer
SELECT COUNT(*) AS completed_orders FROM orders WHERE order_status = ‘Completed’;
Practice 5 Count the total number of active subscriptions
AI Prompt
Write a MySQL query to count the total number of active subscriptions.
SQL Answer
SELECT COUNT(*) AS active_subscriptions FROM subscriptions WHERE status = ‘Active’;
Practice 6 Count the total number of open support tickets
AI Prompt
Write a MySQL query to count the total number of open support tickets.
SQL Answer
SELECT COUNT(*) AS open_tickets FROM support_tickets WHERE ticket_status = ‘Open’;
Practice 7 Count the total number of campaigns using the Email channel
AI Prompt
Write a MySQL query to count the total number of campaigns using the Email channel.
SQL Answer
SELECT COUNT(*) AS email_campaigns FROM campaigns WHERE channel = ‘Email’;
Practice 8 Count the total number of Mobile sessions
AI Prompt
Write a MySQL query to count the total number of Mobile sessions.
SQL Answer
SELECT COUNT(*) AS mobile_sessions FROM sessions WHERE device_type = ‘Mobile’;

Knowledge Check

Five questions β€” select the best answer for each.

1. What does COUNT(*) count?
AOnly rows where all columns have values
BOnly rows where the specified column is not NULL
CEvery row in the table, including rows with NULL values in some columns
DThe number of unique values in the table
2. What is the difference between COUNT(*) and COUNT(column_name)?
AThey always return the same result
BCOUNT(*) counts all rows; COUNT(column_name) skips rows where that column is NULL
CCOUNT(*) is faster; COUNT(column_name) is more accurate
DCOUNT(column_name) counts all rows; COUNT(*) skips NULLs
3. Which query counts only completed orders?
ASELECT COUNT(order_status = ‘Completed’) FROM orders;
BSELECT COUNT(*) FROM orders WHERE order_status = ‘Completed’;
CSELECT COUNT(*) HAVING order_status = ‘Completed’ FROM orders;
DSELECT COUNT(COMPLETED) FROM orders;
4. Does COUNT() require ROUND() like SUM() and AVG()?
AYes β€” COUNT() always produces decimal values
BNo β€” COUNT() always returns a whole number (integer)
CYes β€” only when counting date columns
DIt depends on the size of the table
5. Why do analysts run COUNT(*) as one of the first queries on a new table?
ATo delete any duplicate records before analysis
BTo understand the volume of data they are working with
CTo create an index that speeds up future queries
DTo check for syntax errors in the table structure

Part 3 Summary

The key ideas from this lecture β€” keep these in mind as you move forward.

COUNT() helps analysts measure operational activity, customer volume, and business totals inside organizational datasets. COUNT(*) counts all rows. COUNT(column) skips NULLs. Combine with WHERE to count filtered subsets. COUNT() always returns an integer.

COUNT()COUNT(*)COUNT(column)WHERERecord VolumeBusiness ActivityKPIDashboard