---
description: DISTINCT in PostgreSQL - You can retrieve data from the table using a SELECT statement.
title: DISTINCT in PostgreSQL: Select, Order By &#038; Limit (Examples)
image: https://www.guru99.com/images/distinct-in-postgresql.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

DISTINCT in PostgreSQL is used with a SELECT statement to remove duplicate rows and return only unique values. It covers the SELECT syntax, the SQL Shell and pgAdmin, DISTINCT on single and multiple columns, and DISTINCT versus GROUP BY.

* 🔎 **SELECT:** Retrieve columns from a table with SELECT column FROM table.
* 🧹 **DISTINCT:** Remove duplicate rows and keep one row per unique value.
* ↕️ **ORDER BY:** Sort ascending by default, or descending with DESC.
* 🔢 **LIMIT:** Restrict the number of records the query returns.
* 🖥️ **Tools:** Run the same queries in the SQL Shell or pgAdmin.

[ Read More ](javascript:void%280%29;) 

![DISTINCT in PostgreSQL](https://www.guru99.com/images/distinct-in-postgresql.png)

You can retrieve data from the table using a SELECT statement.

**Syntax:**

SELECT [column names] FROM [table_name]

Here,

* **column names:** Name of the columns whose value you want to retrieve.
* **FROM:** The FROM clause defines one or more source tables for the SELECT.
* **table\_name:** The name of an existing table that you want to query.

## What is DISTINCT in PostgreSQL?

The **DISTINCT** clause in PostgreSQL is used with a SELECT statement to remove duplicate rows from the result set. When a column contains repeated values, SELECT DISTINCT returns only the unique values, keeping a single row for each group of duplicates. This is useful when you want a list of distinct entries, such as unique customer cities or product categories, without repetition.

DISTINCT evaluates all the columns listed in the SELECT statement to decide whether a row is unique. PostgreSQL also provides DISTINCT ON, which returns the first row for each distinct value of a specified expression. You can combine DISTINCT with ORDER BY to control which rows appear and with LIMIT to cap the number of results.

## PostgreSQL Select Statement in SQL Shell

**Step 1)** We have a table “tutorials” with 2 columns, “id” and “tutorial\_name”. Let us query it. Use the following query to list data in the table:

SELECT id,tutorial_name FROM tutorials;

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS1.png)

**NOTE:** Use the command \\c to connect to the database that contains the table you want to query. In our case, we are connected to database guru99.

**Step 2)** If you want to view all the columns in a particular table, we can use the asterisk (\*) wildcard character. This means it checks every possibility and, as a result, it will return every column.

SELECT * FROM tutorials;

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS2.png)

It displays all the records of the tutorials table.

**Step 3)** You can use the ORDER clause to sort data in a table based on a particular column. The ORDER clause organizes data in A to Z order.

SELECT * FROM tutorials ORDER BY id;

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS3.png)

You can sort from Z to A using “DESC” after the “ORDER BY” statement.

SELECT * FROM tutorials ORDER BY id DESC;

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS4.png)

**Step 4)** The SELECT DISTINCT in PostgreSQL clause can be used to remove duplicate rows from the result. It keeps one row for each group of duplicates.

Syntax:
SELECT DISTINCT column_1 FROM table_name;

Let us query Postgres Select Distinct id values from our table tutorials using distinct queries in [PostgreSQL](https://www.guru99.com/postgresql-tutorial.html):

SELECT DISTINCT(id) FROM tutorials;

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS5.png)

**Step 5)** You can use the PostgreSQL LIMIT clause to restrict the number of records returned by the SELECT query.

SELECT * FROM tutorials LIMIT 4;

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS6.png)

### RELATED ARTICLES

* [How to Download & Install PostgreSQL for Windows ](https://www.guru99.com/download-install-postgresql.html "How to Download & Install PostgreSQL for Windows")
* [PostgreSQL DROP/Delete DATABASE Using Command Line ](https://www.guru99.com/postgresql-drop-database.html "PostgreSQL DROP/Delete DATABASE Using Command Line")
* [PostgreSQL Join Types: Inner, Outer, Left, Right ](https://www.guru99.com/postgresql-joins-left-right.html "PostgreSQL Join Types: Inner, Outer, Left, Right")
* [PostgreSQL IN, Not IN with Examples ](https://www.guru99.com/postgresql-in-not.html "PostgreSQL IN, Not IN with Examples")

## PostgreSQL Select Statement in pgAdmin

**Step 1)** In the Object Tree:

1. Right click on the Table.
2. Select Scripts.
3. Click on SELECT SCRIPT.

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS7.png)

**Step 2)** In the panel on the right:

1. Edit the SELECT query if required.
2. Click the Lightning icon.
3. Observe the output.

[](https://www.guru99.com/images/1/092818%5F0705%5FPostgreSQLS8.png)

## DISTINCT on Multiple Columns

The SELECT DISTINCT clause can also apply to more than one column. When you list multiple columns after DISTINCT, PostgreSQL treats the combination of those columns as a unit and removes rows where that whole combination is duplicated.

Syntax:
SELECT DISTINCT column_1, column_2 FROM table_name;

For example, to return the unique combinations of tutorial\_name and id from the tutorials table:

SELECT DISTINCT tutorial_name, id FROM tutorials;

In this case, a row is removed only when both tutorial\_name and id match another row. If two rows share the same tutorial\_name but have different id values, both are kept, because the combination is still unique. This behaviour is important to remember: DISTINCT does not de-duplicate a single column when several columns are selected; it de-duplicates the full set of selected columns together.

## DISTINCT vs GROUP BY in PostgreSQL

Both DISTINCT and GROUP BY can remove duplicate values, but they serve different purposes:

* **DISTINCT** simply returns unique rows from the result and is best when you only need a de-duplicated list.
* **GROUP BY** groups rows that share a value so you can apply aggregate functions such as COUNT, SUM, or AVG to each group.

For example, the two queries below both return the unique id values:

SELECT DISTINCT id FROM tutorials;
SELECT id FROM tutorials GROUP BY id;

Use DISTINCT for a plain list of unique values, and use GROUP BY when you also need a calculation per group, such as counting how many tutorials share each id. On simple queries the planner often produces similar performance for both, but GROUP BY is required whenever aggregation is involved.

## Cheat Sheet

SELECT [column names] FROM [table_name] [clause]

Here are the various parameters:

* **column names:** Name of the columns whose value you want to retrieve.
* **FROM:** The FROM clause defines one or more source tables for the SELECT.
* **table\_name:** The name of an existing table that you want to query.

**Various clauses are:**

| Commands     | Description                                                                                                      |
| ------------ | ---------------------------------------------------------------------------------------------------------------- |
| **\***       | Fetches records for all the rows in the table.                                                                   |
| **DISTINCT** | DISTINCT in PostgreSQL helps you to remove duplicates from the result.                                           |
| **ORDER BY** | Sorts rows based on a column. Default sort order is ascending. Use the keyword DESC to sort in descending order. |
| **LIMIT**    | LIMIT in PostgreSQL restricts the number of records returned by the query.                                       |

## FAQs

🤖 Can AI write PostgreSQL DISTINCT queries from plain English?

Yes. AI assistants can turn a description into SELECT DISTINCT statements, add ORDER BY or LIMIT, and explain the result. You should still test the query on your schema before running it in production.

🧠 Can AI optimize slow DISTINCT queries in PostgreSQL?

Yes. AI tools can read the query plan from EXPLAIN ANALYZE and suggest indexes or rewriting DISTINCT as GROUP BY. Always validate suggestions with real data, since performance depends on table size and indexes.

🔧 What is the difference between DISTINCT and DISTINCT ON in PostgreSQL?

DISTINCT removes duplicate rows across all selected columns. DISTINCT ON (expression) keeps the first row for each distinct value of that expression, letting you pick one row per group, usually combined with ORDER BY.

⚡ Does DISTINCT slow down PostgreSQL queries?

DISTINCT can be slower on large tables because it must compare and remove duplicates, often using a sort or hash. Proper indexes and limiting the columns you select help reduce its cost.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/distinct-in-postgresql.png","url":"https://www.guru99.com/images/distinct-in-postgresql.png","width":"700","height":"250","caption":"DISTINCT in PostgreSQL","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/postgresql-select-distinct.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/postgresql","name":"PostgreSQL"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/postgresql-select-distinct.html","name":"DISTINCT in PostgreSQL: Select, Order By &#038; Limit (Examples)"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/postgresql-select-distinct.html#webpage","url":"https://www.guru99.com/postgresql-select-distinct.html","name":"DISTINCT in PostgreSQL: Select, Order By &#038; Limit (Examples)","dateModified":"2026-07-02T11:03:48+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/distinct-in-postgresql.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/postgresql-select-distinct.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/juniper","name":"Juniper Willow","description":"I am Juniper Willow, a PostgreSQL Developer, offering expert guidance to help you optimize and master PostgreSQL database development.","url":"https://www.guru99.com/author/juniper","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/juniper-willow-author.png","url":"https://www.guru99.com/images/juniper-willow-author.png","caption":"Juniper Willow","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"PostgreSQL","headline":"DISTINCT in PostgreSQL: Select, Order By &#038; Limit (Examples)","description":"DISTINCT in PostgreSQL - You can retrieve data from the table using a SELECT statement.","keywords":"postgresql, sql","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/juniper","name":"Juniper Willow"},"dateModified":"2026-07-02T11:03:48+05:30","image":{"@id":"https://www.guru99.com/images/distinct-in-postgresql.png"},"copyrightYear":"2026","name":"DISTINCT in PostgreSQL: Select, Order By &#038; Limit (Examples)","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Can AI write PostgreSQL DISTINCT queries from plain English?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI assistants can turn a description into SELECT DISTINCT statements, add ORDER BY or LIMIT, and explain the result. You should still test the query on your schema before running it in production."}},{"@type":"Question","name":"Can AI optimize slow DISTINCT queries in PostgreSQL?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI tools can read the query plan from EXPLAIN ANALYZE and suggest indexes or rewriting DISTINCT as GROUP BY. Always validate suggestions with real data, since performance depends on table size and indexes."}},{"@type":"Question","name":"What is the difference between DISTINCT and DISTINCT ON in PostgreSQL?","acceptedAnswer":{"@type":"Answer","text":"DISTINCT removes duplicate rows across all selected columns. DISTINCT ON (expression) keeps the first row for each distinct value of that expression, letting you pick one row per group, usually combined with ORDER BY."}},{"@type":"Question","name":"Does DISTINCT slow down PostgreSQL queries?","acceptedAnswer":{"@type":"Answer","text":"DISTINCT can be slower on large tables because it must compare and remove duplicates, often using a sort or hash. Proper indexes and limiting the columns you select help reduce its cost."}}]}],"@id":"https://www.guru99.com/postgresql-select-distinct.html#schema-1128620","isPartOf":{"@id":"https://www.guru99.com/postgresql-select-distinct.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/postgresql-select-distinct.html#webpage"}}]}
```
