SQL AND OR NOT operators
SQL

SQL AND, OR, NOT Operators: Combine Multiple Conditions

SQL Tutorial · Chapter 6 of 32

The SQL AND, OR and NOT operators combine or reverse conditions in a WHERE clause. AND returns a row only when every condition is true, OR returns a row when at least one condition is true, and NOT flips a condition so matching rows are excluded. Together they let you express any filter, however specific.

SQL AND, OR, NOT Syntax

SELECT columns FROM table_name WHERE condition1 AND condition2;
SELECT columns FROM table_name WHERE condition1 OR condition2;
SELECT columns FROM table_name WHERE NOT condition;

You can chain as many conditions as you need and mix the three operators. When you mix AND with OR, use parentheses to state the grouping you intend.

Sample Table

Orders
OrderID CustomerID Product Amount OrderDate
101 1 Laptop 1200 2026-01-05
102 2 Mouse 25 2026-01-07
103 1 Monitor 300 2026-01-12
104 3 Keyboard 45 2026-02-02
105 6 Headset 80 2026-02-15
106 4 Laptop 1150 2026-03-01

Example 1: AND (Both Conditions Must Be True)

Orders placed by customer 1 that cost more than 500.

SELECT OrderID, Product, Amount
FROM Orders
WHERE CustomerID = 1 AND Amount > 500;
OrderID Product Amount
101 Laptop 1200

Order 103 also belongs to customer 1, but its amount of 300 fails the second condition, so it is excluded.

Example 2: OR (Either Condition Is Enough)

SELECT OrderID, Product, Amount
FROM Orders
WHERE Product = 'Mouse' OR Product = 'Keyboard';
OrderID Product Amount
102 Mouse 25
104 Keyboard 45

For long lists of alternatives the IN operator is shorter than a chain of ORs.

Example 3: NOT (Exclude Matching Rows)

SELECT OrderID, Product
FROM Orders
WHERE NOT Product = 'Laptop';
OrderID Product
102 Mouse
103 Monitor
104 Keyboard
105 Headset

This is equivalent to Product <> 'Laptop'. NOT is more useful with IN, LIKE, BETWEEN and EXISTS, as in NOT IN (...).

Example 4: Mixing AND and OR with Parentheses

Find laptop orders, plus any order over 250 placed in January. Without parentheses the meaning changes, so group explicitly.

SELECT OrderID, Product, Amount, OrderDate
FROM Orders
WHERE Product = 'Laptop'
   OR (Amount > 250 AND OrderDate < '2026-02-01');
OrderID Product Amount OrderDate
101 Laptop 1200 2026-01-05
103 Monitor 300 2026-01-12
106 Laptop 1150 2026-03-01

Operator Precedence

SQL evaluates NOT first, then AND, then OR. So A OR B AND C means A OR (B AND C), not (A OR B) AND C. Parentheses cost nothing and remove all doubt, so add them whenever both AND and OR appear.

Expression Evaluated as
NOT a AND b (NOT a) AND b
a OR b AND c a OR (b AND c)
a AND b OR c AND d (a AND b) OR (c AND d)

Works in MySQL, SQL Server, PostgreSQL, SQLite

AND, OR and NOT behave identically in all four systems. MySQL also accepts &&, || and ! as synonyms, but they are non-standard and || means string concatenation elsewhere, so avoid them.

Readable Conditions

Long WHERE clauses are easier to read when each condition sits on its own line and related conditions are wrapped in parentheses, even where precedence would make them optional. Reviewers can then check the logic at a glance, and a later edit is far less likely to change the meaning by accident.

Common Mistakes

  • Writing WHERE Product = 'Mouse' OR 'Keyboard'. Each side of OR must be a complete condition.
  • Forgetting parentheses when mixing AND and OR, silently returning the wrong rows.
  • Using AND when you mean OR: Country = 'India' AND Country = 'UK' can never be true for one row.
  • Combining NOT with NULL values; NOT (Amount > 100) does not return rows where Amount is NULL.

Practice Exercise

  1. Return orders that are not laptops and cost 50 or more.
  2. Return orders from customer 1 or customer 4 that were placed in 2026-01.
Show answers
-- 1
SELECT OrderID, Product, Amount FROM Orders
WHERE NOT Product = 'Laptop' AND Amount >= 50;
-- 103 Monitor 300, 105 Headset 80

-- 2
SELECT OrderID FROM Orders
WHERE (CustomerID = 1 OR CustomerID = 4)
  AND OrderDate BETWEEN '2026-01-01' AND '2026-01-31';
-- 101, 103

Related Chapters

FAQ

Which is evaluated first in SQL, AND or OR?

AND has higher precedence, so it is evaluated before OR. Use parentheses to force a different grouping and to make the intent obvious to readers.

How many conditions can I combine in a WHERE clause?

There is no practical limit. Very long condition lists are usually better expressed with IN, BETWEEN or a join to a lookup table.

Is NOT the same as <>?

For a single equality test they give the same result. NOT is more general: it can negate IN, LIKE, BETWEEN, EXISTS and whole bracketed expressions.

Chapter 6 of 32 · SQL from Scratch: all 32 chapters

PK
Meet PK, the founder of NeotechNavigators.com! With over 15 years of experience in Data Visualization, Excel Automation, and dashboard creation. PK is a Microsoft Certified Professional who has a passion for all things in Excel. PK loves to explore new and innovative ways to use Excel and is always eager to share his knowledge with others. With an eye for detail and a commitment to excellence, PK has become a go-to expert in the world of Excel. Whether you're looking to create stunning visualizations or streamline your workflow with automation, PK has the skills and expertise to help you succeed. Join the many satisfied clients who have benefited from PK's services and see how he can take your data analysis skills to the next level!
https://neotechnavigators.com