The PostgreSQL DISTINCT catchphrase is utilized related to SELECT explanation to wipe out all the copy records and getting just exceptional records.
There might be a circumstance when you have different copy records in a table. While getting such records, it bodes well to get just exceptional records as opposed to bringing copy records.
Syntax
The fundamental sentence structure of DISTINCT watchword to wipe out copy records is as per the following −
SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]
Example
Consider the table COMPANY having records as follows −
# 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)
Allow us to add two additional records to this table as follows −
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (8, 'Paul', 32, 'California', 20000.00 );
INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (9, 'Allen', 25, 'Texas', 15000.00 );
Presently, the records in the COMPANY table would be −
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 | 32 | California | 20000
9 | Allen | 25 | Texas | 15000
(9 rows)
To start with, let us perceive how the accompanying SELECT inquiry returns copy pay records −
testdb=# SELECT name FROM COMPANY;
This would deliver the accompanying outcome −
name
-------
Paul
Allen
Teddy
Mark
David
Kim
James
Paul
Allen
(9 rows)
Presently, let us utilize DISTINCT catchphrase with the above SELECT inquiry and see the outcome −
testdb=# SELECT DISTINCT name FROM COMPANY;
This would deliver the accompanying outcome where we don't have any copy passage −
name
-------
Teddy
Paul
Mark
David
Allen
Kim
James
(7 rows)