---
title: "Explain the purpose of the DISTINCT keyword in SQL and provide an example."  
description: "Explain the purpose of the DISTINCT keyword in SQL and provide an example."  
author: "Revati S Misra"  
published: 2023-06-30  
updated: 2023-07-02  
canonical: https://www.mindstick.com/forum/158907/explain-the-purpose-of-the-distinct-keyword-in-sql-and-provide-an-example  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# Explain the purpose of the DISTINCT keyword in SQL and provide an example.

[Explain the purpose](https://www.mindstick.com/forum/159480/explain-the-purpose-of-the-virtual-keyword-and-virtual-functions-in-c-plus-plus) of the [DISTINCT keyword](https://www.mindstick.com/forum/160083/what-is-the-sql-distinct-keyword-and-when-it-used-in-a-search-query) in [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) and provide an example.

## Replies

### Reply by Aryan Kumar

Sure. 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 is used to remove duplicate rows from the result set of a SELECT statement. For example, let's say we have a table called `products` with the following columns:

- `product_id` (integer)
- `product_name` (string)
- `price` (decimal)

If we run the following SELECT statement:

SQL

```plaintext
SELECT * FROM products;
```

The result set will contain all of the rows in the `products` table, including any duplicate rows.

To remove the duplicate rows, we can use the DISTINCT keyword:

SQL

```plaintext
SELECT DISTINCT product_name FROM products;
```

This will return a result set that contains only the unique product names from the `products` table.

Here is another example. Let's say we have two tables, `customers` and `orders`:

SQL

```plaintext
CREATE TABLE customers (
  id INT NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  email VARCHAR(255) NOT NULL,
  PRIMARY KEY (id)
);

CREATE TABLE orders (
  id INT NOT NULL AUTO_INCREMENT,
  customer_id INT NOT NULL,
  order_date DATETIME NOT NULL,
  total_amount DECIMAL(10,2) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (customer_id) REFERENCES customers (id)
);
```

We can use the DISTINCT keyword to combine the results of a SELECT statement that queries the `customers` table with a SELECT statement that queries the `orders` table, and remove any duplicate rows:

SQL

```plaintext
SELECT DISTINCT customers.name, orders.order_date
FROM customers
INNER JOIN orders
ON customers.id = orders.customer_id;
```

This will return a result set that contains the unique names of all customers who have placed orders, along with the date of their most recent order.


---

Original Source: https://www.mindstick.com/forum/158907/explain-the-purpose-of-the-distinct-keyword-in-sql-and-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
