SQL SELECT distinct on

In SQL, the SELECT DISTINCT clause is used to retrieve unique values from a specific column or a combination of columns in a table. However, sometimes you may need to retrieve distinct values based on a specific column or a group of columns, where the first occurrence of each unique value is returned. This is where the SELECT DISTINCT ON clause comes into play.

Syntax

The syntax for using SELECT DISTINCT ON in SQL is as follows:

SELECT DISTINCT ON (column_name)
    column_name1, column_name2, ...
FROM table_name
ORDER BY column_name, ...

Here, the column_name represents the column or group of columns that you want to consider for uniqueness, and column_name1, column_name2, ... represents the columns you want to retrieve the values from.

Example Usage

Let’s consider a table employees with the following data:

employee_id first_name last_name department
1 John Doe Sales
2 Jane Smith Marketing
3 John Smith Sales
4 Jane Doe Marketing

If we want to retrieve the distinct values of the first_name column, considering only the first occurrence of each unique name, we can use the SELECT DISTINCT ON clause as follows:

SELECT DISTINCT ON (first_name)
    first_name, last_name
FROM employees
ORDER BY first_name, employee_id;

This query will return the following result:

first_name last_name
John Doe
Jane Smith

In this example, the SELECT DISTINCT ON clause considers the first_name column for uniqueness and retrieves the last_name column along with it. The ORDER BY clause is used to determine which occurrence of each unique value should be considered.

Conclusion

The SELECT DISTINCT ON clause in SQL allows you to retrieve distinct values based on a specific column or group of columns, considering only the first occurrence of each unique value. This can be useful when you need to analyze or manipulate data with specific criteria in mind. Remember to include the appropriate ORDER BY clause to determine the first occurrence of each unique value. #SQL #SELECT #DISTINCT