youv youv - 5 months ago 11
SQL Question

Distinct not working as expected with sql

How does distinct work with the following table:

id | id2 | time
-------------------
1 | 5555 | 12
2 | 5555 | 12
3 | 5555 | 33
4 | 9999 | 44
5 | 9999 | 44
6 | 5555 | 33

select distinct * from table

Answer

If you use select distinct * from table all the row are distinct

if you use

 select distinct id2  , time from table

then you obtain

    id2 | time

   5555 | 12
   5555 | 33
   9999 | 44

With distinct you obtain the distinct rows based on the result of the select