Bird
Raised Fist0
MySQLquery~10 mins

COUNT function in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - COUNT function
Start Query
↓
Scan Table Rows
↓
Check Condition (if any)
Yes No↓
Include Row
↓
Count Included Rows
↓
Return Count Result
↓
End
The COUNT function scans table rows, includes those matching conditions, counts them, and returns the total count.
Execution Sample
MySQL
SELECT COUNT(*) FROM employees WHERE department = 'Sales';
Counts how many employees work in the Sales department.
Execution Table
StepRow IDDepartmentCondition (department='Sales')Include in Count?Running Count
11SalesTrueYes1
22HRFalseNo1
33SalesTrueYes2
44MarketingFalseNo2
55SalesTrueYes3
66HRFalseNo3
77SalesTrueYes4
Exit----Final count = 4
💡 All rows scanned; count returned for rows where department = 'Sales'
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
Running Count011234
Key Moments - 2 Insights
Why does the count not increase for rows where the department is not 'Sales'?
Because the condition department='Sales' is false for those rows, so they are skipped and not included in the count, as shown in steps 2, 4, and 6 in the execution_table.
What does COUNT(*) count exactly?
COUNT(*) counts all rows that meet the condition, regardless of NULL values in any column. Here, it counts all rows where department='Sales', as seen in the included rows in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Running Count after step 3?
A3
B1
C2
D0
💡 Hint
Check the 'Running Count' column at step 3 in the execution_table.
At which step does the condition department='Sales' become false for the first time?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Condition' column in the execution_table to find the first 'False'.
If the WHERE clause was removed, what would the final count be after scanning 7 rows?
A0
B7
C4
D3
💡 Hint
Without a condition, COUNT(*) counts all rows scanned, so check the total number of rows in the execution_table.
Concept Snapshot
COUNT function syntax: COUNT(*) or COUNT(column)
Counts rows matching optional conditions
Ignores NULLs if counting a specific column
Returns a single number
Useful for totals and summaries
Full Transcript
The COUNT function in SQL counts how many rows match a condition. It scans each row in the table, checks if it meets the condition, and if yes, adds one to the count. For example, counting employees in the Sales department scans all employees, includes only those with department='Sales', and returns the total count. Rows not matching the condition are skipped and do not increase the count. COUNT(*) counts all rows regardless of NULLs. This visual trace shows step-by-step how rows are checked and counted, helping beginners understand how COUNT works internally.

Practice

(1/5)
1. What does the COUNT(*) function do in MySQL?
easy
A. Counts all rows in the table, including those with NULL values
B. Counts only rows where all columns are NOT NULL
C. Counts only rows with NULL values in any column
D. Counts the number of columns in the table

Solution

  1. Step 1: Understand COUNT(*) behavior

    The COUNT(*) function counts every row in the table regardless of NULL values.
  2. Step 2: Compare with other COUNT variants

    Unlike COUNT(column), which counts only non-NULL values in that column, COUNT(*) counts all rows.
  3. Final Answer:

    Counts all rows in the table, including those with NULL values -> Option A
  4. Quick Check:

    COUNT(*) counts all rows [OK]
Hint: COUNT(*) counts every row, NULL or not [OK]
Common Mistakes:
  • Thinking COUNT(*) ignores rows with NULL
  • Confusing COUNT(*) with COUNT(column)
  • Assuming COUNT(*) counts columns
2. Which of the following is the correct syntax to count non-NULL values in the column age from the table users?
easy
A. SELECT COUNT(*) FROM users;
B. SELECT COUNT(age) FROM users WHERE age != NULL;
C. SELECT COUNT(age) FROM users WHERE age = NULL;
D. SELECT COUNT(age) FROM users;

Solution

  1. Step 1: Identify correct COUNT syntax for non-NULL values

    COUNT(column) counts only non-NULL values in that column, so COUNT(age) counts non-NULL ages.
  2. Step 2: Check WHERE clause correctness

    SELECT COUNT(*) FROM users; uses COUNT(*) which counts all rows. Using WHERE age IS NOT NULL is valid but unnecessary with COUNT(age). Options B and C use incorrect NULL comparisons.
  3. Final Answer:

    SELECT COUNT(age) FROM users; -> Option D
  4. Quick Check:

    COUNT(column) counts non-NULL values [OK]
Hint: Use COUNT(column) to count non-NULL values directly [OK]
Common Mistakes:
  • Using WHERE age = NULL instead of IS NOT NULL
  • Using COUNT(*) to count non-NULL values
  • Using != NULL which is invalid in SQL
3. Given the table orders with 5 rows where the discount column has 3 NULL values and 2 non-NULL values, what will this query return?
SELECT COUNT(discount) FROM orders;
medium
A. 5
B. 3
C. 2
D. 0

Solution

  1. Step 1: Understand COUNT(column) counts non-NULL values

    The query counts only rows where discount is NOT NULL.
  2. Step 2: Apply to given data

    There are 2 non-NULL discount values, so the count is 2.
  3. Final Answer:

    2 -> Option C
  4. Quick Check:

    COUNT(discount) = 2 [OK]
Hint: COUNT(column) ignores NULLs, counts only real values [OK]
Common Mistakes:
  • Counting total rows instead of non-NULL values
  • Confusing NULL count with non-NULL count
  • Assuming COUNT(column) counts NULLs
4. Identify the error in this query:
SELECT COUNT(name) FROM employees WHERE name = NULL;
medium
A. Using COUNT(name) instead of COUNT(*)
B. Incorrect NULL comparison with '=' instead of IS NULL
C. Missing GROUP BY clause
D. COUNT cannot be used with WHERE clause

Solution

  1. Step 1: Check WHERE clause NULL comparison

    In SQL, NULL cannot be compared with '='; use IS NULL instead.
  2. Step 2: Validate COUNT usage

    COUNT(name) is valid and WHERE clause is allowed; no GROUP BY needed here.
  3. Final Answer:

    Incorrect NULL comparison with '=' instead of IS NULL -> Option B
  4. Quick Check:

    Use IS NULL for NULL checks [OK]
Hint: Use IS NULL, not = NULL, to check for NULL values [OK]
Common Mistakes:
  • Using '=' to compare NULL values
  • Thinking COUNT can't be combined with WHERE
  • Adding unnecessary GROUP BY
5. You have a table sales with columns region and amount. You want to find how many sales records exist for each region, including regions with zero sales. Which query will correctly show all regions with their sales count, including zero counts?
hard
A. SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region;
B. SELECT region, COUNT(*) FROM sales GROUP BY region;
C. SELECT region, COUNT(amount) FROM sales GROUP BY region;
D. SELECT region, COUNT(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region;

Solution

  1. Step 1: Understand requirement to include regions with zero sales

    To include regions with zero sales, we must use a LEFT JOIN from all regions to sales.
  2. Step 2: Analyze query options

    SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region; uses LEFT JOIN from regions table to sales, counting sales per region including zero counts. Options A, B, D only count existing sales records, missing zero counts.
  3. Final Answer:

    SELECT r.region, COUNT(s.amount) FROM regions r LEFT JOIN sales s ON r.region = s.region GROUP BY r.region; -> Option A
  4. Quick Check:

    LEFT JOIN + COUNT(column) includes zero counts [OK]
Hint: Use LEFT JOIN with COUNT(column) to include zero counts [OK]
Common Mistakes:
  • Using only sales table, missing regions with zero sales
  • Using COUNT(*) which counts joined rows, not zero
  • Not using LEFT JOIN to include all regions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard