SQL COUNT DISTINCT function

Definition and Usage

DISTINCT and COUNT keywords can be used together to calculate the number of non-repetitive results.

Syntax

SELECT COUNT(DISTINCT column(s)) FROM table

Example

Note:The following examples are only applicable to ORACLE and Microsoft SQL server and cannot be used with Microsoft Access.

"Orders" table:

Company OrderNumber
IBM 3532
W3School 2356
Apple 4698
W3School 6953

Example 1

SELECT COUNT(Company) FROM Orders

Result:

4

Example 2

SELECT COUNT(DISTINCT Company) FROM Orders

Result:

3