Summary: in this tutorial, you will learn how to the MySQL
AND operator to combine multiple Boolean expressions to filter data.
Introduction to MySQL AND operator
AND operator is a logical operator that combines two or more Boolean expressions and returns true only if both expressions evaluate to true. The
AND operator returns false if one of the two expressions evaluate to false.
WHERE boolean_expression_1 AND boolean_expression_2
The following illustrates the results of the
AND operator when combining true, false, and null.
AND operator is often used in the
WHERE clause of the
DELETE statement to form Boolean expressions. The
AND operator is also used in join conditions of the
INNER JOIN or
LEFT JOIN clause.
When evaluating an expression that has the
AND operator, MySQL evaluates the remaining parts of the expression until it can determine the result. This function is called short-circuit evaluation. See the following example.
SELECT 1 = 0 AND 1 / 0 ;
1 = 0 AND 1 / 0
Note that in MySQL, 0 is considered false, and non-zero is treated as true.
MySQL only evaluates the first part
1 = 0 of the expression
1 = 0 AND 1 / 0 because the expression
1 = 0 returns false so MySQL concludes that the result of the whole expression is false. MySQL does not evaluate the remaining part of the expression, which is 1/0; If it does, it will issue an error message because of the division by zero error.
MySQL AND operator examples
Let’s use the
customers table in the sample database for the demonstration.
The following statement selects customers who locate in
USA country and
CA state. We use the
AND operator in the
country = 'USA' AND state = 'CA';
With the AND operator, you can combine more than two Boolean expressions. For example, the following query returns the customers who locate in California, USA, and has credit limit greater than 100K.
WHERE country = 'USA'
AND state = 'CA'
AND creditlimit > 100000;
In this tutorial, we have shown you how to use the MySQL
AND operator to combine two or more expressions to form a complex predicate for the