12

I have seen in some programmers use 1=0 in where condition for select query , why they use this condition.

XMen
  • 29,384
  • 41
  • 99
  • 151
  • Possible duplicate of [why would you use WHERE 1=0 statement in SQL?](https://stackoverflow.com/questions/9140606/why-would-you-use-where-1-0-statement-in-sql) – YoYo Aug 20 '18 at 15:50

5 Answers5

16

in sql server, you can quickly create a copy of a table without any data like this

select * into Newtable
from Oldtable
where 1 = 0

this will create a new table with the same structure as the old table

Another option is to return an empty resultset

SQLMenace
  • 132,095
  • 25
  • 206
  • 225
  • Even simpler with same effect: `select * into Newtable from Oldtable where 0` – Jpsy Oct 26 '12 at 10:27
  • I want to confirm that this is the same what is done using Oracle as well, not just limited to SQL Server. – YoYo Aug 20 '18 at 15:04
  • SELECT TOP 0 * into newtable from oldtable will give the same results without the confusing math. – discosammy Nov 21 '19 at 21:06
8

The only possible use for that would be to prevent the query from returning any rows. I've done it occasionally for testing "no results found" type logic.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
5

I have seen some utilities use that syntax to gather metadata about a table. They may run a query such as select * from sometable where 1=0 to "efficiently" get the columns from the table without any data.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
  • Wouldn't work in MySQL, I think. That's what `SHOW COLUMNS` is for. – ceejayoz Dec 18 '10 at 17:12
  • @ceejayoz: Thanks for the information. I did not know that about MySQL; I assumed (apparently incorrectly) that it would return an empty result set, which would still have the columns. It does work in other SQL implementations (a few at least). – Mark Wilkins Dec 18 '10 at 22:20
0

I've used this in code when generating the SQL based on a checkbox list of values. For example, if I allow the user to pick from a list of fruit:

Apples Oranges Pears

...and they select Apples and Oranges, my pseudo code looks like:

string sqlWhere = "select * from fruit where 1 = 0"
if (FruitList.Any()) {
   sqlWhere += " or code in ("
   foreach (Fruit in FruitList)
      sqlWhere += string.Format("'{0}',");
   /*Add the close parens*/
   sqlWhere = sqlWhere.Substring(0,sqlWhere.Length-2) + ")";
}

which will only return the values the user selected. If they don't select any fruit, then no results are returned.

*Please note this code does not account for SQL Injection. Just an example of why you might see the

where 1 = 0
Nick DeVore
  • 9,748
  • 3
  • 39
  • 41
0

You could use a query like this as a 'ping' to check if your connection to the database is still open and the database is healthy and so on - if you get no results, it is, but if you get some sort of error, there's a problem. You'd write the query against some sort of system table. Depending on your database, you might be able to do something simpler - in PostgreSQL, you can just say select 1;, and in Oracle, i've seen select 1 from dual;. If your database doesn't have the ability to handle free-floating expressions in selects, and has no equivalent of dual, you could do something like select * from sometable where 0 = 1;.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133