A formatted date alias can change ORDER BY from a date sort into a text sort. The tests here reproduce the wrong order, show three ways to sort by the real date, and explain the two errors you hit when you try to be clever with the alias.
SQL SERVER – Adding Values WITH OVER and PARTITION BY
How do you sum values for each group without GROUP BY and still keep every row? Here is how SUM with OVER and PARTITION BY answers a reader’s question.
How Does TOP WITH TIES Work in SQL Server? – Interview Question of the Week #159
Question: How Does TOP WITH TIES Work in SQL Server?
Answer: This is an ancient question I just encountered in the recent Comprehensive Database Performance Health Check.
How to Keep Certain Records on Top While Ordering Data? – Interview Question of the Week #110
An interview question: how do you keep one city on top while ordering data by name for the rest? The answer is a small trick in the ORDER BY clause.
Sort Query Using Dynamic Variables Without EXEC – Interview Question of the Week #074
How do you sort query results by a column passed in a variable? Here is how to sort query output dynamically without EXEC or sp_executesql.
BULK INSERT ORDER Hint: Loading Pre-Sorted Files Without a Sort
The ORDER hint does not sort anything. It is a promise about your file, and SQL Server checks it. Here is what happens when you break it.
The Last Entered Row: Why a Table Needs an Ordering Column
A table has no last row until you define one. Here is how identity, event time and ties give different answers.







