sqlrelational-databasedatabase-agnostic

Any way to add a falsey `ORDER BY`?


I often add in a falsey predicate for convenience in debugging, like this:

SELECT ...
WHERE 1=1
    AND/OR predicate1
    AND/OR predicate2
    ...etc

This way I can comment out one or more lines and my query will still work. Is there a way to do the equivalent of 1=1 for an ORDER BY? For example, filling in the ?? in the following:

SELECT ...
WHERE ...
ORDER BY ???,
   sort1,
   sort2, 
   ...

I believe almost all mainstream db engines will optimize out 1=1, but I wonder if it does the same for an ORDER BY that is intentionally falsey?


Solution

  • Order by a constant, and use leading commas for your optional other sorts:

    ORDER BY 'x'
       , sort1
       , sort2