bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL COUNT() Function

The SQL COUNT() Function

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

COUNT() Syntax

SELECT COUNT([DISTINCT]

column_name | *)

From

table_name

Where

condition

;

The behavior of COUNT()

depends on the argument used within the parentheses: COUNT(*) - Counts the total number of rows in a table (including NULL values).

COUNT(columnname)

Formula

- Counts all non - null values in the column.

COUNT(DISTINCT columnname)

Formula

- Counts only the unique, non - null values in the column.

Using COUNT(*)

The following SQL uses

COUNT(*), and counts the total number of rows in the "Products" table (will include NULL values):

Example

SELECT COUNT(*)
FROM Products;

Demo Database

Below is a selection from the

Products table used in the examples:

ProductID

ProductName

SupplierID

CategoryID

Unit

Price

Chais

10 boxes x 20 bags 18.00

Chang

Formula

24 - 12 oz bottles

19.00

Aniseed Syrup

Formula

12 - 550 ml bottles

10.00

Chef Anton's Cajun Seasoning

Formula

48 - 6 oz jars

22.00

Chef Anton's Gumbo Mix

36 boxes 21.35 Using COUNT(column_name)

The

COUNT(column_name)

Formula

counts all non - null values in the specified column.
The following SQL counts all non - null values of the "ProductName" column:

Example

Previous

SQL MAX() Function

Next

SQL SUM() Function