MySQL MariaDB

How to Put Two Conditions in WHERE Clause in MySQL

MySQL has the ability to store massive amounts of data in the form of tables related to one another. The data from these tables can be extracted by using the “select” statement with “where” and “from” clauses. More specifically, the “where” clause defines a condition for data extraction. However, if the user wants to include multiple conditions in the same query, then in that case, the user has to use the logical operator in the where clause.

This post will teach about:

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:

mysql -u <username> -p

After the successful login, change the database in which you want to work using the syntax:

use <database-name>;

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:

select * from [table-name] where [condition];

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:

select * from customer where id <= 10;

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:

select * from [table] where [condition1] or [condition2];

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:

select * from customer where id <= 6 or id >= 83;

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”:

select * from customer where id <= 10 or country = '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:

select * from [table] where [condition1] and [condition2];

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”:

select * from product where id <= 25 and unitprice >= 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”:

select * from product where id <= 10 and ProductName = '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:

select * from product where id <= 10 and (ProductName = 'ikura' or ProductName = 'chai');

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.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.