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
How to Enable Intelligent Cloud FinOps for Financial Services with Enteros Database Analytics
- 14 June 2026
- Database Performance Management
Introduction Financial services organizations are accelerating cloud adoption to support digital banking, mobile applications, payment platforms, risk management systems, customer analytics, and AI-driven financial services. While cloud transformation provides agility, scalability, and innovation opportunities, it also introduces new challenges around cost control, resource optimization, and operational visibility. Today’s financial institutions operate complex environments that include: … Continue reading “How to Enable Intelligent Cloud FinOps for Financial Services with Enteros Database Analytics”
How to Improve Telecom Profitability with Enteros Cost Attribution and Database Intelligence
Introduction Telecommunications providers are operating in one of the most competitive and technology-intensive industries in the world. While demand for connectivity, mobile services, broadband access, and digital experiences continues to grow, profit margins are increasingly challenged by rising infrastructure costs, complex network operations, and expanding customer expectations. Modern telecom organizations must support: 5G networks Cloud-native … Continue reading “How to Improve Telecom Profitability with Enteros Cost Attribution and Database Intelligence”
Driving Enterprise Efficiency Through AI-Based Database Performance Optimization
- 12 June 2026
- Database Performance Management
Introduction In today’s digital-first economy, enterprises depend heavily on data-driven applications to power everything from customer transactions to real-time analytics and AI workloads. As these systems scale, database performance becomes a critical determinant of business success. Even minor inefficiencies—slow queries, resource contention, or poor scaling strategies—can lead to significant revenue loss, degraded user experience, and … Continue reading “Driving Enterprise Efficiency Through AI-Based Database Performance Optimization”
How Predictive Database Monitoring Improves Application Uptime and Business Continuity
In today’s always-on digital economy, application availability is no longer just an IT metric—it is a business imperative. Customers expect seamless digital experiences, employees depend on uninterrupted access to critical systems, and organizations rely on applications to drive revenue, operations, and customer engagement. Whether supporting e-commerce transactions, financial services, healthcare applications, SaaS platforms, or telecommunications … Continue reading “How Predictive Database Monitoring Improves Application Uptime and Business Continuity”