# How to use SQL SELECT DISTINCT

SQL `SELECT DISTINCT` ensures that only **unique records and rows** are included in the results of a data query. It removes duplicates from the records and rows in question, enabling clearer and more precise data analysis.

## What is SQL `SELECT DISTINCT`?

SQL `SELECT DISTINCT` is a processing operator that’s used with the SQL SELECT command. In queries, `SELECT` selects one or more columns and records. Combined with the `DISTINCT` operator, it ensures that your query results contain **only unique rows** and removes duplicate rows and values. You can combine `SELECT DISTINCT` with other SQL operators and SQL commands to customise your queries based on your individual needs.

### What’s the difference between SQL `SELECT DISTINCT` and `SELECT UNIQUE`

‘Unique’ and ‘distinct’ have similar meaning as words, but the SQL operators `UNIQUE` and `DISTINCT` serve **different purposes**. To use SQL `SELECT DISTINCT` correctly, you should be aware of how it differs from `UNIQUE`.

<table>
  <thead>
    <tr>
      <th>SQL SELECT DISTINCT</th>
      <th>SQL SELECT UNIQUE</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Checks datasets for duplicate rows and values; removes redundancies and only returns distinct results</td>
      <td>Suitable for preventing entry of duplicates in selected tables and columns using column restraints</td>
    </tr>
    <tr>
      <td>Primarily used for evaluating, analysing and comparing data sets to get clear, meaningful results</td>
      <td>Used when creating and editing tables to ensure better data integrity without redundancies</td>
    </tr>
  </tbody>
</table>

Tip Looking for a quick refresher on [SQL](https://www.ionos.com/en-ie/digitalguide/server/know-how/what-is-sql/)? Our [SQL introduction with examples](https://www.ionos.com/en-ie/digitalguide/server/configuration/sql-introduction-with-examples/) presents the most important functions.

## What is the syntax of SQL `SELECT DISTINCT`?

The basic syntax for SQL `SELECT DISTINCT` looks as follows:

```sql
SELECT  DISTINCT  ColumnA, ColumnB, …
FROM table
```

It uses the following parameters:

- `SELECT DISTINCT`: Here is where you specify which columns you’d like to use the `SELECT` command and the `DISTINCT` operator on.
- `FROM`: Specify here which table contains those columns.
- `WHERE`: In an optional WHERE clause, you can add additional conditions to narrow down your query.

## What is SQL `SELECT DISTINCT` used for?

There are **countless use cases** for SQL `SELECT DISTINCT`. Here are just a few examples:

- Reduce customer, order, patient or product data that appears in multiple rows into one unique entry
- Count and display the number of purchases based on orders and product IDs
- Retrieve unique patient or customer data
- Display all shipping addresses or suppliers in a region

## Examples of SQL `SELECT DISTINCT`

We’ll now look at 2 examples using `SELECT DISTINCT`.

### Retrieve all customers

Let’s say you want to retrieve all unique customers from a table called ‘Orders’. You don’t want to have any duplicate rows if a customer has made multiple orders. The syntax for that would look as follows:

```sql
SELECT  DISTINCT  Customer_name
FROM  Orders
```

### Filtering customers from a certain region

You can use a `WHERE` clause to add **additional conditions** to your evaluation of customer data. Let’s say you want to retrieve the names of all customers in the UK from a table called ‘Orders’. Here’s how that would look:

```sql
SELECT  DISTINCT  Customer_name, Country
FROM  Orders
WHERE  Country  =  'UK'
```

## Are there alternatives to SQL `SELECT DISTINCT`?

`SELECT DISTINCT` is a practical way to remove duplicates from your queries, but it’s not always the best solution. When applied in large data sets, it uses a lot of resources comparing each individual column. In that case, you can look at these **alternatives with similar functions**:

- `GROUP BY`: SQL GROUP BY clauses group together records from the columns you specify. You can make them more precise using additional functions and operators.
- `EXISTS`: SQL EXISTS checks whether values and records are present in another table.
- `COUNT`: Combine `DISTINCT` with SQL COUNT to count the unique values in a column.
- `SELECT` + `FROM` + `WHERE`: You can narrow down your results better with a subquery (a query nested inside another query). In the subquery, choose the column using `SELECT`, specify which table you’re working in with `FROM` and then set the conditions you want results to satisfy with `WHERE`.


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/server/configuration/sql-select-distinct/](https://www.ionos.com/en-ie/digitalguide/server/configuration/sql-select-distinct/) for AI/LLM consumption.