PostgreSQL - SELECT DISTINCT clause
Last Updated :
17 Jul, 2024
The SELECT
statement with the DISTINCT
clause to remove duplicate rows from a query result set in PostgreSQL. By leveraging the DISTINCT
clause, you can ensure your query results contain only unique rows, whether you're dealing with a single column or multiple columns.
The DISTINCT
clause in PostgreSQL is used to return unique rows from a result set. When used, it keeps only one row for each group of duplicates, effectively eliminating any duplicate rows in the output.
Let us better understand the SELECT DISTINCT Clause in PostgreSQL to better understand the concept.
Syntax
For a single column:
SELECT DISTINCT column_1 FROM table_name;
For multiple columns:
SELECT DISTINCT column_1, column_2, column_3 FROM table_name;
PostgreSQL SELECT DISTINCT clause Examples
Now, let's look into a few examples for better understanding. For the sake of example, we will create a sample database (say, Favourite_colours) using the commands shown below.
PostgreSQL
CREATE DATABASE Favourite_colours;
CREATE TABLE my_table(
id serial NOT NULL PRIMARY KEY,
colour_1 VARCHAR,
colour_2 VARCHAR
);
INSERT INTO my_table(colour_1, colour_2)
VALUES
('red', 'red'),
('red', 'red'),
('red', NULL),
(NULL, 'red'),
('red', 'green'),
('red', 'blue'),
('green', 'red'),
('green', 'blue'),
('green', 'green'),
('blue', 'red'),
('blue', 'green'),
('blue', 'blue');
SELECT
id,
colour_1,
colour_2
FROM
my_table;
Output: If everything is as intended, the output will be like as shown below:

Since, our database is good to go, we move onto the implementation of the SELECT DISTINCT clause.
Example 1: PostgreSQL DISTINCT on One Column
Retrieve unique values from the 'colour_1'
column.
Query:
SELECT
DISTINCT colour_1
FROM
my_table
ORDER BY
colour_1;
Output:

Example 2: PostgreSQL DISTINCT on multiple columns
Retrieve unique combinations of values from the 'colour_1'
and 'colour_2'
columns.
Query:
SELECT
DISTINCT colour_1,
colour_2
FROM
my_table
ORDER BY
colour_1,
colour_2;
Output:

Important Points About PostgreSQL SELECT DISTINCT clause
- The
DISTINCT
clause compares entire rows, not just individual columns. This means that if you use SELECT DISTINCT *
, PostgreSQL will return unique rows based on all column values. - The
DISTINCT
clause treats NULL
values as equal. Therefore, rows with NULL
values in the specified columns are considered duplicates. - The order of columns in the
SELECT DISTINCT
clause matters. Changing the order of columns can yield different results if the dataset has duplicates across multiple columns. DISTINCT
can be used within subqueries to filter out duplicates before applying additional operations.
Similar Reads
PostgreSQL - WHERE clause The PostgreSQL WHERE clause is a critical component of SQL queries, allowing users to filter records based on specified conditions. In this tutorial, we'll explore how the WHERE clause works in PostgreSQL, its integration with the SELECT statement, and various examples. By using the WHERE clause, we
6 min read
PostgreSQL - HAVING clause The HAVING clause in PostgreSQL is an essential feature for filtering grouped data that has been aggregated using functions like SUM(), COUNT(), AVG(), and others. Unlike the WHERE clause, which filters rows before aggregation, the HAVING clause is used to filter results after the grouping and aggre
4 min read
PostgreSQL - SELECT PostgreSQL SELECT statement is an command for retrieving data from tables within a PostgreSQL database. It enables users to specify which columns to fetch and apply filters using the WHERE clause for targeted results.In this article, We will learn about the PostgreSQL SELECT in detail by understandi
3 min read
Group By Vs Distinct in PostgreSQL When working with PostgreSQL, efficiently organizing and retrieving data is critical for database performance and decision-making. Two commonly used clauses are DISTINCT and GROUP BY that serve distinct purposes in data retrieval. DISTINCT is used to filter out duplicate values, while GROUP BY is em
6 min read
PostgreSQL - GROUP BY clause The GROUP BY clause in PostgreSQL is an essential tool that allows us to group rows that share the same values in one or more columns. This powerful functionality is commonly used to perform aggregate calculations such as SUM(), COUNT(), AVG(), and more, enabling us to summarize data efficiently. In
4 min read