SQL row return order

后端 未结 5 1602
失恋的感觉
失恋的感觉 2020-12-11 03:46

I have only used SQL rarely until recently when I began using it daily. I notice that if no \"order by\" clause is used:

  1. When selecting part of a table the row
5条回答
  •  情深已故
    2020-12-11 04:07

    It never ceased to amaze me when I was a DBA that this feature of SQL was so often thought of as quirky. Consider a simple program that runs against a text file and produces some output. If the program never changes, and the data never changes, you'd expect the output to never change.

    As for this:

    If no ORDER BY clause is included in the query, the returned order of rows is undefined.

    Not strictly true - on every RDBMS I've ever worked on (Oracle, Informix, SQL Server, DB2 to name a few) a DISTINCT clause also has the same effect as an ORDER BY as finding unique values involves a sort by definition.

    EDIT (6/2/14):

    Create a simple table

    enter image description here

    For DISTINCT and ORDER BY, both the plan and the cost is the same since it is ostensibly the same operation to be performed

    enter image description here

    enter image description here

    And not surprisingly, the effect is thus the same

    enter image description here

提交回复
热议问题