SQL Learning Hub
SQL COUNT Function
SQL COUNT Function
Learn how to count rows and values in your database
Understanding the SQL COUNT Function
The COUNT function is one of SQL's most fundamental and frequently used aggregate functions. It does exactly what its name suggests: it counts things. Whether you need to count the total number of users, the number of videos per creator, the quantity of non-null entries in a column, or the variety of interaction types in your database, COUNT is your go-to tool.
Basic Syntax
The expression can be *, a column name, or DISTINCT column_name.
Common SQL COUNT Interview Questions
- What's the difference between
COUNT(*),COUNT(1), andCOUNT(column_name)? - How do you count only the unique values present in a column?
- Explain how the
COUNTfunction handles NULL values. - How can you get counts for different categories (e.g., count of likes vs. comments) in one query?
- When would you use
COUNTin aHAVINGclause?
SQL COUNT Variations Explained
1. COUNT(*) - Counting All Rows
This is the most common form. COUNT(*) counts all rows returned by the query, including rows with NULL values in some or all columns and duplicate rows. It simply counts the number of rows in the result set defined by the FROM and WHERE clauses.
Result: A single value representing the total number of rows in the interactions table.
2. COUNT(column_name) - Counting Non-NULL Values
When you specify a column name inside COUNT(), it counts the number of rows where the specified column_name is not NULL. It ignores rows where that particular column has a NULL value.
Result: The count of rows where comment_text is not NULL. This effectively counts only the comment interactions.
3. COUNT(DISTINCT column_name) - Counting Unique Non-NULL Values
To count the number of unique, non-NULL values within a specific column, use the DISTINCT keyword before the column name.
Result: The count of unique user_id values present in the videos table. If a user uploaded 10 videos, they are counted only once. NULLs in user_id (if allowed) would be ignored.
4. COUNT(1) or COUNT(constant) - Counting All Rows (Alternative)
COUNT(1) (or COUNT(0), COUNT('any_constant')) behaves identically to COUNT(*). It counts every row returned by the query because the constant expression 1 is never NULL.
Historically, some believed COUNT(1) was faster than COUNT(*), but most modern database optimizers treat them the same. COUNT(*) is generally preferred for clarity as it explicitly means "count rows".
Practical COUNT Examples with TokTuk Data
Example 1: Basic Platform Overview
Get a quick sense of the scale of the TokTuk platform:
This uses scalar subqueries to fetch multiple independent counts in a single result row.
Example 2: Counting with Conditions (WHERE Clause)
Use the WHERE clause to apply conditions before counting:
Only rows meeting the WHERE criteria are considered by COUNT(*).
Example 3: Conditional Counting (CASE within COUNT)
Use CASE expressions inside COUNT to count based on conditions within the same aggregation scope. This is powerful for pivoting or creating summary statistics.
The CASE statement returns 1 (a non-NULL value) when the condition is met, and NULL otherwise. COUNT(expression) only counts the non-NULL results, giving us separate counts for each type.
Summary
The COUNT function is a versatile and essential tool in SQL for summarizing data:
- Use
COUNT(*)orCOUNT(1)to count all rows. - Use
COUNT(column_name)to count non-NULL values in a specific column. - Use
COUNT(DISTINCT column_name)to count unique non-NULL values in a column. - Combine
COUNTwithGROUP BYto get counts for different categories. - Use
HAVINGto filter groups based on their count. - Use conditional logic (
CASEwithinCOUNT) for more complex counting scenarios in a single query.
Mastering the different forms of COUNT and understanding how it interacts with NULLs, DISTINCT, WHERE, GROUP BY, and HAVING is crucial for effective data analysis and reporting in SQL.
Ready for hands-on SQL practice?
We have 200+ questions on real companies and real products.