MySQL AND Operator
The AND
operator in MySQL is used to combine two or more conditions in a WHERE
clause. It returns TRUE
only when all the conditions are TRUE
.
Syntax
1. Using AND with Multiple Conditions
Example
Retrieve employees who are in the "IT" department and earn a salary greater than 50,000.
Result
2. Combining AND with Other Operators
You can combine the AND
operator with comparison operators such as =
, >
, <
, >=
, <=
, and <>
(not equal).
Example
Find products with a price greater than 100 and less than or equal to 500:
3. Using AND with Logical Operators
You can use AND
in conjunction with other logical operators like OR
and NOT
.
Example
Get customers from "New York" who have made purchases over 500 or are VIP members:
4. AND with Multiple Conditions on the Same Column
You can use AND
to specify multiple conditions for the same column.
Example
Get employees with salaries between 40,000 and 60,000:
5. Practical Example
Sample Table: orders
Query: Get orders where the amount is greater than 500 and the product is not "Laptop":
Result:
6. Common Errors
Operator Precedence: MySQL evaluates
AND
beforeOR
. Use parentheses to clarify precedence.Case Sensitivity in String Comparisons: MySQL string comparisons are case-insensitive unless configured otherwise.
7. Best Practices
Use Parentheses:
When combiningAND
withOR
, use parentheses to make conditions explicit and avoid confusion.Optimize with Indexes:
Ensure columns used inAND
conditions are indexed to improve query performance.
Let me know if you need further examples or clarifications!