MySQL AND operator
In MySQL, the
AND operator is a logical operator used to combine multiple conditions in a query’s
WHERE clause. It allows you to retrieve rows from a table that satisfy all specified conditions.
The syntax for using the
AND operator in a MySQL query is as follows:
SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND ... AND conditionN;
condition3, and so on, represent the individual conditions that need to be met for a row to be included in the result set. You can use various comparison operators (e.g.,
<>) and logical operators (e.g.,
NOT) to construct these conditions.
Let’s consider an example to illustrate the usage of the
AND operator. Suppose we have a table named
employees with columns
salary. We want to retrieve all employees who have a salary greater than 5000 and are from the ‘Sales’ department. We can write the following query:
SELECT employee_id, first_name, last_name, salary FROM employees WHERE salary > 5000 AND department = 'Sales';