Preamble
PostgreSQL DISTINCT statement is used to remove duplicates from the result set. DISTINCT can be used only with SELECT operators.
Syntax for DISTINCT statement in PostgreSQL
SELECT DISTINCT | DISTINCT ON (distinct_expressions_id)
expressions_id
FROM tabs
[WHERE conds];
Parameters and statement arguments
- Expressions_expressions_id – Expressions used to delete duplicates.
- Column – The column or calculation you want to get.
- tabs – The tables from which you want to get the records. The FROM operator must specify at least one table.
- WHERE conds – Optional. The conditions to be met for the records to be selected.
Note:
- If only one expression is specified in DISTINCT, the query will return unique values for that expression.
- If several expressions are specified in DISTINCT, the query retrieves the unique combinations for the listed expressions.
- If DISTINCT ON keywords are specified, the query will return unique values for Different_expressions and other fields for the selected entries based on ORDER BY (limit 1).
- In PostgreSQL, DISTINCT does not ignore NULL values. Therefore when using DISTINCT in your SQL statement, your resulting set will contain NULL as a separate value.
Example of a DISTINCT statement with one expression
Let’s look at the simplest example of DISTINCT in PostgreSQL. We can use the DISTINCT operator to return a single field that removes duplicates from the result set.
For example:
SELECT DISTINCT last_name
FROM contacts
ORDER BY last_name;
In this PostgreSQL example, DISTINCT will return all unique last_name values from the contacts table.
An example of a DISTINCT statement with multiple expressions
Let’s see how you can use the PostgreSQL DISTINCT statement to remove duplicates from more than one field in your SELECT statement.
For example:
SELECT DISTINCT last_name, city, state
FROM contacts
ORDER BY last_name, city, state;
This example will return each unique combination of last_name, city, and state from the contacts table. In this case, DISTINCT applies to each field specified after the DISTINCT keyword and therefore returns different combinations.
Example of a DISTINCT ON statement
One thing that is unique in PostgreSQL, compared to other databases, is that you have another option when using the DISTINCT operator, which is called DISTINCT ON.
DISTINCT ON will return only the first line for DISTINCT ON (diver_expressions) based on the ORDER BY operator provided in the query. Any other fields listed in the SELECT statement will be returned for this first line. This is similar to running LIMIT in 1 for each combination of DISTINCT ON (Different_expressions).
Let’s have a closer look at how to use DISTINCT ON in a DISTINCT operator and what it returns.
In this way, we could change the example above :
SELECT DISTINCT ON (last_name) last_name, city, state
FROM contacts
ORDER BY last_name, city, state;
This DISTINCT example, which uses DISTINCT ON keywords, will return all unique values of last_name. But in this case, for each unique value of last_name, it will return only the first unique last_name record it encounters, based on the ORDER BY operator together with the city and state values from that record.
It does not return unique combinations of last_name, city, and state. In fact, it performs a LIMIT of 1 for each DISTINCT ON (last_name) and returns the corresponding city and state values after it has selected the returned records.
PostgreSQL: Distinct | Course
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Optimizing University Data Systems with AI-Driven Database Analytics
- 25 April 2026
- Database Performance Management
Universities and higher education institutions are undergoing a massive digital transformation. From online learning platforms and student information systems to research databases and digital libraries, modern universities rely heavily on complex IT infrastructure and data-driven applications. These systems generate enormous amounts of data every day—from student records and course materials to financial information and research … Continue reading “Optimizing University Data Systems with AI-Driven Database Analytics”
Optimizing Healthcare IT Performance with AI-Driven Database Monitoring
The healthcare sector is undergoing a rapid digital transformation. Hospitals, clinics, research centers, and telemedicine providers increasingly rely on sophisticated IT infrastructures to manage patient records, support diagnostics, and enable data-driven decision-making. From Electronic Health Records (EHR) and imaging systems to remote patient monitoring platforms and clinical analytics, modern healthcare environments generate massive volumes of … Continue reading “Optimizing Healthcare IT Performance with AI-Driven Database Monitoring”
Strengthening Financial Data Platforms with AI-Powered Database Optimization
- 24 April 2026
- Database Performance Management
The financial services industry is undergoing rapid digital transformation. From online banking and digital payments to real-time fraud detection and financial analytics, modern financial institutions rely heavily on powerful data infrastructures. Behind every financial transaction lies a complex database system that processes large volumes of data in real time. As financial platforms scale and customer … Continue reading “Strengthening Financial Data Platforms with AI-Powered Database Optimization”
Boosting E-commerce Platform Performance with AI-Driven Database Monitoring
The e-commerce industry is evolving at an unprecedented pace. From personalized shopping experiences to real-time inventory management and seamless checkout systems, modern online stores rely heavily on high-performing data infrastructures. Behind every successful e-commerce platform lies a powerful database environment that processes thousands—sometimes millions—of transactions, searches, and customer interactions every day. However, as online marketplaces … Continue reading “Boosting E-commerce Platform Performance with AI-Driven Database Monitoring”