---
title: "Selecting COUNT(*) with DISTINCT"  
description: "Selecting COUNT(*) with DISTINCT"  
author: "Rocky Dada"  
published: 2023-04-13  
updated: 2023-04-19  
canonical: https://www.mindstick.com/forum/157774/selecting-count-with-distinct  
category: "database"  
tags: ["database"]  
reading_time: 2 minutes  

---

# Selecting COUNT(*) with DISTINCT

In [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) 2005 I have a [table](https://www.mindstick.com/articles/43918/how-to-design-table-using-bootstrap) `cm_production` that [lists](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) all the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) that's been put into production. The table has a `ticket_number`, `program_type`, `program_name` and `push_number` along with some other columns.

GOAL: [Count](https://www.mindstick.com/forum/2295/how-to-count-number-of-visitors-for-website) all the [DISTINCT](https://www.mindstick.com/forum/159558/why-c-sharp-linq-distinct-doesn-t-work) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) names by program type and [push](https://www.mindstick.com/forum/23133/push-not-showing-when-app-is-open) number.

What I have so far is:

```plaintext
DECLARE @push_number INT;
SET @push_number = [HERE_ADD_NUMBER];

SELECT DISTINCT COUNT(*) AS Count, program_type AS [Type]
FROM cm_production
WHERE push_number=@push_number
GROUP BY program_type
```

This gets me partway there, but it's counting all the program names, not the distinct ones (which I don't expect it to do in that [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver)). I guess I just can't wrap my head around how to tell it to count only the distinct program names without selecting them. Or something.

## Replies

### Reply by Aryan Kumar

In SQL, selecting **COUNT(*)** with **DISTINCT** is a way to count the number of distinct values in a column of a table.

Suppose we have a table called **orders** with the following columns: **order_id**, **customer_id**, and **order_date**. We want to know how many unique customers have placed orders. We can do this using the following SQL query:

```php
SELECT COUNT(DISTINCT customer_id) FROM orders;
```

This query selects the **customer_id** column from the **orders** table and counts the number of distinct values. The result is a single value indicating the number of unique customers who have placed orders.

Note that if we simply used **COUNT(*)** without the **DISTINCT** keyword, we would get the total number of rows in the **orders** table, not the number of unique customers. By using **DISTINCT**, we ensure that each customer is counted only once, regardless of how many orders they have placed.


---

Original Source: https://www.mindstick.com/forum/157774/selecting-count-with-distinct

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
