PostgreSQL SELECT DISTINCT Clause

Learn via video courses
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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.