Skip to main content
HomeTutorialsSQL

COUNT() SQL FUNCTION

COUNT() lets you count the number of rows that match certain conditions. Learn how to use it in this tutorial.
Updated Oct 2022  · 3 min read

What is the COUNT() function?

The COUNT() function returns the number of rows that matches a criterion.

COUNT() syntax

The basic syntax of COUNT() is as follows.

SELECT COUNT(column_name)
FROM table_name;

Variations of the syntax achieve different goals.

  • The COUNT(*) syntax allows us to count the number of rows in a table
  • The COUNT(DISTINCT column) syntax allows us to count the number of distinct values in a column 
  • The COUNT(CASE WHEN condition THEN column END) syntax allows us to count the number of values that fulfill conditions.

COUNT(), used with GROUP BY, is useful for counting the number of rows that belong to each group.

COUNT(), used with HAVING, is useful for filtering groups according to the number of rows they have.

We will illustrate these with the examples below.

COUNT() examples

Example 1: Counting the number of rows with COUNT(*)

The table called products contains all the products a company sells. The COUNT(*) clause allows us to calculate the number of rows in the table. 

SELECT
    COUNT(*) AS number_of_rows
FROM products

number_of_rows

321

Example 2: Counting unique values with COUNT(DISTINCT …)

The COUNT(DISTINCT column) syntax allows us to count the number of unique values in a column.

For example, each product has an associated brand in the products table. We can count the number of unique products and brands in the table.

SELECT
    COUNT(DISTINCT product_id) AS unique_product_count,
    COUNT(DISTINCT brand_id) AS unique_brand_count
FROM products

unique_product_count

unique_brand_count

321

9

Example 3: Count rows that match a condition using COUNT() with CASE WHEN 

The COUNT(CASE WHEN condition THEN column END) syntax allows us to calculate the number of rows that match a condition.

For example, in the products table, each product has a list_price. We can calculate how many products are “expensive” (having a list price of more than $500) or otherwise.

SELECT
    COUNT(CASE WHEN list_price >= 500 THEN product_id END) AS expensive_product_count,
   COUNT(CASE WHEN list_price < 500 THEN product_id END) AS cheap_product_count
FROM products

expensive_product_count

cheap_product_count

213

108

Example 4: Count rows in groups using COUNT() with GROUP BY

COUNT() can be used with GROUP BY to find the number of rows in each group.

For example, the product table contains bicycle models from 2016 to 2019. To find the distribution of the bicycle, we can use COUNT(product_id) and GROUP BY model_year to count the number of products from each year. 

SELECT
    model_year,
    COUNT(product_id) AS product_count
FROM products
GROUP BY model_year

model_year

product_count

2016

26

2017

85

2018

204

2019

6

Example 5: Filter for groups using COUNT() with GROUP BY and HAVING

In example 4, we see that COUNT() can be used with GROUP BY. We can use the HAVING statement to filter for groups using the number of rows in that group.

For example, to find the number of years that have less than 50 products, we can use the following syntax. 

SELECT
   model_year
FROM products
GROUP BY model_year
HAVING COUNT(product_id) < 50

year_with_less_than_50_pdt

2016

2019

Technical requirements

COUNT() is a function that is available to all versions of modern SQL. 

See also

Learn more about SQL

Topics

Popular SQL Courses

Course

Introduction to SQL

2 hr
630.4K
Learn how to create and query relational databases using SQL in just two hours.
See DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

SQL Developer Salaries: Expectations in 2024

In this article, we're going to learn what an SQL developer does, what factors impact SQL developer salaries, how the average SQL developer salary compares to the average salaries of other developer profiles, and how you can increase your salary as an SQL developer.
Elena Kosourova's photo

Elena Kosourova

7 min

Scaling Enterprise Analytics with Libby Duane Adams, Chief Advocacy Officer and Co-Founder of Alteryx

RIchie and Libby explore the differences between analytics and business intelligence, generative AI and its implications in analytics, the role of data quality and governance, Alteryx’s AI platform, data skills as a workplace necessity, and more. 
Richie Cotton's photo

Richie Cotton

43 min

[Radar Recap] Building a Learning Culture for Analytics Functions, with Russell Johnson, Denisse Groenendaal-Lopez and Mark Stern

In the session, Russell Johnson, Chief Data Scientist at Marks & Spencer, Denisse Groenendaal-Lopez, Learning & Development Business Partner at Booking Group, and Mark Stern, VP of Business Intelligence & Analytics at BetMGM will address the importance of fostering a learning environment for driving success with analytics.
Adel Nehme's photo

Adel Nehme

41 min

[Radar Recap] From Data Governance to Data Discoverability: Building Trust in Data Within Your Organization with Esther Munyi, Amy Grace, Stefaan Verhulst and Malarvizhi Veerappan

Esther Munyi, Amy Grace, Stefaan Verhulst and Malarvizhi Veerappan focus on strategies for improving data quality, fostering a culture of trust around data, and balancing robust governance with the need for accessible, high-quality data.
Richie Cotton's photo

Richie Cotton

39 min

[Radar Recap] Scaling Data ROI: Driving Analytics Adoption Within Your Organization with Laura Gent Felker, Omar Khawaja and Tiffany Perkins-Munn

Laura, Omar and Tiffany explore best practices when it comes to scaling analytics adoption within the wider organization
Richie Cotton's photo

Richie Cotton

40 min

50 Years of SQL with Don Chamberlin, Computer Scientist and Co-Inventor of SQL

Richie and Don explore the early development of SQL, the commercialization and adoption of SQL, how it became standardized, how it evolved and spread via open source, the future of SQL through NoSQL and SQL++ and much more.
Richie Cotton's photo

Richie Cotton

36 min

See MoreSee More