Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL DISTINCT Clause

In PostgreSQL, the DISTINCT clause is used with a SELECT statement to remove duplicate rows from a result set. It keeps only one row out of two or more duplicate rows. It can also be used for finding distinct values on one or more columns.

The SELECT ALL is the default clause specified which keeps all rows from the result set.

Syntax
SELECT DISTINCT <column_1>,<column_2>... <column_n>
FROM <table_name>;

Let's use the following weather_report table to demonstrate the DISTINCT clause.

To get a list of distinct Cities, use the SELECT DISTINCT clause like below.

Example: Get Distinct Rows
SELECT DISTINCT city FROM weather_reports;

The above query will return the following result:

The SELECT DISTINCT clause can be applied to multiple columns. In that case, the unique combination of all columns will be returned from the result set.

Example: Get Distinct Values of Multiple Columns
SELECT DISTINCT city, weather FROM weather_reports;

DISTINCT ON

Just like the DISTINCT clause, PostgreSQL also supports DISTINCT ON can be used with a SELECT statement to remove duplicates from a query result set. The only difference is it keeps the “first row” from each set of rows where the expression evaluates to be true.

Syntax: DISTINCT ON
SELECT DISTINCT ON (<column_1>) <column_alias>,
<column_2>...<column_n>
FROM <table_name>
ORDER BY <column_1>, <column_2>…<column_n>;

The order of rows returned from the SELECT statement is unpredictable. Hence it is always a good practice to use the ORDER BY clause with the DISTINCT ON clause to ensure the desired row appears first. The DISTINCT ON expression must match the leftmost ORDER BY expression.

The DISTINCT ON clause can be used to get the “first” row of every group of the duplicate resultset. For example, the following statement will sort the data of the weather_report table by ascending order of the city and descending order of report_date, and then for every group of duplicates, it keeps the first row in the result set.

As described below, DISTINCT ON with ORDER BY clause is used to fetch the most recent weather report of each city from the data stored in the weather_reports table.

Example: DISTINCT ON
SELECT DISTINCT ON (city) city, report_date, weather
FROM weather_reports
ORDER BY city, report_date DESC;

Note that the DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s). If we don't use ORDER BY in the above query, we can get weather reports for unpredictable report_date for each city.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.