0
0
MySQLquery~5 mins

COUNT function in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the COUNT function do in SQL?
The COUNT function counts the number of rows that match a specified condition or all rows if no condition is given.
Click to reveal answer
beginner
How do you count all rows in a table named 'employees'?
Use: SELECT COUNT(*) FROM employees; This counts every row in the 'employees' table.
Click to reveal answer
intermediate
What is the difference between COUNT(*) and COUNT(column_name)?
COUNT(*) counts all rows including those with NULLs. COUNT(column_name) counts only rows where the column is NOT NULL.
Click to reveal answer
beginner
Can COUNT be used with a WHERE clause? Give an example.
Yes. Example: SELECT COUNT(*) FROM employees WHERE department = 'Sales'; counts only employees in Sales.
Click to reveal answer
intermediate
What will SELECT COUNT(DISTINCT department) FROM employees; return?
It returns the number of unique departments in the employees table, ignoring duplicates.
Click to reveal answer
What does COUNT(*) count in a table?
AOnly rows with non-NULL values in the first column
BAll rows including those with NULL values
COnly rows with NULL values
DOnly distinct rows
Which query counts only rows where the 'age' column is NOT NULL?
ASELECT COUNT(age) FROM table;
BSELECT COUNT(*) FROM table;
CSELECT COUNT(DISTINCT age) FROM table;
DSELECT COUNT(NULL) FROM table;
How to count unique values in a column 'city'?
ASELECT COUNT(DISTINCT city) FROM table;
BSELECT COUNT(NULL) FROM table;
CSELECT COUNT(*) FROM table;
DSELECT COUNT(city) FROM table;
What does this query do? SELECT COUNT(*) FROM employees WHERE salary > 50000;
ACounts all employees
BCounts employees with NULL salary
CCounts employees with salary less than 50000
DCounts employees with salary greater than 50000
If a column has NULL values, which COUNT counts them?
ACOUNT(DISTINCT column_name)
BCOUNT(column_name)
CCOUNT(*)
DNone count NULL values
Explain how the COUNT function works and how it handles NULL values.
Think about counting all rows versus counting only non-NULL values in a column.
You got /4 concepts.
    Describe how to count unique values in a column using COUNT.
    Use DISTINCT keyword inside COUNT.
    You got /3 concepts.