***** NOTE: IMO, this is NOT a duplicate***** There are multiple questions that show the same example query statement but those are about selecting random row. They do not explain specifically on SYNTAX of how the order by clause allows usage of a column not present in Select part, although they explain how the random generation works. If the above query were to use a function like tan(45) in the order by list it wont work. So what determines, a) which functions are allowed in the ORDER by clause? b) And why some function are added into the select list in the background and some are not.
How does SQL server allow ordering by newid(),rand() functions even when the column is not present in select list? Note: My question is NOT about how to generate random numbers.
select top 1 * from mytable order by newid()
SQL server rules specifically state:
The column names referenced in the ORDER BY clause must correspond to either a column in the select list or to a column defined in a table specified in the FROM clause without any ambiguities.
If they are an exemption, are there specific rules around that so we know how they can be used further in other scenarios?