Order by Alias

suggest change

Because 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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents