---
title: "How to use distinct with the count() in SQL Server?"  
description: "How to use distinct with the count() in SQL Server?"  
author: "Revati S Misra"  
published: 2023-02-07  
updated: 2023-04-10  
canonical: https://www.mindstick.com/forum/157378/how-to-use-distinct-with-the-count-in-sql-server  
category: "mysql"  
tags: ["sql server", "sql"]  
reading_time: 1 minute  

---

# How to use distinct with the count() in SQL Server?

How to use [distinct](https://www.mindstick.com/forum/159558/why-c-sharp-linq-distinct-doesn-t-work) with the [count](https://www.mindstick.com/forum/157774/selecting-count-with-distinct)() for getting [unique](https://www.mindstick.com/blog/12870/how-to-be-unique-in-business) count [values in SQL](https://www.mindstick.com/forum/159818/how-to-use-the-coalesce-function-to-handle-null-values-in-sql-queries) [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over)?\

## Replies

### Reply by Rocky Dada

To use **DISTINCT** with the **COUNT()** function in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-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](https://www.mindstick.com/forum/327/sum-textbox-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.


---

Original Source: https://www.mindstick.com/forum/157378/how-to-use-distinct-with-the-count-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
