Analytics

Wednesday, March 20, 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.

DISTINCT will retrieve the first value of each group in multiple groups containing duplicates. DISTINCT can operate on a single or multiple columns.

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.

SELECT DISTINCT city
FROM suppliers;

This SQL DISTINCT clause example would return all unique cities from the suppliers table.

Multiple Fields Example

The SQL DISTINCT clause can be used with more than one field in your SQL SELECT statement.

SELECT DISTINCT city, state
FROM suppliers;

This 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.

No comments:

Post a Comment