Distinct Keyword in SQL

SQL DISTINCT Keyword

DISTINCT is Keyword in SQL

The keyword DISTINCT in SQL retrieves unique records from a set. It eliminates duplicate rows in the output. The keyword returns only distinct values for chosen columns.

Using DISTINCT in a SQL SELECT Statement

For using DISTINCT in a SQL SELECT statement, the basic syntax is as follows:

Syntax:
SELECT DISTINCT column1, column2, ... FROM table_name;
    

In the above syntax:

  • column1, column2, ...: Here you refer to columns you wish to select.
  • table_name: This is the table name from where you wish to get data.

Example

Table: Employees

EmployeeID    Name
1             Alice
2             Bob
3             Alice
4             David
5             Alice
    

Using DISTINCT:

SELECT DISTINCT Name FROM Employees;
    

Result:

Name
Alice
Bob
David
    

The previous example demonstrates the use of SELECT DISTINCT. This statement retrieves unique names from the Employees table. It eliminates duplicate values.

Using DISTINCT with Multiple Columns

The DISTINCT keyword can also be paired with multiple columns. This is beneficial when you need to access unique value combinations across various columns. The procedure is shown below:

SELECT DISTINCT column1, column2 FROM table_name;
    

The basic situation has been explained. It gives you distinct, unique value pairings of column1 and column2. Where? In the specified table.