SQL Select Distinct
The SELECT DISTINCT
statement is used in SQL to retrieve unique values from a specified column or a combination of columns in a table. It eliminates duplicate rows and returns only the distinct values.
Here’s the basic syntax of the SELECT DISTINCT
statement:
sql
SELECT DISTINCT column1, column2, ...
FROM table_name;
You replace column1, column2, ...
with the names of the columns you want to retrieve unique values from, and table_name
with the name of the table you want to query.
For example, if you have a table called employees
with columns name
, department
, and salary
, and you want to retrieve the distinct values from the department
column, you would use the following query:
sql
SELECT DISTINCT department
FROM employees;
This query will return a result set with only the unique values found in the department
column of the employees
table.
Note that the SELECT DISTINCT
statement applies the distinctness to the entire row, so if you include multiple columns in the SELECT DISTINCT
clause, the combination of values in those columns will be considered for distinctness.