---
title: "What is the DISTINCT keyword, and when is it used in SQL queries?"  
description: "What is the DISTINCT keyword, and when is it used in SQL queries?"  
author: "Steilla Mitchel"  
published: 2023-09-04  
updated: 2023-09-25  
canonical: https://www.mindstick.com/forum/159825/what-is-the-distinct-keyword-and-when-is-it-used-in-sql-queries  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 3 minutes  

---

# What is the DISTINCT keyword, and when is it used in SQL queries?

What is the [DISTINCT keyword](https://www.mindstick.com/forum/160083/what-is-the-sql-distinct-keyword-and-when-it-used-in-a-search-query), and when is it used in [SQL queries](https://www.mindstick.com/forum/160287/how-can-parameterized-stored-procedures-improve-performance-compared-to-dynamic-sql-queries)?

## Replies

### Reply by Aryan Kumar

The **[DISTINCT](https://www.mindstick.com/forum/159558/why-c-sharp-linq-distinct-doesn-t-work)** [keyword](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) is used to eliminate duplicate rows from the result set of a query. It ensures that only unique rows are returned, and any duplicate rows are removed. This can be particularly useful when you want to retrieve a list of unique values from a specific column or when you want to eliminate redundant data from your query results.

Here's the basic syntax for using the **DISTINCT** keyword:

```plaintext
SELECT DISTINCT column1, column2, ...
FROM table_name
WHERE condition;
```

**column1, column2, ...**: The columns you want to select from the table. You can specify multiple columns if needed.

**table_name**: The name of the table from which you want to retrieve data.

**condition** (optional): Any conditions you want to apply to filter the rows before removing duplicates.

**When to Use DISTINCT in SQL [Queries](https://www.mindstick.com/forum/33678/sub-queries-in-sql-server)**:

**Removing Duplicate Values**: The primary use of **DISTINCT** is to remove duplicate values from the result set. This is helpful when you want to see a list of unique values from one or more columns. For example, to get a list of distinct product categories from a table of products:

```plaintext
SELECT DISTINCT Category FROM Products;
```

**Aggregating Data**: In some cases, you might want to use **DISTINCT** in combination with aggregate functions to calculate aggregated values based on unique data. For instance, to find the total number of unique customers who placed orders:

```plaintext
SELECT COUNT(DISTINCT CustomerID) AS UniqueCustomerCount FROM Orders;
```

**Eliminating Redundant Rows**: When joining multiple tables or performing complex queries, you may encounter scenarios where the result set contains redundant rows. Using **DISTINCT** can help you eliminate these redundancies.

```plaintext
SELECT DISTINCT A.*, B.*
FROM TableA A
JOIN TableB B ON A.ID = B.ID;
```

**Data Cleansing**: **DISTINCT** can be useful when you're dealing with data that might contain duplicates due to data entry errors or other issues. It allows you to quickly identify and address duplicates in your dataset.

It's important to use **DISTINCT** judiciously because it can have a performance impact, especially on large datasets. When using **DISTINCT**, the database engine needs to sort and filter the data to remove duplicates, which can slow down query execution. Therefore, it's a good practice to use it when genuinely necessary, such as when you need unique values or when you're performing data analysis tasks.


---

Original Source: https://www.mindstick.com/forum/159825/what-is-the-distinct-keyword-and-when-is-it-used-in-sql-queries

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
