In PostgreSQL, the DISTINCT keyword/clause is used within the SELECT statement to fetch the unique records from the result-set. The DISTINCT keyword removes all the redundant/duplicate records and returns only the unique ones.
In PostgreSQL, the SELECT DISTINCT clause can be used to fetch unique records for a single or multiple columns of a result set returned by the SELECT statement. In the case of multiple columns, the SELECT DISTINCT clause skips only those records that have duplicates in all the specified columns.
In this write-up, we will explain the different use cases of the SELECT DISTINCT clause using some examples. So, let’s get started.
How to Use DISTINCT Keyword/Clause in PostgreSQL?
The below-given snippet illustrates the syntax of the DISTINCT keyword:
SELECT DISTINCT col_1, col_2,.....col_N FROM tab_name
- col_1, col_2,.....col_N are the n number of columns to be fetched by the SELECT statement.
- The SELECT DISTINCT represents that only unique records will be fetched/returned, and the duplicate records will be neglected.
Example 1: Fetch Table Records Without DISTINCT Keyword
Firstly, we will run the SELECT statement to get all the records of the bike_details table:
SELECT * FROM bike_details;
Let’s execute the below-given query to fetch the bike_model column:
SELECT bike_model FROM bike_details;
The output shows that the bike_model column has some unique as well as some duplicate records.
Example 2: Fetch Table Records With DISTINCT Keyword
Let’s run the SELECT statement with the collaboration of the DISTINCT keyword to get only unique records from the bike_model column:
SELECT DISTINCT bike_model FROM bike_details;
The output proved that the DISTINCT keyword fetched only the unique records and skipped duplicated ones.
Example 3: Fetch Sorted and Unique Records
Let’s execute the following query to get the sorted and unique records from the bike_model column:
SELECT DISTINCT bike_model FROM bike_details ORDER BY bike_model DESC;
The output clarified that the SELECT DISTINCT clause returned the unique and sorted(descending order) records of the bike_details table.
Example 4: Fetch Multiple Columns Using DISTINCT Keyword
You have to follow the comma-separated syntax to fetch more than one column using the DISTINCT keyword:
SELECT DISTINCT bike_model, bike_color FROM bike_details ORDER BY bike_model DESC;
- From the above snippet, we can observe that the bike_model 2022 occurs twice. But the SELECT DISTINCT didn’t eliminate it because bike_color for both these records is different.
- This proved that the DISTINCT keyword evaluated the duplicated values based on both bike_model and bike_color columns.
- The SELECT DISTINCT clause skipped only those records that were duplicated in both bike_model and bike_color columns.
Conclusion
In PostgreSQL, the DISTINCT keyword/clause is used with the collaboration of the SELECT statement to fetch the unique records from a result-set. The DISTINCT keyword removes all the redundant/duplicate records and returns only the unique ones. In PostgreSQL, the DISTINCT keyword or the SELECT DISTINCT clause can be used to fetch single or multiple columns of a result set returned by the SELECT statement. This write-up considered several examples to explain the working of the SELECT DISTINCT clause in PostgreSQL.