Quick answer: The SQL DISTINCT keyword removes duplicate rows from a query result based on the selected expressions. For example, SELECT DISTINCT department FROM employees; returns each department value once. DISTINCT does not delete duplicate records from the underlying table.
While working with huge data there may be a situation when you have multiple duplicate records in a table. While fetching such records, you want to fetch only unique/distinct records instead of fetching duplicate records.
For such type of cases, DISTINCT is an keyword in SQL/PostgreSQL. It allows to show unique and distinct values.
Syntax for SQL Distinct in Single Column
SELECT
DISTINCT column_name
FROM
table_name;
Here the values in the column_name column are used to evaluate the duplicate.
Understanding with an example
For example, we have a movies table.
First, let’s select all the data from movies table as shown in below image.

As you can see, movies table have total 53 records.
Now suppose you need to check that how many languages are there for movies.
Simply write the below query.
SELECT movie_lang
FROM movies;
Below is the output.

But as you can see there is a lot of duplicate values and you need unique values.
For this you need DISTINCT keyword.
--Getting distinct movies language from movies table
SELECT DISTINCT movie_lang
FROM movies;
The output is

Now what about taking multiple columns like movies language with director’s id.
Syntax for SQL Distinct in Multiple Column
If you specify multiple columns, the DISTINCT clause will evaluate the duplicate based on the combination of values of these columns.
SELECT DISTINCT column_name1, column_name2
FROM table_name
ORDER BY column_name1, column_name2;
With ordinary SELECT DISTINCT, PostgreSQL compares the entire selected row: a pair appears once even if either value occurs in other pairs. This differs from PostgreSQL’s DISTINCT ON, which selects one row per specified grouping expression.
Use ORDER BY when the displayed order matters. Without it, DISTINCT does not guarantee result order. For DISTINCT ON, the leading ORDER BY expressions must match the DISTINCT ON expressions; add further sort expressions to choose the preferred row.
Example for Distinct in Multiple Column
Let’s understand this with an example
--Getting multiple distinct values
SELECT DISTINCT movie_lang,directors_id
FROM movies
ORDER BY 1;
The output will be

You can see there are still multiple movie languages and for each movie language there is a corresponding director’s id.
It works with the combination of columns. It is combining the movies language with the director’s id together and as a whole it returns distinct values for each of the column.
Reproducible DISTINCT Example Without Existing Tables
Run this read-only query in PostgreSQL. It uses a common table expression, so no sample table needs to be created:
WITH sample(department, city) AS (
VALUES ('Sales', 'Mumbai'),
('Sales', 'Mumbai'),
('Sales', 'Pune'),
('HR', 'Mumbai'),
('HR', NULL),
('HR', NULL)
)
SELECT DISTINCT department, city
FROM sample
ORDER BY department, city NULLS LAST;Expected result: four rows: HR/Mumbai, HR/NULL, Sales/Mumbai and Sales/Pune (ordered by department and then city). Duplicate rows and duplicate NULL-containing pairs appear only once. No rows are deleted from the source data.
When DISTINCT Is Not the Right Tool
Use GROUP BY with aggregates when you need counts or totals per group; use ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...) when you need a portable, explicitly ranked choice per group. Do not add DISTINCT merely to hide unexpected duplicates caused by a join: first check whether the join keys and relationship cardinality are correct. On large datasets, compare execution plans with EXPLAIN and test with representative data before making performance claims.
DISTINCT Across Multiple Columns
SELECT DISTINCT department, city FROM employees returns unique department-and-city combinations, not one row per department. If either column differs, the rows remain distinct. This behavior is important when choosing columns for reports.
DISTINCT and NULL Values
For DISTINCT comparisons, PostgreSQL treats duplicate NULL results as duplicates, so SELECT DISTINCT on a column returns at most one NULL entry. DISTINCT can require sorting or hashing, so consider query plans and indexes for large datasets.
DISTINCT vs. GROUP BY and DISTINCT ON
GROUP BY is typically used when aggregating rows. PostgreSQL also supports DISTINCT ON (expression), which keeps the first row for each group according to ORDER BY. Use a deterministic ORDER BY to choose which row is retained; DISTINCT ON is PostgreSQL-specific.
PostgreSQL DISTINCT ON: Keep One Row per Group
Unlike ordinary DISTINCT, PostgreSQL’s DISTINCT ON retains one row from each group. For example, to return the movie with the highest ID in each language, use the following query (assuming movie_id exists):
SELECT DISTINCT ON (movie_lang) movie_lang, movie_id
FROM movies
ORDER BY movie_lang, movie_id DESC;
The first ORDER BY expression matches DISTINCT ON (movie_lang); movie_id DESC selects the largest ID within each language. If IDs are not unique, add a stable tie-breaker to make the selected row deterministic. This is different from SELECT DISTINCT movie_lang, movie_id, which returns every different language-and-ID pair.
Common DISTINCT Mistakes and FAQs
Does DISTINCT delete duplicates? No. It changes only the query result. Can I order by a column I did not select? With ordinary SELECT DISTINCT, PostgreSQL requires ORDER BY expressions to appear in the select list. Why is my result still repeated? Check every selected expression: differences in any one of them make the rows distinct. Does DISTINCT ignore NULL? No; a single-column distinct result can include one NULL row.
For related SQL concepts, see the official PostgreSQL SELECT reference.
Related guides: CREATE TABLE in PostgreSQL: Syntax, Constraints and Examples · PostgreSQL Constraints Explained: PRIMARY KEY, FOREIGN KEY and CHECK · How to Get Distinct Values in Excel: UNIQUE, Filter & Duplicates

C P Gupta is a YouTuber and Blogger. He is expert in Microsoft Word, Excel and PowerPoint. His YouTube channel @pickupbrain is very popular and has crossed 9.9 Million Views.
