Home > Articles

  • Print
  • + Share This
This chapter is from the book

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:

The SELECT statement returned nine rows (even though there are only three unique vendors in that list) because there are nine 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.

Analysis ▾

SELECT DISTINCT vend_id tells the DBMS to only return distinct (unique) vend_id rows, and so only three rows are returned, as seen in the following output. If used, the DISTINCT keyword must be placed directly in front of the column names.

  • + Share This
  • 🔖 Save To Your Account