views:

989

answers:

10

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)?

+1  A: 

No, it is not implying a sort. In my experience, it sorts by the known index, which may happen to be foo.

Why be subtle? Why not specific Select Distinct foo from Bar Order by foo?

jerebear
How about because that's not the ordering you want?
le dorfier
In his post "sorting on foo (unless otherwise specified)" I'm asking why not specify foo unless otherwise specified
jerebear
Why be subtle and hint? I just don't want to have to recompile and redeploy at this point. If I could give oracle a hint, it would solve my problem and I could put the explicit fix in at a more convenient time.
S Ennis
ahhh...well that makes sense.
jerebear
What do you mean by "give the table a hint"? I've never heard of putting a hint on the table, only on SQL.
RussellH
+1  A: 

Not to my knowledge, no. The only reason I can think of is that SQL Server would internally sort the data in order to detect and filter out duplicates, and thus return it in a "pre-sorted" manner. But I wouldn't rely on that "side effect" :-)

marc_s
A: 

On at least one server I've used (probably either Oracle or SQL Server, about six years ago), SELECT DISTINCT was rejected if you didn't have an ORDER BY clause. It was accepted on the "other" server (Oracle or SQL Server). Your mileage may vary.

Dan Breslau
A: 

Is it possible that foo is set as the primary key in Oracle but not in Access?

Michael Todd
A: 

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

Stephen Pace
A: 

There is no "authoritative" answer link, since this is something that no SQL server guarantees.

You will often see results in order when using distinct as a side effect of the best methods of finding those results. However, any number of other things can mix up the results, and some server may hand back results in such a way as to not give them sorted even if it had to sort to get the results.

Bottom line: if your server doesn't guarantee something you shouldn't count on it.

dwc
+1  A: 

From the SQL92 specification:

If DISTINCT is specified, then let TXA be the result of eliminating redundant duplicate values from TX. Otherwise, let TXA be TX.

...

4) If an is not specified, then the ordering of the rows of Q is implementation-dependent.

Ultimately the real answer is that DISTINCT and ORDER BY are two separate parts of the SQL statement; If you don't have an ORDER BY clause, the results by definition will not be specifically ordered.

Chris Shaffer
A: 

Yes. Oracle does use a sort do calculate a distinct. You can see that if you look at the explain plan. The fact that it did a sort for that calculation does not in any way imply that the result set will be sorted. If you want the result set sorted, you are required to use the ORDER BY clause.

EvilTeach
Oracle 10 and 11 use hashing to calculate a distinct, not sorting.
tuinstoel
Oracle 10 and 11 *may* use hashing.
WW
May I can believe. I have observed sort in the plan in response to a distinct. I am stuck on 9i and 10g.
EvilTeach
+4  A: 

No. There are a number of circumstances in which a DISTINCT in Oracle does not imply a sort, the most important of which is the hashing algorithm used in 10g+ for both group by and distinct operations.

Always specify ORDER BY if you want an ordered result set, even in 9i and below.

David Aldridge
I don't think that answers the question. Of course you have to specify both if you want both. But is a sort performed based on the DISTINCT regardless of your requested sort (or no sort)?
le dorfier
Q: "Does including DISTINCT in a SELECT query imply that the resulting set should be sorted?"A: "There are a number of circumstances in which a DISTINCT in Oracle does not imply a sort"Prior to 10g a DISTINCT result set **usually** was sorted, but not always.
David Aldridge
A: 

As the answers mostly say, DISTINCT does not mandate a sort - only ORDER BY mandates that. However, one standard way of achieving DISTINCT results is to sort; the other is to hash the values (which tends to lead to semi-random sequencing). Relying on the sort effect of DISTINCT would be foolish.

Jonathan Leffler