Home page   WebCheatSheet.com


SQL: DISTINCT clause


Select lets you use the Distinct keyword to eliminate duplicate rows from the query results. Distinct is very useful in queries where you simply want to know if a value is present in a table and are not interested in how many times it occurs. Distinct itself can only be used once in a Select statement. However, you can specify more than one column after Distinct. In this case, SQL will eliminate those rows where the values are the same in all the columns.

The syntax for the Distinct clause is:

SELECT DISTINCT column_name
FROM derived_table
WHERE conditions

Example #1

SELECT DISTINCT OWNERLASTNAME
FROM AntiqueOwners

Consider a table of lastnames, where you have the last name, 'Jones', repeated numerous times. This code returns only one 'Jones'.

Example #2

SELECT DISTINCT CITY,STATE
FROM EmployeeAddressTable

This SQL statement will return each unique city and state combination.


If you'd like to see how it works, enter SQL statement and press Execute
 



Copyright © 2005-2007 www.WebCheatSheet.com All Rights Reserved.