Skip to content
Laviyx Learn

Try

SQL course menu

SQL AND, OR, NOT: Combine Conditions Correctly

Learn how to combine conditions in SQL with AND, OR and NOT, why AND runs before OR, how parentheses change results, and how NOT works with IN, BETWEEN, LIKE and NULL.

Beginner7 min readUpdated
On this page

Real questions usually have more than one condition: engineers earning over 80,000, employees in Mumbai or Delhi, everyone except Sales. AND, OR and NOT let you combine conditions in a WHERE clause.

The example table#

idnamedepartmentsalarycity
1AshaSales52000Mumbai
2RaviEngineering78000Bengaluru
3MeeraEngineering81000Pune
4JohnMarketing46000Mumbai
5PriyaSales61000Delhi

AND: every condition must be true#

SQLRun
SELECT name, department, salary
FROM employees
WHERE department = 'Engineering' AND salary > 80000;
Output
name  | department  | salary
------+-------------+-------
Meera | Engineering | 81000

Ravi is in Engineering but earns 78,000, so the second condition fails and he is left out. Each AND makes the result smaller or equal.

OR: at least one condition must be true#

SQLRun
SELECT name, city
FROM employees
WHERE city = 'Mumbai' OR city = 'Delhi';
Output
name  | city
------+-------
Asha  | Mumbai
John  | Mumbai
Priya | Delhi

Each OR makes the result larger or equal.

NOT: reverse a condition#

SQLRun
SELECT name, department
FROM employees
WHERE NOT department = 'Sales';
Output
name  | department
------+------------
Ravi  | Engineering
Meera | Engineering
John  | Marketing

NOT department = 'Sales' means the same as department <> 'Sales'. NOT is most useful with the other operators:

With NOTMeaning
NOT IN ('Mumbai', 'Pune')not any of these values
NOT BETWEEN 50000 AND 70000outside the range
NOT LIKE 'A%'does not start with A
IS NOT NULLhas a value

Truth tables#

ABA AND BA OR B
truetruetruetrue
truefalsefalsetrue
falsetruefalsetrue
falsefalsefalsefalse

AND runs before OR#

This is the most important rule in this lesson. Just as multiplication happens before addition in maths, AND is evaluated before OR. Look at this query:

SQLRun
SELECT name, department, city
FROM employees
WHERE department = 'Sales' OR department = 'Marketing' AND city = 'Mumbai';
Output
name  | department | city
------+------------+-------
Asha  | Sales      | Mumbai
John  | Marketing  | Mumbai
Priya | Sales      | Delhi

You might have meant "Sales or Marketing people in Mumbai", but Priya from Delhi is included. SQL read it as:

SQL
WHERE department = 'Sales'
   OR (department = 'Marketing' AND city = 'Mumbai')

Add parentheses to say what you mean:

SQLRun
SELECT name, department, city
FROM employees
WHERE (department = 'Sales' OR department = 'Marketing')
  AND city = 'Mumbai';
Output
name | department | city
-----+------------+-------
Asha | Sales      | Mumbai
John | Marketing  | Mumbai

The full order of evaluation is: comparisons (=, >, …) first, then NOT, then AND, then OR.

Combining everything#

Employees outside Mumbai and Pune who earn at least 60,000:

SQLRun
SELECT name, city, salary
FROM employees
WHERE city NOT IN ('Mumbai', 'Pune')
  AND salary >= 60000;
Output
name  | city      | salary
------+-----------+-------
Ravi  | Bengaluru | 78000
Priya | Delhi     | 61000

A note on NULL#

Conditions involving NULL are neither true nor false; they are unknown, and WHERE keeps only rows where the condition is true. That means NOT does not always return "everything else". This is explained in detail in NULL Values.

Common mistakes#

Practice#

Practice exercise

Return the name, department and salary of employees who are not in Marketing and whose salary is either below 55,000 or above 80,000.

Show hint

You need <> (or NOT) for the department, and an OR for the salary, wrapped in parentheses.

Show solution
SQLRun
SELECT name, department, salary
FROM employees
WHERE department <> 'Marketing'
  AND (salary < 55000 OR salary > 80000);
Output
name  | department  | salary
------+-------------+-------
Asha  | Sales       | 52000
Meera | Engineering | 81000

Without the parentheses, John (Marketing, 46,000) would also appear, because salary < 55000 alone would be enough for him.

Key takeaway#

Finished this lesson?