MySQL AND Operator are one of the Logical Operators which is generally used in the WHERE Clause to apply multiple filters on the records returned by SELECT Statement. It returns the result as:
- 1, if all operands are non-zeros and not nulls.
- 0, if one of the operands is zero.
- NULL, If one of the operands is NULL, and the remaining operands are non-zero.
Let us see how to use the Logical AND Operator in the WHERE Clause to filter the data. And to explain this, we are going to use the below shown data.
MySQL AND Operator Command prompt
In this example, we pass Ones, Zeros, and Null values with a different combination. It helps you to understand the Truth table behind the AND Operator.
SELECT 0 AND 0; SELECT 1 AND 0; SELECT 1 AND 1; SELECT 1 AND NULL; SELECT 0 AND NULL; SELECT NULL AND NULL;
AND Operator Example
Here, AND test multiple conditions in the WHERE Clause. If all the conditions are TRUE, then only it displays the records.
USE company; SELECT CustID, First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers WHERE Profession LIKE '%Developer' AND Yearly_Income > 75000;
The above SELECT Statement retrieves all the Customers present in the MySQL Customers table whose Profession contains Developer, and Yearly Income is greater than 75000 (WHERE Clause).
You can also use this one in between three or 4 conditions. Let us apply three conditions using MySQL AND Operator
USE company; SELECT CustID, First_Name, Last_Name, Education, Profession, Yearly_Income, Sales FROM customers WHERE Profession LIKE '%Developer' AND Yearly_Income > 75000 AND Sales > 10000;
The above statement returns all the Customers available in the Customers table whose Profession contains Developer, Yearly Income is greater than 75000, and Sales is greater than 10000.