How to use distinct with the count() for getting unique count values in SQL Server?
How to use distinct with the count() for getting unique count values in SQL Server?
Student
Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
To 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.