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:
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. If used, the DISTINCT keyword must be placed directly in front of the column names.
The DISTINCT keyword applies to all columns, not just the one it precedes. If you were to specify SELECT DISTINCT vend_id, prod_price, all rows would be retrieved unless both of the specified columns were distinct.
0 comments:
Post a Comment