Lesson 10
Beginner Level
10 of 30 lessons
Logical Operators
Learn about AND, OR, NOT operators for combining conditions in SQL queries. Logical operators are essential for creating complex filtering conditions.
What are Logical Operators?
Logical operators in SQL are used to combine multiple conditions in a WHERE clause. They help build complex filter logic using AND, OR, and NOT.
Types of Logical Operators
| Operator | Description | Usage Example |
|---|---|---|
| AND | Returns true if both conditions are true | salary > 3000 AND department_id = 10 |
| OR | Returns true if at least one condition is true | salary < 3000 OR department_id = 20 |
| NOT | Negates a condition | NOT job_id = 'IT_PROG' |
Example Queries
-- Employees in department 90 with salary over 5000
SELECT first_name, salary, department_id
FROM employees
WHERE department_id = 90 AND salary > 5000;
-- Employees in department 10 or 20
SELECT first_name, department_id
FROM employees
WHERE department_id = 10 OR department_id = 20;
-- Employees not in job IT_PROG
SELECT first_name, job_id
FROM employees
WHERE NOT job_id = 'IT_PROG';
Practice Task
- Write a query to find employees in department 60 and hired after 2020.