8 November 2012

Without Using DISTINCT Keyword retriving Unique values


employee
employee_nameemployee_location
JoeNew York
SunilIndia
AlexRussia
AlbertCanada
JackNew York
AlexRussia
We can actually accomplish this with the GROUP BY keyword. Here’s what the SQL would look like:
SELECT employee_location from employee 
GROUP BY employee_location
Running this query will return the following results:
employee_location
New York
India
Russia
Canada
So, you can see that the duplicate values for "Russia" and "Canada" are not returned in the results.
This is a valid alternative to using the DISTINCT keyword. If you need a refresher on the GROUP BY clause, then check out this question: Group By and Having. This question would probably be asked just to see how good you are with coming up with alternative options for SQL queries. Although, it probably doesn’t prove much about your SQL skills.