views:

56

answers:

1

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

+3  A: 

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

John Nolan
can you explain more i cann't get what you saiedis this mean that name,salary,addr are dictinct with writing dictinct in after select word
sama