PostgreSQL SELECT DISTINCT Clause

Topics Covered

SELECT DISTINCT will only return the unique rows of the result returned by the query. It will remove the duplicated rows from the result set. Postgres DISTINCT can also be used with multiple columns, then it removes duplicate rows on the basis of a combination of all the columns.

PostgreSQL SELECT DISTINCT Syntax

Syntax of Postgres DISTINCT for the single column is given below:

In the above syntax, unique rows will be selected on the basis of the value of columnName1.

Syntax of Postgres DISTINCT for multiple columns is given below:

In the above syntax, duplicate values will be removed based on the combination of values of all the specified columns(columnName1, columnName2, ...).

PostgreSQL SELECT DISTINCT Parameters

PostgreSQL SELECT DISTINCT have the following parameters:

  • columnName: It represents the column name(s) for which a unique value will be evaluated for the result set.
  • tableName: It represents the table name(s) from which the data is required to be fetched.
  • WHERE conditions(Optional): It is optional and is used to specify the conditions that are to be applied to the data to be fetched.

PostgreSQL SELECT DISTINCT examples

Let us assume we have a table named Colors, having columns id, textColor and backColor And the following data is inserted in the table.

idtextColorbackColor
1bluered
2redblue
3redgreen
4blueyellow
5blueyellow
6redyellow
7greenred
8greenblue
9yellowgreen
10yellowblue
11redblue
12yellowblue
13yellowgreen
14redgreen
15redyellow

Suppose we want to fetch the unique textColor from the table Colors, then we have to write the following query:

Query:

Output:

textColor
blue
red
green
yellow

PostgreSQL DISTINCT one column example

Below is the query to fetch a unique backColor from the table Colors.

Query:

Output:

backColor
red
blue
green
yellow

PostgreSQL DISTINCT multiple columns

If we specify multiple columns with the DISTINCT clause, then the unique value of the combination of all the columns will be selected.

Below is an example of using DISTINCT with columns textColor and backColor, so now it will only skip those rows which are duplicated in both textColor and backColor columns.

Query:

Output:

textColorbackColor
bluered
redblue
redgreen
blueyellow
redyellow
greenred
greenblue
yellowgreen
yellowblue

PostgreSQL DISTINCT Clause: With DISTINCT ON Clause

Postgres DISTINCT On clause includes the unique values, removes all duplicate values of the first column and keeps only a single row for every unique of the first column.

DISTINCT ON Clause Syntax:

In the above syntax, the result set will include distinct values of columnName1, and for each distinct value, it will include the first occurrence row of that value based on the specified ORDER BY clause.

Query:

Output:

textcolorid
blue1
green7
red2
yellow9

Explanation: The above query will include unique values of textColor and id will be chosen corresponds to the first occurrence of every textColor value.

PostgreSQL DISTINCT Clause: Multiple Tables

Let us create two tables Employee and Department, with the following attributes.

Now let us insert some data in both the tables:

Now we will use the DISTINCT clause to get the unique values and combine both tables using join.

Query:

Output:

empidnamedepart_iddepart_name
1JohnD1Training
2HannaD2Hiring
3SteveD3Production

Conclusion

  • SELECT DISTINCT will only return the unique rows of the result returned by the query.

  • Postgres DISTINCT can also be used with multiple columns.

  • Postgres DISTINCT On clause includes the unique values, removes all duplicate values of the first column and keeps only a single row for every unique of the first column.