Alex Rivera | Logout

Does SELECT DISTINCT imply a sort of the results

Asked 2009-03-27T21:39:29.890
9

Does including DISTINCT in a SELECT query imply that the resulting set should be sorted?

I don't think it does, but I'm looking for a an authoritative answer (web link).

I've got a query like this:

Select Distinct foo
From Bar

In oracle, the results are distinct but are not in sorted order. In Jet/MS-Access there seems to be some extra work being done to ensure that the results are sort. I'm assuming that oracle is following the spec in this case and MS Access is going beyond.

Also, is there a way I can give the table a hint that it should be sorting on foo (unless otherwise specified)?

Edit
Report

1 Answer

0

No, the results are not sorted. If you want to give it a 'hint', you can certainly supply an ORDER BY:

select distinct foo from bar order by foo

But keep in mind that you might want to sort on more than just alphabetically. Instead you might want to sort on criteria on other fields. See:

http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx

answered 2009-03-27T21:50:17.120

Your Answer