Order by Alias
suggest changeBecause ORDER BY is processed after SELECT, a select-list alias can be used in ORDER BY. It cannot be used in WHERE.
SELECT DisplayName, JoinDate as jd, Reputation as rep
FROM Users
ORDER BY jd, rep
You can also order by the 1-based position of a select-list item. DisplayName is 1, JoinDate is 2, and Reputation is 3. Reordering the select list silently changes the sort, so names are safer.
SELECT DisplayName, JoinDate as jd, Reputation as rep
FROM Users
ORDER BY 2, 3
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents