Why would someone put `where 1=1` in a query?

Some dynamic query builders include this condition so that any "real" conditions can be added with an AND without doing a check like if (first condition) 'WHERE' else 'AND'.


If you have many SQL statement building points in your program which generate similar queries, you can mark the examined one by this trick. If the sentence is about counting, you may use code below so you can grep out 42 from a SQL log.

select count(42) from table

It provides a situation that is always true, so it doesn't affect the results, but you know there is one item in the WHERE clause already.