Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why would you use WHERE 1=0 statement in SQL?

I saw a query run in a log file on an application. and it contained a query like:

SELECT ID FROM CUST_ATTR49 WHERE 1=0 

what is the use of such a query that is bound to return nothing?

like image 624
MozenRath Avatar asked Feb 04 '12 11:02

MozenRath


People also ask

What does WHERE 1 0 mean in SQL?

A query like this can be used to ping the database. The clause: WHERE 1=0. Ensures that non data is sent back, so no CPU charge, no Network traffic or other resource consumption. A query like that can test for: server availability.

What is the purpose of WHERE in SQL?

The WHERE clause is used to filter records. It is used to extract only those records that fulfill a specified condition.

What does WHERE 1/2 mean in SQL?

The reason you put the WHERE 1=2 clause in that SELECT INTO query is to create a field-copy of the existing table with no data. If you did this: select * into Table2 from Table1. Table2 would be an exact duplicate of Table1 , including the data rows.

What is the meaning of WHERE 1 1 in SQL query?

If you have worked with SQL databases before, you might have come across the statement WHERE 1=1. It is a common statement that is used to return all the records from a given table. The statement where 1=1 in SQL means true. It is the same operation as running the select statement without the where clause.


2 Answers

This may be also used to extract the table schema from a table without extracting any data inside that table. As Andrea Colleoni said those will be the other benefits of using this.

like image 25
Darshana Avatar answered Sep 18 '22 16:09

Darshana


A query like this can be used to ping the database. The clause:

WHERE 1=0 

Ensures that non data is sent back, so no CPU charge, no Network traffic or other resource consumption.

A query like that can test for:

  • server availability
  • CUST_ATTR49 table existence
  • ID column existence
  • Keeping a connection alive
  • Cause a trigger to fire without changing any rows (with the where clause, but not in a select query)
  • manage many OR conditions in dynamic queries (e.g WHERE 1=0 OR <condition>)
like image 122
Andrea Colleoni Avatar answered Sep 21 '22 16:09

Andrea Colleoni