Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default row ordering for select query in oracle

In Oracle, what is the the default ordering of rows for a select query if no "order by" clause is specified.

Is it

  1. the order in which the rows were inserted
  2. there is no default ordering at all
  3. none of the above.
like image 431
sengs Avatar asked May 22 '09 19:05

sengs


People also ask

What is the default ordering of rows returned from a SELECT query?

When you issue a SELECT , the rows often get returned in the same order they were inserted in the table. You can change the order of the rows by adding an ORDER BY clause at the end of your query, with a column name after. By default, the ordering will be in "ascending order", from lowest value to highest value.

What is the default ordering when using ORDER BY in a query?

The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default.

What is the default sorting order in SQL?

By default, SQL Server sorts out results using ORDER BY clause in ascending order.

What is ORDER BY in Oracle SQL?

It can also be used in an INSERT statement or a CREATE VIEW statement. An ORDER BY clause allows you to specify the order in which rows appear in the result set.


Video Answer


1 Answers

According to Tom Kyte: "Unless and until you add "order by" to a query, you cannot say ANYTHING about the order of the rows returned. Well, short of 'you cannot rely on the order of the rows being returned'."

See this question at asktom.com.

As for ROWNUM, it doesn't physically exist, so it can't be "freed". ROWNUM is assigned after a record is retrieved from a table, which is why "WHERE ROWNUM = 5" will always fail to select any records.

@ammoQ: you might want to read this AskTom article on GROUP BY ordering. In short:

Does a Group By clause in an Query gaurantee that the output data will be sorted on the Group By columns in order, even if there is NO Order By clause?

and we said...

ABSOLUTELY NOT,

It never has, it never did, it never will.

like image 60
DCookie Avatar answered Sep 27 '22 20:09

DCookie