SQL Server Interview Question #51

What is ORDER BY?

SELECT, Filtering, Sorting & Built-in Functions Mid-Level Intermediate

Detailed Explanation

ORDER BY sorts the final query result. ASC sorts ascending and is the default; DESC sorts descending.

Without ORDER BY, SQL Server does not guarantee row order, even if results appear consistently ordered during testing. For deterministic paging, ORDER BY should use a key or combination of columns that provides a stable ordering.

Code Example

SELECT Id, Name, Price
FROM dbo.Products
ORDER BY Price DESC, Id ASC;