Professional Documents
Culture Documents
4-PostgreSQL SELECT DISTINCT by Practical Examples
4-PostgreSQL SELECT DISTINCT by Practical Examples
Summary: in this tutorial, you will learn how to use the PostgreSQL SELECT DISTINCT clause to remove duplicate rows from
a result set returned by a query.
SELECT
DISTINCT column1
FROM
table_name;
In this statement, the values in the column1 column are used to evaluate the duplicate.
If you specify multiple columns, the DISTINCT clause will evaluate the duplicate based on the combination of values of
these columns.
SELECT
DISTINCT column1, column2
FROM
table_name;
In this case, the combination of values in both column1 and column2 columns will be used for evaluating the duplicate.
PostgreSQL also provides the DISTINCT ON (expression) to keep the “first” row of each group of duplicates using the
following syntax:
SELECT
DISTINCT ON (column1) column_alias,
column2
FROM
table_name
ORDER BY
column1,
column2;
The order of rows returned from the SELECT statement is unspecified therefore the “first” row of each group of the
duplicate is also unspecified.
Notice that the DISTINCT ON expression must match the leftmost expression in the ORDER BY clause.
Note that you will learn how to create a table and insert data into a table in the subsequent tutorial. In this tutorial,
you just execute the statement in psql or pgAdmin to execute the statements.
Second, insert some rows into the distinct_demo table using the following INSERT (https://www.postgresqltutorial.com/postgresql-
tutorial/postgresql-insert/) statement:
Third, query the data from the distinct_demo table using the SELECT (https://www.postgresqltutorial.com/postgresql-
tutorial/postgresql-select/) statement:
SELECT
id,
bcolor,
fcolor
FROM
distinct_demo ;
The following statement selects unique values in the bcolor column from the t1 table and sorts
(https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-order-by/) the result set in alphabetical order by using the ORDER BY
(https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-order-by/) clause.
SELECT
DISTINCT bcolor
FROM
distinct_demo
ORDER BY
bcolor;
The following statement demonstrates how to use the DISTINCT clause on multiple columns:
SELECT
DISTINCT bcolor,
fcolor
FROM
distinct_demo
ORDER BY
bcolor,
fcolor;
Because we specified both bcolor and fcolor columns in the SELECT DISTINCT clause, PostgreSQL combined the values
in both bcolor and fcolor columns to evaluate the uniqueness of the rows.
The query returns the unique combination of bcolor and fcolor from the distinct_demo table. Notice that the
distinct_demo table has two rows with red value in both bcolor and fcolor columns. When we applied the
DISTINCT to both columns, one row was removed from the result set because it is the duplicate.
The following statement sorts the result set by the bcolor and fcolor , and then for each group of duplicates, it keeps
the first row in the returned result set.
SELECT
DISTINCT ON (bcolor) bcolor,
fcolor
FROM
distinct_demo
ORDER BY
bcolor,
fcolor;