---
title: "SQL Count Vs Count Distinct With Group By"
description: "Explained how to get records using COUNT and COUNT Distinct with Group By clause in SQL"
url: https://srinimf.com/2022/11/22/sql-count-vs-count-distinct-with-group-by/
date: 2022-11-22
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/11/pexels-photo-5561913.jpeg?fit=1880%2C1255&ssl=1
categories: ["SQL"]
tags: ["Count With Group By"]
type: post
lang: en
---

# SQL Count Vs Count Distinct With Group By

[How to](https://srinimf.com/2016/04/07/sql-query-to-find-nth-min-and-nth-max-salary/) know the products for sale each market date or how many different products each vendor has offered? You can determine these values using **COUNT and COUNT DISTINCT.**

## SQL Count [GROUP BY](https://srinimf.com/2014/09/18/db2-sql-distinct-and-group-by/)

COUNT will count up the rows within a group when used with **[GROUP BY](https://srinimf.com/2019/07/27/count-sql-statement/)**, and COUNT DISTINCT will count up the unique values present ( in the specified field within the group.)

Related[FAANG-Style SQL Interview Traps (And How to Avoid Them)](https://srinimf.com/2026/02/09/faang-style-sql-interview-traps-and-how-to-avoid-them/)

```
To determine how many products are for sale on each market date, you can count the rows in the vendor_inventory table grouped by date.
```

Related[SQL AVG Column Function: How to Use it](https://srinimf.com/2015/08/20/db2-avg-column-function-best-example/)

It doesn’t tell us what quantity each product was offered or sold. But it **counts available products**. It is because a row is present in the table for each product for each vendor for each market date.

```
SELECT market_date, COUNT(product_id) AS product_count
FROM farmers_market.vendor_inventory
GROUP BY market_date
ORDER BY market_date
```

Advertisements

## SQL Count DISTINCT Group By

If you want to know how many different products—with **unique product IDs**—each vendor brought to market during a date range. So you can use **[COUNT DISTINCT](https://srinimf.com/2016/03/24/2-key-differences-of-count-and-countdistinct-db2-v11/)** on the product_id field:

```
SELECT vendor_id, COUNT(DISTINCT product_id) AS different_products_offered
FROM farmers_market.vendor_inventory
WHERE market_date BETWEEN '2019-03-02' AND '2019-03-16'
GROUP BY vendor_id
ORDER BY vendor_id
```

Related

- [SQL COUNT(*) Vs COUNT(DISTINCT)](https://srinimf.com/2016/03/24/2-key-differences-of-count-and-countdistinct-db2-v11/)DateMarch 24, 2016
- [SQL Query to Remove, get Only Duplicates](https://srinimf.com/2015/12/22/best-db2-sql-queries-to-work-on-duplicate-values/)DateDecember 22, 2015
- [SQL ‘Aggregate’ Vs ‘DISTINCT’](https://srinimf.com/2015/10/11/db2-aggregate-function-tricky-questions/)DateOctober 11, 2015

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F11%2F22%2Fsql-count-vs-count-distinct-with-group-by%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F11%2Fpexels-photo-5561913.jpeg%3Ffit%3D1200%252C801%26ssl%3D1&description=%3Cstrong%3ESQL%20Count%20Vs%20Count%20Distinct%20With%20Group%20By%3C%2Fstrong%3E)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/11/22/sql-count-vs-count-distinct-with-group-by/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/11/22/sql-count-vs-count-distinct-with-group-by/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/11/22/sql-count-vs-count-distinct-with-group-by/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/11/22/sql-count-vs-count-distinct-with-group-by/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20%3Cstrong%3ESQL%20Count%20Vs%20Count%20Distinct%20With%20Group%20By%3C%2Fstrong%3E&body=https%3A%2F%2Fsrinimf.com%2F2022%2F11%2F22%2Fsql-count-vs-count-distinct-with-group-by%2F&share=email)
-

### Like this:

Like Loading…
