Reputation:
I want to know can I use distinct in different columns when writing a SQL query. If so then what is the statement if I have employee table that has id,name, salary, addr as columns
Upvotes: 0
Views: 162
Reputation: 29657
No
Distinct works across the whole tuple.
You cannot have
SELECT DISTINCT(name), salary, addr, Id from employee
If you want to group by salary you could do something like
SELECT salary, name, addr, Id from employee
GROUP BY name, addr, Id
To expand further
When you use distinct it eliminates duplicates of the whole result set
So if your table is like so
1 'John' '1 my street' '$1000'
2 'Janet''1 my street' '$1000'
and you call
SELECT DISTINCT addr, salary FROM employee
you will get 1 result
'1 my street' '$1000'
but if you want to call
SELECT DISTINCT addr, salary, **name** FROM employee
you will get 2 results
'John' '1 my street' '$1000'
'Janet''1 my street' '$1000'
You cannot say get me distinct salary and address, but with different names. it doesn't make sense
Upvotes: 3