Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the purpose of using WHERE 1=1 in SQL statements?
    text
    copied!<blockquote> <p><strong>Possible Duplicates:</strong><br> <a href="https://stackoverflow.com/questions/517107/why-would-a-sql-query-have-where-1-1">Why would a sql query have &ldquo;where 1 = 1&rdquo;</a><br> <a href="https://stackoverflow.com/questions/242822/why-would-someone-use-where-11-and-conditions-in-a-sql-clause">Why would someone use WHERE 1=1 AND &lt;conditions&gt; in a SQL clause?</a> </p> </blockquote> <p>I've seen that a lot in different query examples and it goes to probably all SQL engines.</p> <p>If there is a query that has no conditions defined people (and specially ORM frameworks) often add always-true condition <code>WHERE 1 = 1</code> or something like that.</p> <p>So instead of</p> <pre><code>SELECT id, name FROM users; </code></pre> <p>they use</p> <pre><code>SELECT id, name FROM users WHERE 1 = 1; </code></pre> <p>The only possible reason I could think of if you are adding conditions dynamically you don't have to worry about stripping the initial <code>AND</code> but still quite often this <code>1 = 1</code> condition is stripped if there is an actual condition in the query.</p> <p>Actual example from CakePHP (generated by framework):</p> <p>(no conditions)</p> <pre><code>SELECT `User`.`id`, `User`.`login` FROM `users` AS `User` WHERE 1 = 1 ORDER BY `User`.`id` ASC; </code></pre> <p>(with condition)</p> <pre><code>SELECT `User`.`id`, `User`.`login` FROM `users` AS `User` WHERE `User`.`login` = 'me@example.com' LIMIT 1; </code></pre> <p>Is there any reason for adding that extra condition?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload