What does "ORDER BY (SELECT NULL)" mean? What does "ORDER BY (SELECT NULL)" mean? sql-server sql-server

What does "ORDER BY (SELECT NULL)" mean?


ROW_NUMBER requires an ORDER BY clause syntactically. You cannot use it without one. SELECT NULL is a hack to shut up the error while not enforcing any particular order. In this case we don't need to enforce any order, so the fastest option is to use SELECT NULL.

The optimizer sees through this trick, so it has no runtime cost (this claim is easily verified by looking at the execution plan).