Do you want to sort by primary key without specifying order by in SQL

Time:2021-10-13

Order by is not specified in SQL. Is the sorting based on the primary key? The answer is not necessarily. for instance:

Query the attendceemprank table. The primary key is attendceemployeerank ID and it is a clustered index

1

Executing the following statement, it is found that the first sentence does not specify order by, and the result is different from the second sentence.

2

Looking at the execution plan, we can see that the first sentence uses the date index and the second sentence uses the primary key index

3

Let’s look at another set of SQL and query results:

4

The indexes used in the execution plan are also different:

5

Therefore, it is concluded that without specifying order by, SQL server will obtain data according to the actual query method of the execution plan, and the execution plan will use different indexes according to many factors in SQL (query columns, where conditions, order by, etc.), and the final results are likely to be different.

Recommended Today

Swift advanced (XV) extension

The extension in swift is somewhat similar to the category in OC Extension can beenumeration、structural morphology、class、agreementAdd new features□ you can add methods, calculation attributes, subscripts, (convenient) initializers, nested types, protocols, etc What extensions can’t do:□ original functions cannot be overwritten□ you cannot add storage attributes or add attribute observers to existing attributes□ cannot add parent […]