A Simple Guide to the COUNT Function in SQL

Advertisement

Jun 04, 2025 By Tessa Rodriguez

Most people don’t get excited about SQL functions, but if you've ever worked with data, you’ve probably leaned on the COUNT() function more than once. It's simple, dependable, and one of the first tools people learn in SQL. Whether you're checking how many users have signed up, how many orders have been placed, or how many records meet a specific condition, COUNT() provides quick answers.

What makes it more than just basic is its flexibility. You can count all rows, skip nulls, check for unique values, or combine them with other SQL features for more specific results. It's not just a function—it’s a go-to solution for making sense of raw numbers in everyday queries and reports.

What You Can Actually Do With the COUNT Function in SQL?

COUNT(*) – Count All Rows

This is the most common and direct use of COUNT(). When you write COUNT(*), you're asking SQL to count every row in a result set, regardless of what’s in the rows. Even if some columns have NULL values, they still get counted.

SELECT COUNT(*) FROM employees;

This tells you how many total entries are in the employee's table. It’s useful when you're interested in just the quantity, not the content.

COUNT(column_name) – Ignore NULL Values

To count solely the rows containing data in a particular column (not NULL), place the column name in COUNT().

SELECT COUNT(email) FROM users;

This counts how many users have an email address. If some entries have NULL in the email column, they won’t be included in the total. This is handy for checking data completeness.

COUNT(DISTINCT column_name) – Count Unique Non-NULL Values

When you want to count how many unique values exist in a column, COUNT(DISTINCT ...) does the job.

SELECT COUNT(DISTINCT department) FROM employees;

This tells you how many different departments there are on your employee's table. Like the previous example, NULLs are not counted.

COUNT with GROUP BY – Count per Category

Sometimes, you don't just want a single number—you want to see counts broken down by a category. This is where GROUP BY comes in.

SELECT department, COUNT(*) FROM employees GROUP BY department;

This shows how many employees are in each department. It’s one of the most useful combinations of COUNT() because it helps you understand distributions within your data.

COUNT with WHERE – Count Specific Rows

If you only want to count rows that meet certain conditions, use a WHERE clause.

SELECT COUNT(*) FROM orders WHERE status = 'shipped';

This only counts the orders that have a status of "shipped." It filters your results before counting. It’s good for getting numbers tied to business logic or performance metrics.

COUNT in JOIN Queries – Count Matching Records Across Tables

You can use COUNT() after joining multiple tables to see how many records match.

SELECT customers.name, COUNT(orders.id)

FROM customers

LEFT JOIN orders ON customers.id = orders.customer_id

GROUP BY customers.name;

This shows how many orders each customer has. If a customer has no orders, they’ll still appear (because of the LEFT JOIN) with a count of zero. It’s useful when you need a full picture, including the gaps.

COUNT in Subqueries – Count as a Column Value

Sometimes, you don't want to show just the count—you want to include it as part of a larger query.

SELECT name,

(SELECT COUNT(*) FROM orders WHERE orders.customer_id = customers.id) AS total_orders

FROM customers;

This gives a list of customers and shows the number of orders each one has placed. It works like a lookup for each row, embedding the result as a column. It can be slower on big datasets but is flexible for custom reports.

COUNT with HAVING – Filter Groups by Count

If you use GROUP BY, you might want to filter the results by how many items are in each group. You can’t use WHERE for this—use HAVING instead.

SELECT department, COUNT(*)

FROM employees

GROUP BY department

HAVING COUNT(*) > 10;

This lists departments that have more than ten employees. It’s how you apply conditions after the count has been calculated, which is especially helpful when looking for overrepresented or underrepresented groups.

COUNT with CASE – Conditional Counting in One Query

This method lets you count different conditions in the same query using CASE statements inside COUNT(). It’s helpful when you want multiple counts side by side without running multiple queries.

SELECT

COUNT(CASE WHEN status = 'pending' THEN 1 END) AS pending_count,

COUNT(CASE WHEN status = 'shipped' THEN 1 END) AS shipped_count,

COUNT(CASE WHEN status = 'cancelled' THEN 1 END) AS cancelled_count

FROM orders;

This gives you counts for each order status all at once. It’s efficient and keeps your reports cleaner when you need quick stats across categories.

COUNT with OVER() – Count Without Grouping

This use of COUNT() with the OVER() clause lets you get row counts while keeping all individual rows in your result. It works like a window function.

SELECT name, department,

COUNT(*) OVER (PARTITION BY department) AS department_count

FROM employees;

Each row still shows the employee’s name and department, but now it also includes the total number of people in that department—without collapsing rows. This is useful when you need row-level detail along with grouped totals. It’s often used in analytics dashboards or reports where you need both individual and summary data together.

Conclusion

The COUNT() function in SQL appears simple, but it has more depth than most people realize. Used effectively, it helps you understand your data without requiring complicated logic. Whether you're trying to measure activity, find missing information, or build out reports that give real insight, COUNT() is something you’ll use often. And the more you work with it in different contexts—joins, subqueries, conditions, groupings—the more useful it becomes. It's not just a row counter. It's a building block for understanding what’s happening in your data.

Advertisement

Recommended Updates

Applications

Google Launches Tools and Protocol for Building AI Agents

Google debuts new tools and an agent protocol to simplify the creation and management of AI-powered agents.

Applications

AI Image Enhancers That Actually Work: 10 Top Picks for 2025

Looking for the best AI image enhancers in 2025? Discover 10 top tools that improve image quality, sharpen details, and boost resolution with a single click

Technologies

Top 5 Compelling Reasons to Switch from VLOOKUP to INDEX MATCH in Excel

Why INDEX MATCH is often a better choice than VLOOKUP in Excel. Learn the top 5 reasons to use INDEX MATCH for more flexible, efficient, and reliable data lookups

Basics Theory

Choosing Between Alpaca and Vicuna: Which LLM Performs Better

Curious about Vicuna vs Alpaca? This guide compares two open-source LLMs to help you choose the better fit for chat applications, instruction tasks, and real-world use

Technologies

How to Use Permutation and Combination in Python

How to use permutation and combination in Python to solve real-world problems with simple, practical examples. Explore the built-in tools and apply them in coding without complex math

Impact

Top Cloud GPU Providers for 2025 – 9 Best Options for Compute-Intensive Work

Looking for the best cloud GPU providers for 2025? Compare pricing, hardware, and ease of use from trusted names in GPU cloud services

Technologies

How to Use Python’s time.sleep() Like a Pro

How to use the Python time.sleep() function with clear examples. Discover smart ways this sleep function can improve your scripts and automate delays

Technologies

A Simple Guide to the COUNT Function in SQL

How to apply the COUNT function in SQL with 10 clear and practical examples. This guide covers conditional counts, grouping, joins, and more to help you get the most out of SQL queries

Technologies

Mastering the Python strftime() Function for Date Formatting

Explore the Python strftime() function and how it helps convert datetime objects into formatted strings. Learn common usage, tips, and avoid pitfalls in this detailed guide

Applications

What Happens When Writers Use ChatGPT? Honest Pros and Cons

Explore the real pros and cons of using ChatGPT for creative writing. Learn how this AI writing assistant helps generate ideas, draft content, and more—while also understanding its creative limits

Applications

Run Llama 3.1 405B On Vertex AI Without Hassle Today

Need to deploy a 405B-parameter Llama on Vertex AI? Follow these steps for a smooth deployment on Google Cloud

Applications

The Galaxy S24 Series: Samsung’s New Era of Intelligent Phones

Samsung launches world’s smartest AI phone with the new Galaxy S24 series, bringing real-time translation, smart photography, and on-device AI that adapts to your daily routine