Sunday, 30 March 2014

SQL: DISTINCT CLAUSE

SQL DISTINCT clause with examples.

DESCRIPTION

SQL DISTINCT clause is used to remove duplicates from the result set of a SELECT statement.

SYNTAX

The syntax for the SQL DISTINCT clause is:
SELECT DISTINCT expressions FROM tables WHERE conditions;

expressions are the columns.
tables are the tables that you wish to retrieve records from..
conditions are conditions that must be met for the records to be selected.

EXAMPLE - WITH SINGLE FIELD

the simplest SQL DISTINCT query example. the SQL DISTINCT clause to return a single field that removes the duplicates from the result set.

For example:
SELECT DISTINCT designation FROM employee;

This SQL DISTINCT example would return all unique designation values from the employee table.

EXAMPLE - WITH MULTIPLE FIELDS

How you might use the SQL DISTINCT clause to remove duplicates from more than one field in your SQL SELECT statement.

For example:
SELECT DISTINCT designation, location FROM employee;

This SQL DISTINCT clause example would return each unique designation and location combination. In this case, the DISTINCT applies to each field listed after the DISTINCT keyword.


No comments:

Post a Comment