This post will teach about:
- The “where” clause in “select” Statement
- The “where” Clause with One Condition
- The “where” Clause with Two Conditions Using “or” Operator
- The “where” Clause with Two Conditions Using “and” Operator
- The “where” Clause with a Complex Two Conditions Using Logical Operators
Prerequisite: Login MySQL Server
To begin with this post, MySQL should be installed on your system. Open the command prompt to log in to your Local MySQL server using the syntax, and make sure to provide your username and run it:
After the successful login, change the database in which you want to work using the syntax:
The success message will display:
“where” clause in “select” Statement
The “where” clause is utilized to define a condition. The “select” statement with the “where” clause filters the records from the table that fulfill the defined condition. This condition can be simple and complex.
“where” Clause with One Condition
The syntax of the “where” clause with single/simple condition in the “select” statement is given below:
Let’s see an example to extract the records from the “customer” table if the “id” is less than or equal to “10”, run this command:
The output will display the records that match the defined condition in the “customer” table:
You have successfully used one condition to extract the result. Now let’s see the “where” clause to define two conditions for filtering records that fulfill both conditions.
“where” Clause with Two Conditions Using “or” Operator
The “or” operator can be used between two conditions in the “where” clause to return records if any condition is “TRUE”. The syntax of the “where” clause with two conditions separated using the “or” operator is:
Let’s see an example of the “or” operator to filter the records from the table “customer” if any of two or both conditions are fulfilled. The first condition is that “id” is less than or equal to “6”, whereas the second condition is “id” greater than and equal to “83”. The command for this example is provided below:
The output displays records that are obtained from the command:
Let’s see another example to filter the data from the table “customer” if the “id” is greater than and equal to “10” “OR” the “country” is equal to “UK”:
In the output, you can see that results are obtained that fulfill any one or both conditions:
“where” Clause with Two Conditions Using “and” Operator
The “and” operator can be used between two conditions in the “where” clause to return records if both the conditions are “TRUE”. The syntax of the “where” clause with two conditions separated using the “and” operator is:
Let’s see an example of the “and” operator in the “where” clause. Use the given below command to filter the records from the “product” table. When the “id” is less than and equal to “25” and the “unitprice” is greater than and equal to “20”:
The output displays records that fulfill both conditions:
Let’s see another example to filter the data from the table “product” if the “id” is less than and equal to “10” “and” the “ProductName” is equal to “ikura”:
The output is showing the extracted records that match both conditions:
“where” Clause with a Complex Two Conditions Using Logical Operators
The “where” clause can create complex conditions using logical operators in conditions. If the user wants to filter the data from one table but with one simple and one complex condition using logical operators in between them is possible.
Let’s see an example to extract data from the “product” table if the “id” is less than and equal to “10” “and” the second condition. The second condition provides two conditions if the “ProductName” = “chai” “or” “ProductName” = “ikura.” The command for this example is:
The output filtered the records for when both conditions are “TRUE”:
You have learned how to put two conditions in the “where” clause in MySQL.
Conclusion
The “or” and “and” logical operators can be used to put two conditions in the “where” clause. The “or” operator displays records that match any of the conditions, whereas the “and” operator returns records that match both conditions. This post demonstrated the syntax and examples for putting two conditions in the “where” clause in MySQL.