DEV Community

Cover image for The Use of Condition '1=1' in SQL Database
Vieri Lusen
Vieri Lusen

Posted on

The Use of Condition '1=1' in SQL Database

Have you ever seen or come across an SQL query that includes the condition '1=1'?

In essence, '1=1' is a condition that always evaluates to true. When used in a query, this condition does not have any effect on the query results. However, can we actually utilize and benefit from '1=1' in the implementation of writing SQL queries?

Although it may seem trivial and the use of '1=1' is very simple, it can be highly useful. One of its applications is in creating dynamic queries.

Simply put, a dynamic query is a query that can change based on needs or input provided.


The challenge in creating dynamic queries in some SQL databases is when we need to start the conditional statement using the WHERE clause, which can be cumbersome for creating dynamic queries.

Therefore, when creating dynamic queries, we often make use of the condition '1=1' at the beginning of the query's conditional statement (WHERE). This allows us to flexibly build condition criteria based on user input more easily, as we only need to add additional conditions (AND).

Here's a brief example in PHP:

//Assume dynamic filters from user input
$inputFilterName = $request->input('name');
$inputFilterAddress = $request->input('address');

//String Query
$sql = "SELECT * FROM users WHERE 1=1";

//Dynamic condition for name
if($inputFilterName !== null){
 $sql .= "AND name = '$inputFilterName'";

//Dynamic condition for address
if($inputFilterAddress !== null){
 $sql .= "AND address = '$inputFilterAddress'";

//Exec query
mysqli_query($koneksi, $sql);
Enter fullscreen mode Exit fullscreen mode

With the above condition, we have created a dynamically generated query based on different inputs and requirements. The use of '1=1' in the example above is very helpful, as it simplifies the process of creating dynamic queries.

Note: The code and query above are just examples to illustrate the use of '1=1'. In practice, the above query example is not optimal and additional mechanisms are needed to prevent the risk of SQL injection attacks.


In this article, we have explored the use of the '1=1' condition in SQL queries and how it can be beneficial in creating dynamic queries. Although it may appear simple, '1=1' provides flexibility in constructing query conditions based on user input more easily.

This article is translated using AI from the original article written in Indonesian language : Medium

Top comments (0)