In PostgreSQL
DISTINCT
Keywords vs.
SELECT
Statement to remove duplicate records and get only unique records.
When we are usually manipulating data, there may be a situation in which there are multiple duplicate records in a table, and when extracting such records Used to remove duplicate records Create Let’s insert two pieces of data: Now the figures are as follows: Next, let’s find all the NAME in the COMPANY table: The results are as follows: Now we’re here. The results are as follows: As you can see from the results, the duplicate data has been deleted.
DISTINCT
The keyword is particularly meaningful because it only gets a single record, not a duplicate record. 5.27.1. Grammar ¶
DISTINCT
The basic syntax for keywords is as follows:SELECT DISTINCT column1, column2,.....columnN
FROM table_name
WHERE [condition]
5.27.2. Example ¶
COMPANY
表( 下载 COMPANY SQL 文件 ), the data are as follows:runoobdb# 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)
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 );
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)
runoobdb=# SELECT name FROM COMPANY;
name
-------
Paul
Allen
Teddy
Mark
David
Kim
James
Paul
Allen
(9 rows)
SELECT
Statement using the
DISTINCT
Clause:runoobdb=# SELECT DISTINCT name FROM COMPANY;
name
-------
Teddy
Paul
Mark
David
Allen
Kim
James
(7 rows)