DataBolt

SQL LEARNING PLATFORM

3

CHAPTER 3

AND, OR, NOT

Combining multiple conditions into one filter.

5

Learning Blocks

SQL

Interactive Queries

In This Chapter

Concepts you'll master

1

AND: both conditions must be true

2

OR: at least one condition must be true

3

NOT: invert a condition

4

Parentheses to control evaluation order

SQL QUERY
SELECT name, department, salary 
FROM employees
WHERE department = 'IT' AND salary > 70000;

SQL QUERY
SELECT name, department 
FROM employees
WHERE department = 'IT' OR department = 'HR';

5.3 - NOT

Everyone except Salses

SQL QUERY
SELECT name, department 
FROM employees
WHERE NOT department = 'Sales';

5.4 - Parentheses - Always Be Explicit

❌ Common Mistake: AND has higher precedence than OR, just like multiplication vs addition in maths. WHERE a OR b AND c is evaluated as WHERE a OR (b AND c), which is almost never what you intended. Always use parentheses

SQL QUERY
SELECT name, department, salary 
FROM employees
WHERE department = 'Sales'
      OR (department = 'IT' AND salary > 80000);

Exercise

Practice your SQL skills

SQL EDITOR
Press semicolon (;) to auto-run query

Tasks

Complete all SQL challenges

1

Find employees who are in IT and have salary greater than 70000.

Current Task
2

Find employees who are in HR or Sales.

3

Find employees who have salary less than 60000 or work in IT.

4

Find employees who are not in Sales department.

5

Find employees who do have a manager (i.e., manager_id is NOT NULL)

6

Find employees who: are in IT or HR, and salary greater than 60000, and not in Sales

Need help solving this task?