Contents /
Previous /
Next
Distinct Rows
The keyword DISTINCT prevents duplicates from being returned:
SELECT DISTINCT .... FROM table WHERE condition
Example:
workshop=# SELECT * FROM stud;
id | name | semester | diploma | gender
----+------+----------+---------+-----
1 | fred | 2 | bio | m
3 | tom | 1 | bio | m
4 | john | 3 | phy | m
2 | lisa | 2 | bio | f
(4 rows)
workshop=# SELECT DISTINCT gender FROM stud;
gender
-----
f
m
(2 rows)
Note that DISTINCT operates on all the columns selected in the query
together, thus a difference in one of them is enough to be distinct:
workshop=# SELECT DISTINCT gender, diploma FROM stud;
gender | diploma
--------+---------
f | bio
m | bio
m | phy
(3 rows)