How to use distinct with the count() for getting unique count values in SQL Server?
How to use distinct with the count() in SQL Server?
827
07-Feb-2023
Updated on 10-Apr-2023
Rocky Dada
10-Apr-2023To use DISTINCT with the COUNT() function in SQL Server, you can use the following syntax:
sqlCopy code
SELECT COUNT(DISTINCT column_name) FROM table_name;Replace column_name with the name of the column that you want to count the distinct values for, and table_name with the name of the table that the column belongs to.
For example, if you have a table called customers with a column called city, and you want to count the number of distinct cities in the table, you can use the following SQL statement:
sqlCopy code
SELECT COUNT(DISTINCT city) FROM customers;This will return the number of distinct cities in the customers table.