Monday, April 14, 2014

T SQL Tutorial: DISTINCT

T SQL (Transact) Tutorial: DISTINCT
Check T SQL DISTINCT clause with syntax and examples.
Theory
The SQL DISTINCT clause is used to remove duplicates result set.The syntax for the SQL DISTINCT clause is:
SELECT DISTINCT expressions
FROM tables
WHERE conditions;
 
Point to remember
 
When only one expression is provided in the DISTINCT clause, the query will return the unique values for that expression.
When more than one expression is provided in the DISTINCT clause, the query will retrieve unique combinations for the expressions listed.
 
Distinct With Single field
Let's look at the simplest SQL DISTINCT query example. We can use the SQL DISTINCT clause to return a single field that removes the duplicates from the result set.
For example:
SELECT DISTINCT city
FROM Customers;
This SQL DISTINCT example would return all unique city values from the Customers table.
Distinct With Multiple fields
Let's look at 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 city, state
FROM Customers;
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. If there are multiple column distinct will apply that many  time with multiple of rows.
 

No comments:

Post a Comment