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


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


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.


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


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


The indexes used in the execution plan are also different:


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 […]