MySQL AND, OR and NOT Operators

In MySQL, the AND, OR, and NOT operators are used in the WHERE clause to combine multiple conditions and create complex queries.

AND operator

The AND operator is used to specify that all conditions must be true for a row to be included in the result set. For example, the following query selects all the employees whose age is less than 30 and whose salary is greater than 50000:

SELECT * FROM employees WHERE age < 30 AND salary > 50000;

OR operator

The OR operator is used to specify that at least one of the conditions must be true for a row to be included in the result set. For example, the following query selects all the employees whose age is less than 30 or whose salary is greater than 50000:

SELECT * FROM employees WHERE age < 30 OR salary > 50000;

NOT operator

The NOT operator is used to negate a condition. It returns all rows that do not meet the specified condition. For example, the following query selects all the employees whose age is not less than 30:

SELECT * FROM employees WHERE NOT age < 30;

It is also possible to use parentheses to group conditions and control the order of evaluation. For example, the following query selects all the employees whose age is less than 30 and whose salary is greater than 50000, or whose name starts with “J”:

SELECT * FROM employees WHERE (age < 30 AND salary > 50000) OR name LIKE 'J%';

These operators are powerful tools that allow you to create complex queries that can filter and manipulate data in many different ways.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial