Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort Order of results for Select WITHOUT WHERE or ORDER BY Clause

I have a table with a PK clustered index as well as other indexes on it, both unique and non-unique. If I issue (exactly):

SELECT * FROM table_name

or

SELECT col1, col2 FROM table_name

in what order will the rows be returned?

This is the first question in an interview questionnaire a customer has forwarded us. Here are the instructions:

If the answer to this question is incorrect, terminate the interview immediately! The individual, regardless of their stated ability does not understand SQL-Based relational database management systems. This is SQL-101 logic for the past 25+ years. The correct answer is: “unknown/random/undetermined because no ORDER BY clause was specified as part of the query”.

I am somehow not convinced that this is actually correct. All comments welcome.

Thanks,

Raj

like image 979
Raj Avatar asked Dec 23 '11 06:12

Raj


People also ask

How do you sort data without using ORDER BY?

You could make your query without ORDER BY, put every row into a multidimensional array and after that sorting your array. It can be done in several languages. That is the antithesis of the requested answer.

Can we use ORDER BY without WHERE clause?

ORDER BY Characteristics: The ORDER BY clause is used to get the sorted records on one or more columns in ascending or descending order. The ORDER BY clause must come after the WHERE, GROUP BY, and HAVING clause if present in the query.

In what order are SQL results returned if there is no ORDER BY clause?

If you don't specify an ORDER BY , then there is NO ORDER defined. The results can be returned in an arbitrary order - and that might change over time, too.

Can I use column in ORDER BY without specifying in SELECT?

Yes, you can order by a field(s)even if it is not your in your select statement but exists in your table. For a group by clause though you'd need it to be in your select statement. is it possible to give an SELECT statement ...


2 Answers

Even if a table has a primary key/clustered index, you can't be sure about the order of rows. Although in the execution plan there will be an index/heap scan at the end, if query is performed in parallel on many cores, the resulting dataset won't be sorted due to parallel streams merge plan step.

You probably won't see it on small databases, but try creating one with many files on separate harddrives and run a simple query on a multicore machine. Most likely you'll get results "partialy sorted" by ID - i.e. there will be blocks where rows are sorted, but blocks will be retrieved in semi-random order.

like image 75
MagnatLU Avatar answered Oct 07 '22 18:10

MagnatLU


The instructions speak to SQL at a conceptual level, at which the result of a query is a relationship, and relationships are unordered. Moving from the conceptual to the actual, the reason no implicit ordering is defined in the SQL standard is so RDBMSs are free to return whatever order is most efficient for their implementation.

like image 40
outis Avatar answered Oct 07 '22 18:10

outis