Menu & Search
Why use WHERE 1=1 in SQL Queries

Why use WHERE 1=1 in SQL Queries

A common SQL question is, why does anyone use WHERE 1=1 in their queries? And what does it do?

The condition means WHERE TRUE, so it’s just bringing back the same query result as it would without. Also, there’s no impact on query execution time.

This is really something you’d add into a query for convenience. When adding in conditions to the query all of them will contain AND so it’s easier when commenting out conditions :

SELECT *
FROM TABLE
WHERE 1=1
--  and column1='blah'
    and column2='more_blah'
--  and column3 is not null

This is similar to another technique where you’d have commas before column names rather than after. Again, it’s easier for commenting :

SELECT
     Column1
--  ,Column2
    ,Column3
    ,Column4
--  ,Column5
FROM TABLE

Share

0 Comments