-2

Why do we write WHERE 1=0 or 1=1 in SQL query under WHERE clause?

Kumar Gaurav
  • 27
  • 1
  • 3
  • Usually it's there by code generators that need to have a WHERE clause and a base condition that's always true or false. – siride Mar 06 '17 at 04:48
  • 1
    Another question (first result from google): http://stackoverflow.com/questions/9140606/why-would-you-use-where-1-0-statement-in-sql – default locale Mar 06 '17 at 04:50
  • To avoid confusion - this 'duplicate' marking should be corrected from the `1=1` reference, to the correct `1=0` duplicate as in the previous comment. I would suggest to reopen, and then again marking it as duplicate using the correct reference. Even though the body of the question rephrases the question to both 1=0 and 1=1, the 1=0 question has better quality answers that reflect those both scenarios as well. – YoYo Aug 20 '18 at 15:53

1 Answers1

1

It's just used to make it easier to concatenate extra conditions to the WHERE clause. For example:

var sql = "SELECT * FROM Person WHERE 1=1 ";

if(something)
   sql += " AND Something = 1";

if(somethingElse)
   sql += " AND SomethingElse = 1";

This way you don't need to check if it is the first condition or not, you can always append AND at the start.

AndreFeijo
  • 10,044
  • 7
  • 34
  • 64