Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Sorting Algorithm

There are so many sorting algorithms, but I want to know which algorithm is used in SQL Server when we use Order by and without Order by.

like image 926
sam11 Avatar asked May 04 '11 07:05

sam11


People also ask

What sorting algorithm does SQL Server use?

There are two different sort logics used by SQL Server to handle sorting! First one is the quick sort and another one is Merge sort. It begins sort in memory using Quick Sort algorithm but note that the memory requirement for this is at least 200% of its input rows size.

What search algorithm does SQL use?

SQL Server's algorithm, which searches the index tree to find the exact row in the leaf page, uses the extra complexity of the index to great advantage, making the index faster than either the O(N/2) table scan or an O(Log2N) binary search.

What are SQL algorithms?

SQL Server Data Mining includes the following algorithm types: Classification algorithms predict one or more discrete variables, based on the other attributes in the dataset. Regression algorithms predict one or more continuous numeric variables, such as profit or loss, based on other attributes in the dataset.

Which algorithm is best for sorting?

Quicksort. Quicksort is one of the most efficient sorting algorithms, and this makes of it one of the most used as well. The first thing to do is to select a pivot number, this number will separate the data, on its left are the numbers smaller than it and the greater numbers on the right.


1 Answers

If you don't use ORDER BY, then there is no implied or natural order. So no algorithm. This applies to most RDBMS. Only ORDER BY will give any ordering of results.

When you do use ORDER BY, it follows the column list, asc/desc, collation, expressions etc that you specify. The only non-intuitive rule I can think of is "NULLs first" for a column but otherwise sorts are straightforward in SQL Server.

like image 191
gbn Avatar answered Nov 16 '22 00:11

gbn