How to use DISTINCT in SQL
I am in search of someone who can help me in my SQL query. I am having problem with my DISTINCT. My query is as below:
Code:
SELECT DISTINCT(empId) FROM (SELECT empId FROM Employee WHERE Name LIKE 'R%')
But the above query gives me the message as "Incorrect syntax near ')". Can anyone help me figure out how to use DISTINCT in SQL?
Re: How to use DISTINCT in SQL
The SQL DISTINCT command retrieves only unique data entries depending on the column list you have specified after it. The SELECT statement provides us all the information from column(s) on a table. Situations may arise where you will be find a lot of duplicates in the result that you got. To remove all this duplicates, we need to use DISTINCT after SELECT keyword. The syntax is as follows:
Quote:
SELECT DISTINCT col_name
FROM table1
Re: How to use DISTINCT in SQL
You can even do the same thing as in here:
Code:
SELECT DISTINCT empId FROM Employee WHERE Name LIKE 'R%'
In other words, you don't need to add a sub-query in your SELECT statement. You can directly specify the condition in your query.