Retrieving Distinct Rows
As you have seen, SELECT returns all matched rows. But what if you do not want every occurrence of every value? For example, suppose you want the vendor ID of all vendors with products in your products table:
SELECT vend_id FROM Products;
vend_id ---------- BRS01 BRS01 BRS01 DLL01 DLL01 DLL01 DLL01 FNG01 FNG01
The SELECT statement returned 14 rows (even though there are only four vendors in that list) because there are 14 products listed in the products table. So how could you retrieve a list of distinct values?
The solution is to use the DISTINCT keyword which, as its name implies, instructs the database to only return distinct values.
SELECT DISTINCT vend_id FROM Products;
SELECT DISTINCT vend_id tells the DBMS to only return distinct (unique) vend_id rows, and so only four rows are returned, as seen in the following output. If used, the DISTINCT keyword must be placed directly in front of the column names.
vend_id ---------- BRS01 DLL01 FNG01