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.

admin

admin

Leave a Reply

Your email address will not be published.