Postgresql Distinct
# PostgreSQL DISTINCT Keyword
In PostgreSQL, the DISTINCT keyword is used with the SELECT statement to remove duplicate records and retrieve only unique records.
When operating on data, it is possible to have multiple duplicate records in a table. When extracting such records, the DISTINCT keyword becomes particularly useful as it retrieves only unique records instead of duplicate ones.
### Syntax
The basic syntax of the DISTINCT keyword used to remove duplicate records is as follows:
```sql
SELECT DISTINCT column1, column2,.....columnN FROM table_name WHERE
### Examples
Create a COMPANY table ((https://static.jyshare.com/download/company.sql)), with the following data:
```sql
tutorialdb# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall | 45000
7 | James | 24 | Houston | 10000
(7 rows)
Let us insert two more rows:
```sql
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (8, 'Paul', 24, 'Houston', 20000.00);
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (9, 'James', 44, 'Norway', 5000.00);
Now let us check the data in the table:
```sql
tutorialdb# select * from COMPANY;
id | name | age | address | salary
----+-------+-----+------------+--------
1 | Paul | 32 | California | 20000
2 | Allen | 25 | Texas | 15000
3 | Teddy | 23 | Norway | 20000
4 | Mark | 25 | Rich-Mond | 65000
5 | David | 27 | Texas | 85000
6 | Kim | 22 | South-Hall | 45000
7 | James | 24 | Houston | 10000
8 | Paul | 24 | Houston | 20000
9 | James | 44 | Norway | 5000
(9 rows)
Now let us use the DISTINCT keyword to get unique records:
```sql
tutorialdb# SELECT DISTINCT name FROM COMPANY;
The above PostgreSQL statement will produce the following result:
```sql
name
-------
Teddy
Paul
Mark
James
Allen
David
Kim
(7 rows)
You can see that the duplicate name "Paul" and "James" have been removed and only unique names are displayed.
YouTip