MySQL AND, OR and NOT Operators

MySQL operators are used to compare values, perform arithmetic operations, and make logical decisions. In this article, we’ll cover AND, OR, and NOT operators with examples.

AND Operator

The AND operator returns true if all the conditions separated by AND are true.

Syntax: SELECT column1, column2, … FROM table_name WHERE condition1 AND condition2 AND condition3 …;

Example: SELECT * FROM users WHERE first_name=’John’ AND last_name=’Doe’;

This SQL statement selects all the records from the users table where the first name is John and the last name is Doe.

OR Operator

The OR operator returns true if any of the conditions separated by OR is true.

Syntax: SELECT column1, column2, … FROM table_name WHERE condition1 OR condition2 OR condition3 …;

Example: SELECT * FROM users WHERE first_name=’John’ OR last_name=’Doe’;

This SQL statement selects all the records from the users table where the first name is John or the last name is Doe.

NOT Operator

The NOT operator negates a condition.

Syntax: SELECT column1, column2, … FROM table_name WHERE NOT condition;

Example: SELECT * FROM users WHERE NOT first_name=’John’;

This SQL statement selects all the records from the users table where the first name is not John.

In conclusion, MySQL AND, OR, and NOT operators are used to combine conditions in a SQL statement. The AND operator returns true if all conditions are true, the OR operator returns true if any condition is true, and the NOT operator negates a condition.