-
Hajipur, Bihar, 844101
In MySQL, the AND, OR, and NOT operators are logical operators used to combine or negate conditions in SQL queries. They are typically used with the WHERE clause to filter data based on multiple criteria.
Understanding these operators is crucial because in real-world databases, you rarely filter data using a single condition. Often, you need to specify complex rules — for example, selecting students who are older than 18 and live in a specific city, or students who are not enrolled in a particular course.
The AND operator requires both conditions to be true for a record to be selected.
Syntax:
SELECT columns
FROM table_name
WHERE condition1 AND condition2;
Example:
SELECT * FROM students
WHERE age > 18 AND city = 'Mumbai';
This query fetches students who are older than 18 and live in Mumbai.
If either condition is false, the record is excluded.
The OR operator requires at least one condition to be true.
Syntax:
SELECT columns
FROM table_name
WHERE condition1 OR condition2;
Example:
SELECT * FROM students
WHERE city = 'Delhi' OR city = 'Pune';
This retrieves students who live in Delhi or Pune.
Records meeting either condition are included.
The NOT operator negates a condition, returning records that do not satisfy the condition.
Syntax:
SELECT columns
FROM table_name
WHERE NOT condition;
Example:
SELECT * FROM students
WHERE NOT city = 'Mumbai';
This fetches students who do not live in Mumbai.
You can combine these logical operators to create more complex conditions.
Example:
SELECT * FROM students
WHERE age > 18 AND (city = 'Mumbai' OR city = 'Delhi');
Parentheses () are used to group conditions.
Without parentheses, MySQL evaluates AND before OR, which can lead to unexpected results.
Here, students must be older than 18 and live in either Mumbai or Delhi.
In MySQL, logical operators have a priority:
NOT – highest precedence
AND – medium precedence
OR – lowest precedence
This means:
SELECT * FROM students
WHERE age > 18 OR city = 'Mumbai' AND name LIKE 'A%';
MySQL evaluates city = 'Mumbai' AND name LIKE 'A%' first, then applies the OR with age > 18.
To avoid confusion, use parentheses:
SELECT * FROM students
WHERE (age > 18 OR city = 'Mumbai') AND name LIKE 'A%';
Logical operators are often combined with comparison operators (=, >, <, BETWEEN, IN) for precise filtering.
Example 1:
SELECT * FROM students
WHERE age BETWEEN 18 AND 22 AND city = 'Mumbai';
Example 2:
SELECT * FROM students
WHERE city IN ('Delhi', 'Pune') OR age > 20;
Example 3:
SELECT * FROM students
WHERE NOT (city = 'Mumbai' OR city = 'Delhi');
This fetches students who live in cities other than Mumbai or Delhi.
Consider a table students:
| id | name | age | city |
|---|---|---|---|
| 1 | Aditi | 19 | Mumbai |
| 2 | Meera | 20 | Delhi |
| 3 | Pooja | 18 | Pune |
| 4 | Riya | 21 | Mumbai |
| 5 | Ananya | 22 | Delhi |
Example 1 – AND operator:
SELECT * FROM students
WHERE age > 18 AND city = 'Mumbai';
Result: Aditi and Riya
Example 2 – OR operator:
SELECT * FROM students
WHERE city = 'Pune' OR age = 22;
Result: Pooja and Ananya
Example 3 – NOT operator:
SELECT * FROM students
WHERE NOT city = 'Delhi';
Result: Aditi, Pooja, Riya
Example 4 – Combined operators:
SELECT * FROM students
WHERE age > 18 AND (city = 'Mumbai' OR city = 'Delhi');
Result: Aditi, Meera, Riya, Ananya
Use parentheses to clarify the order of operations.
Avoid overly complex queries without breaking them into smaller parts.
Combine AND and OR carefully; remember operator precedence.
Test NOT conditions separately to ensure they produce expected results.
Use indexes on columns used in WHERE conditions with logical operators to improve performance.
Student Filtering:
Select students older than 18 and enrolled in a particular course:
SELECT * FROM students
WHERE age > 18 AND course_id = 1;
City-Based Queries:
Find students from multiple cities:
SELECT * FROM students
WHERE city = 'Mumbai' OR city = 'Delhi';
Excluding Records:
Fetch students who are not from a specific city:
SELECT * FROM students
WHERE NOT city = 'Pune';
Combined Criteria:
Retrieve students aged between 18 and 22 and not living in Mumbai:
SELECT * FROM students
WHERE age BETWEEN 18 AND 22 AND NOT city = 'Mumbai';
AND, OR, NOT are logical operators used with the WHERE clause.
AND requires all conditions to be true, OR requires at least one condition, and NOT negates a condition.
Operator precedence matters: NOT > AND > OR.
Parentheses can be used to clarify evaluation order.
Logical operators can be combined with comparison operators, IN, BETWEEN, and LIKE for precise filtering.
Using these operators effectively allows you to handle complex queries and extract exactly the data you need from your MySQL database.
Retrieve all students whose age is greater than 18 and live in Mumbai.
Select students who live in Delhi or Pune.
List students who are not from Mumbai.
Fetch students whose age is between 18 and 22 and city is either Delhi or Mumbai.
Retrieve students whose names start with ‘A’ and live in Pune.
Find students who are either older than 20 or live in Delhi.
Select students who are not older than 18.
Fetch students whose city is not Delhi and age is greater than 19.
List students who are younger than 20 or whose names end with ‘a’.
Retrieve students whose age is greater than 18 and (city is Mumbai or name starts with ‘R’).