You can sort on columns by their column number

Published: {ts '2020-08-18 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/you-can-sort-on-columns-by-their-column-number/

So I was looking back through some very old code when I happened across this sql snippet that reminded me this was possible.

Normally when you are writing sql you name the column(s) you want to order on, however it is possible (at least in SQL Server) to order on a column by its numeric column number in the query. Take for example the following query:

SELECT firstname ,lastname ,dob FROM users ORDER BY lastname

We can rewrite this query as:

SELECT firstname ,lastname ,dob FROM users ORDER BY 2

Note that the value 2 corresponds to the 'lastname' column lastname (1 indexed).

Warning: Unless you have an extremely unique use case such as some kind of dynamically generated sql system I cannot recommend this as a good idea. It is easily broken by changes to the sql and obfuscates the intent of the order condition. However it is useful to be aware of in case you ever encounter it.