Bird
Raised Fist0
MySQLquery~10 mins

COUNT function in MySQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to count all rows in the employees table.

MySQL
SELECT [1](*) FROM employees;
Drag options to blanks, or click blank then click option'
ATOTAL
BSUM
CCOUNT
DNUMBER
Attempts:
3 left
💡 Hint
Common Mistakes
Using SUM instead of COUNT.
Using TOTAL or NUMBER which are not valid SQL functions.
2fill in blank
medium

Complete the code to count only the non-null values in the salary column.

MySQL
SELECT COUNT([1]) FROM employees;
Drag options to blanks, or click blank then click option'
Asalary
BDISTINCT salary
CNULL
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using COUNT(*) which counts all rows regardless of nulls.
Using COUNT(NULL) which always returns zero.
3fill in blank
hard

Fix the error in the code to count distinct departments.

MySQL
SELECT COUNT([1] department) FROM employees;
Drag options to blanks, or click blank then click option'
ASINGLE
BALL
CUNIQUE
DDISTINCT
Attempts:
3 left
💡 Hint
Common Mistakes
Using ALL which counts all values including duplicates.
Using UNIQUE or SINGLE which are not valid SQL keywords here.
4fill in blank
hard

Fill both blanks to count employees with salary greater than 50000.

MySQL
SELECT COUNT([1]) FROM employees WHERE salary [2] 50000;
Drag options to blanks, or click blank then click option'
A*
Bsalary
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using COUNT(salary) which counts only non-null salaries but is less clear here.
Using < instead of > which counts salaries less than 50000.
5fill in blank
hard

Fill all three blanks to count distinct job titles with salary less than 70000.

MySQL
SELECT COUNT([1] job_title) FROM employees WHERE salary [2] [3];
Drag options to blanks, or click blank then click option'
ADISTINCT
B<
C70000
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < which counts salaries greater than 70000.
Not using DISTINCT which counts duplicates.

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