Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default sort order for a select query in SQL Server 2005 and SQL Server 2012

Is there a difference between default sort order for a select query in SQL Server 2005 and SQL Server 2012?

I have a table variable without a primary key. When I execute a select query on the table variable in SQL Server 2005, the records are selected and displayed in alphabetical order as per one of the columns. In SQL Server 2012, the records are displayed in the same order as in the parent table.

like image 597
Krithika R Avatar asked Mar 18 '16 06:03

Krithika R


People also ask

What is the default sort order on SELECT statement?

The result of the SELECT statement is sorted in an ascending or descending order. An ordering term can be a column in the result list, an alias as specified in the result list, or a column index number in the result list, with or without ASC or DESC . The default sort order is ascending.

What is the default sort order in SQL Server?

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 sort order for a SQL Azure?

ASC sorts from the lowest value to highest value. DESC sorts from highest value to lowest value. ASC is the default sort order.

How do I sort SQL from newest to oldest?

If you'd like to see the latest date first and the earliest date last, you need to sort in descending order. Use the DESC keyword in this case. ORDER BY ExamDate DESC ; Note that in T-SQL, NULL s are displayed first when sorting in ascending order and last when sorting in descending order.


1 Answers

There is no default sort order. Unless you specify it in the ORDER BY clause, there is no guarantee that the result will be returned the same way all the time.

You might observe that the result is ordered by the PK or the clustered index, but that's not always going to be the case.


You might want to read this:

Without ORDER BY, there is no default sort order by Alexander Kuznetsov

like image 175
Felix Pamittan Avatar answered Sep 28 '22 16:09

Felix Pamittan