Why 'WHERE 1=1'?

Publish date: 2020-02-12

Why 'WHERE 1=1?

Have you ever seen code like this:

SELECT
A
, B
, C
FROM TabA
WHERE 1=1
AND A = 'Foo'
AND B = 'Bar'

The simple answer is that, this is just for some lazy debugging.

Let's say you have the similar code:

SELECT
A
, B
, C
FROM TabA
WHERE A = 'Foo'
AND B = 'Bar'

and you briefly want to ignore the first filter "A = 'Foo'" for testing purposes. You would end up with

SELECT
A
, B
, C
FROM TabA
--WHERE A = 'Foo'
WHERE B = 'Bar'

Notice that the 'WHERE' line was commented out and therefore the next 'AND' needs to turn in a 'WHERE' to allow the syntax to remain correct.

As a result a number of developers will always make the WHERE clause 'WHERE 1=1' to avoid ever needing to comment it out and therefore never needing to change the next 'AND' to 'WHERE'.