COUNT Function

SQL · Aggregate Functions

Theory

COUNT is an aggregate function: it reads many rows and returns a single number. It has three forms. COUNT(*) counts every row, even a row whose columns are all NULL. COUNT(column) counts only the rows where that column is not NULL. COUNT(DISTINCT column) counts how many different non-NULL values the column holds.

Suppose employees has 5 rows and Meera has no city recorded. COUNT(*) returns 5, COUNT(city) returns 4, and COUNT(DISTINCT dept) returns 3 because there are three departments (IT, HR, Sales). Put COUNT next to GROUP BY and it runs once per group, so you get a headcount per department instead of one total. Unlike SUM or AVG, COUNT never returns NULL: on an empty table or empty group it returns 0.

Why we use it

Almost every report starts with 'how many?': how many orders today, how many customers per city, how many rows failed a check. COUNT answers this directly in the database, so you do not have to pull every row into Excel or Python just to count it.

When to use it

Use COUNT(*) for 'how many rows', COUNT(col) for 'how many rows have this filled in', and COUNT(DISTINCT col) for 'how many unique values'. To check if any row exists at all, EXISTS is often faster than COUNT(*) > 0.

How it works

  1. Rows arrive after FROM and WHERE
  2. Is it COUNT(*)? Yes: Add 1 for every row, NULLs included. No: Look at the column value.
  3. Is the value NULL? Yes: Skip the row. No: Add 1 (with DISTINCT: only if not seen before).
  4. Repeat for every row in the group
  5. Return the count (0 if no rows)

Syntax

COUNT(column | *)

Example

SELECT COUNT(*) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic