In the table below, how would you retrieve the unique values for the employee_location without using the DISTINCT keyword?

 
employee
employee_name employee_location
Joe New York
Sunil India
Alex Russia
Albert Canada
Jack New York
Alex Russia

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.

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.