DISTINCT

DISTINCT is a keyword in SQL that is used to remove duplicates in the result set of a SELECT statement. It only allows unique values to be returned, ensuring the elimination of any duplicate entries.

Example

SELECT DISTINCT country
FROM customers;

Output

+-----------+
| country |
+-----------+
| USA |
| Germany |
| France |
+-----------+

Explanation

The DISTINCT keyword in SQL is used to return only distinct (unique) values. In the example above, SELECT DISTINCT country FROM customers returns only unique country names from the ‘customers’ table.

Example

SELECT DISTINCT column_name
FROM table_name;

Output

column_name
--------------
value1
value2
value3

Explanation

DISTINCT keyword in SQL is used to return unique values in the output. It removes all the duplicate records and only shows distinct (unique) records. In the provided example, unique values from the specified column in the table are returned.

Example

SELECT DISTINCT LastName
FROM Employees;

Output

LastName
--------
Johnson
Smith
Brown

Explanation

The DISTINCT keyword is used in SELECT statements to return unique values in the output. In the example above, the SQL returns only unique last names from the “Employees” table.

Example

SELECT DISTINCT department_id
FROM employees;

Output

DEPARTMENT_ID
-------------
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150

Explanation

In the provided example, the SELECT DISTINCT statement is used to return unique department IDs from the ‘employees’ table, thus eliminating duplicate entries. The result is a list of distinct or unique ‘department_id’s.

Example

SELECT DISTINCT Country FROM Customers;

Output

Country
----------
Germany
Mexico
UK
Canada
USA
France

Explanation

The DISTINCT keyword in SQL is used to return only distinct (different) values. In the example above, from the ‘Customers’ table, it returns all distinct countries present in the ‘Country’ column.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.