When retrieving data from a table, there might be some data rows with the same value of a column.

To avoid such duplication, we can add DISTINCT to the SELECT statement:

SELECT DISTINCT column_name 
FROM table_name
WHERE [condition]

Example

We have this table upcoming_movies:

movie_name release_year universe
Joker 2019 DC
The New Mutants 2020 Marvel
Black Widow 2020 Marvel
Birds of Prey 2020 DC
The Eternals 2020 Marvel
Wonder Woman 1984 2020 DC
Doctor Strange and the Multiverse of Madness 2021 Marvel
Thor: Love and Thunder 2021 Marvel
The Batman 2021 DC
The Suicide Squad 2021 DC
Black Panther 2 2022 Marvel
Aquaman 2 2022 DC

To SELECT all universe value without duplication:

SELECT DISTINCT
  universe
FROM
  upcoming_movies;

The result is:

universe
DC
Marvel

When SELECT DISTINCT clause contains more than one column, the values of all the columns combined determine the uniqueness of rows.

For example, with the upcoming_movies above, we SELECT DISTINCT two columns universe and release_year:

SELECT DISTINCT
  universe,
  release_year
FROM
  upcoming_movies;

The results will be:

universe release_year
DC 2019
Marvel 2020
DC 2020
Marvel 2021
DC 2021
Marvel 2022
DC 2022

Need a good GUI tool for databases? TablePlus provides a native client that allows you to access and manage Oracle, MySQL, SQL Server, PostgreSQL, and many other databases simultaneously using an intuitive and powerful graphical interface.

Download TablePlus for Mac.

Not on Mac? Download TablePlus for Windows.

On Linux? Download TablePlus for Linux

Need a quick edit on the go? Download TablePlus for iOS

TablePlus in Dark mode