Search code examples
sqlselect-query

SELECT * FROM tablename WHERE 1


I've been curious. What are the differences between these respective queries:

  1. SELECT * FROM `tablename`

  2. SELECT * FROM `tablename` WHERE 1

  3. SELECT * FROM `tablename` WHERE 1=1


Solution

  • 2 and 3 are the same in MySQL, functionally 1 is also the same.

    where 1 is not standard so, as others have pointed out, will not work in other dialects.

    People add where 1 or where 1 = 1 so where conditions can be easily added or removed to/from a query by adding in/commenting out some "and ..." components.

    i.e.

    SELECT * FROM `tablename` WHERE 1=1
    --AND Column1 = 'Value1'
    AND Column2 = 'Value2'