Skip to content

Beginner: asking questions · SELECT: filtering and sorting · topic 7 of 62 · 30 min

WHERE: comparison and logic

Explanation

WHERE keeps only the rows for which a condition is true. Conditions use the comparison operators =, <> (or !=), <, >, <= and >=, and are combined with AND, OR and NOT. AND is worked out before OR, so put brackets around OR conditions, or you get a classic bug.

Why WHERE?

Most questions are about some rows, not all: products under ₹500, orders not yet delivered, employees in Engineering. WHERE is the filter. The database checks the condition for each row and keeps only the rows where it is true.

Think of it like this: WHERE is like the security guard at a hostel gate with a rule: "only residents with an ID card". Every person (row) is checked against the rule. Those who pass go in (into the result); the rest are turned back.

Comparison operators

OperatorMeaningExample
=Equal tostatus = 'Delivered'
<> or !=Not equal tostatus <> 'Delivered'
< >Less than, greater thanprice < 500
<= >=Less or equal, greater or equalhire_date >= '2020-01-01'
sql
SELECT name, price FROM products WHERE price < 500;
-- Toor Dal 1kg     | 160
-- Steel Tiffin Box | 450

SQL uses a single = for comparing, not == as in Python or JavaScript. <> is the standard way to write "not equal"; != also works in SQLite, PostgreSQL, MySQL and SQL Server.

Text and dates are compared too. Text compares letter by letter and is case-sensitive, so 'pune' is not equal to 'Pune'. Dates stored as 'YYYY-MM-DD' text compare correctly, so hire_date >= '2020-01-01' means "hired in 2020 or later".

AND, OR and NOT

  • AND: both conditions must be true. dept_id = 1 AND salary > 100000 gives Engineering staff earning above one lakh.
  • OR: at least one condition must be true. stock < 20 OR price > 2000.
  • NOT: flips a condition. NOT stock > 10 is the same as stock <= 10.
sql
SELECT name, salary FROM employees WHERE dept_id = 1 AND salary > 100000;
-- Meera Iyer   | 250000
-- Rahul Sharma | 120000

Precedence: the classic bug

Precedence is the order in which parts of an expression are worked out. Just as multiplication comes before addition in maths, NOT comes first, then AND, then OR. That surprises people. The manager asks for "Kitchen or Fashion products under ₹1000", and someone writes:

sql
SELECT name, category, price FROM products
WHERE category = 'Kitchen' OR category = 'Fashion' AND price < 1000;
-- Steel Tiffin Box   | Kitchen | 450
-- Pressure Cooker 3L | Kitchen | 1450   <- wrong! costs more than 1000
-- Cotton Kurta       | Fashion | 899
AND binds first, so this means: Kitchen (any price) OR (Fashion under 1000).
sql
SELECT name, category, price FROM products
WHERE (category = 'Kitchen' OR category = 'Fashion') AND price < 1000;
-- Steel Tiffin Box | Kitchen | 450
-- Cotton Kurta     | Fashion | 899
Brackets make the OR happen first. Now the price rule applies to both categories.
Tip: Whenever a WHERE mixes AND and OR, put brackets around each OR group, even if you think you know the precedence. The next person to read the query will thank you.

NULL does not pass a comparison

Deepak Yadav has no department (dept_id is NULL). WHERE dept_id <> 1 does NOT return him, because comparing NULL with anything gives "unknown", and WHERE keeps only true. The next topic explains this and shows IS NULL.

Common mistakes

  • Mixing AND and OR without brackets. Fix: bracket every OR group.
  • Writing == instead of =. SQLite accepts ==, but most databases do not; use =.
  • Quoting numbers or not quoting text: write price < 500 and city = 'Pune', not price < '500' or city = Pune.
  • Comparing text with the wrong case. Store values in a consistent case, or compare with LOWER(), covered in module 3.
  • Writing dates as '01-01-2020'. Compare in the stored YYYY-MM-DD form.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

WHERE: comparison and logic · 1/7

WHERE: filtering rows

Most questions are about some rows, not all. WHERE is the filter that keeps only the rows we want.

Prefer a video by a teacher? Find free videos on “sql where clause and or not operator precedence brackets” on YouTube ↗

In real life: Filtering orders for the delivery team

Each morning a delivery hub needs the orders that are paid, not yet delivered, and in its pin codes. That is one WHERE with AND and OR, and a missing bracket would send riders to the wrong addresses. Teams often keep such filters in a saved query that everyone reuses, so the logic is written and checked once.

sql
SELECT id, customer_id, status
FROM orders
WHERE (status = 'Placed' OR status = 'Shipped')
  AND order_date >= '2025-03-01';
Returns orders 105 (Shipped), 107 and 108 (Placed).

Practice (0/4 done)

Task 1 · Code · SQL

From products, return name and price of the products that cost less than 500.

Task 2 · Code · SQL

From employees (COMPANY), return name and salary of people in department 1 who earn more than 100000.

Task 3 · Code · SQL

The manager wants Kitchen or Fashion products that cost less than 1000. Return name, category and price. Watch the brackets.

Task 4 · Code · SQL

Which orders are not delivered yet? From orders, return id and status of every order whose status is not 'Delivered'.

Quick quiz

Question 1

1.WHERE a = 1 OR b = 2 AND c = 3 is read by SQL as...

Question 2

2.Which operator means "not equal" in standard SQL?

Question 3

3.Which WHERE finds employees hired in 2020 or later (hire_date stored as YYYY-MM-DD text)?

Question 4

4.Deepak has dept_id NULL. Does WHERE dept_id <> 1 return him?

Question 5

5.NOT stock > 10 is the same as...

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

WHERE a = 1 OR b = 2 AND c = 3 is read by SQL as...

Answer: a = 1 OR (b = 2 AND c = 3)

AND is worked out before OR.

Which operator means "not equal" in standard SQL?

Answer: <>

The standard form is <>. Most databases also accept !=.

Which WHERE finds employees hired in 2020 or later (hire_date stored as YYYY-MM-DD text)?

Answer: hire_date >= '2020-01-01'

Compare against a full date in the same YYYY-MM-DD form.

Deepak has dept_id NULL. Does WHERE dept_id <> 1 return him?

Answer: No, because the comparison gives unknown, and WHERE keeps only true

Any comparison with NULL gives unknown; use IS NULL to find such rows.

NOT stock > 10 is the same as...

Answer: stock <= 10

The opposite of "greater than 10" is "10 or less".

Deepak's dept_id is NULL. Which WHERE returns him together with everyone outside departments 1 and 2?Related

Answer: dept_id NOT IN (1, 2) OR dept_id IS NULL

NOT IN never matches a NULL value, so add OR dept_id IS NULL.

From IN, BETWEEN, LIKE and NULL

hasTicket is true and hasId is false. What is hasTicket && hasId?Related

Answer: false

&& needs both to be true.

From Operators and conditions

string? name = null; Is if (name != null && name.Length > 2) safe?Related

Answer: Yes, because && stops after the first false part

&& short-circuits: when name is null, name.Length is never evaluated.

From Decisions: if, else, switch and switch expressions

Integer.MAX_VALUE + 1 gives…Related

Answer: Integer.MIN_VALUE (-2147483648)

int overflow wraps around silently.

From Operators and expressions

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 3 more topics in “SELECT: filtering and sorting” before its exam: IN, BETWEEN, LIKE and NULL, ORDER BY, LIMIT and OFFSET, DISTINCT and how a query runs.
  2. Module 2 exam: SELECT, filtering and sorting · 15 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Functions, CASE and NULL. 62 topics left in the course before the final exam.

← Previous: SELECT, columns, aliases and expressions