Wednesday, January 9, 2013

DISTINCT Clause


The SQL DISTINCT clause allows you to remove duplicates from the result set. The SQL DISTINCT clause can only be used with SQL SELECT statements.
The syntax for the SQL DISTINCT clause is:
SELECT DISTINCT columns
FROM tables
WHERE predicates;

SQL DISTINCT Clause - Single field example

The simplest way to use the SQL DISTINCT clause would be to return a single field that removes the duplicates from the result set.
For example:
SELECT DISTINCT city
FROM suppliers;
This SQL DISTINCT clause example would return all unique cities from the suppliers table.

SQL DISTINCT Clause - Multiple fields example

The SQL DISTINCT clause can be used with more than one field in your SQL SELECT statement.
For example:
SELECT DISTINCT city, state
FROM suppliers;
This SQL DISTINCT clause example would return each unique city and state combination. In this case, the DISTINCT applies to each field listed after the DISTINCT keyword.