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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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.