Logical Operators in SQL
? Logical Operators in SQL
Logical operators in SQL are used to combine or evaluate multiple conditions in WHERE, HAVING, and JOIN clauses.
? List of Logical Operators
| Operator | Description | Example |
|---|---|---|
AND | True if both conditions are true | age > 18 AND gender = 'M' |
OR | True if any one of the conditions is true | age < 18 OR age > 65 |
NOT | Reverses the result (true becomes false) | NOT (status = 'active') |
BETWEEN | True if value is between a range | salary BETWEEN 30000 AND 50000 |
IN | True if value is in a list | city IN ('Delhi', 'Mumbai') |
LIKE | True if pattern matches string | name LIKE 'A%' |
IS NULL | Checks if a value is NULL | email IS NULL |
EXISTS | True if a subquery returns any rows | WHERE EXISTS (SELECT 1...) |
? Example with AND, OR, NOT:
SELECT * FROM employeesWHERE (department = 'Sales' OR department = 'Marketing') AND salary > 30000 AND NOT is_terminated;? Use Cases:
Combine multiple filters
Conditional logic in
CASESubquery validations (
EXISTS,IN)
Let me know if you want practice questions or real-world query examples!