Beginner: asking questions · SELECT: filtering and sorting · topic 7 of 62 · 30 min
WHERE: comparison and logic
Explanation
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.
Comparison operators
| Operator | Meaning | Example |
|---|---|---|
| = | Equal to | status = 'Delivered' |
| <> or != | Not equal to | status <> 'Delivered' |
| < > | Less than, greater than | price < 500 |
| <= >= | Less or equal, greater or equal | hire_date >= '2020-01-01' |
SELECT name, price FROM products WHERE price < 500;
-- Toor Dal 1kg | 160
-- Steel Tiffin Box | 450SQL 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.
SELECT name, salary FROM employees WHERE dept_id = 1 AND salary > 100000;
-- Meera Iyer | 250000
-- Rahul Sharma | 120000Precedence: 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:
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 | 899SELECT name, category, price FROM products
WHERE (category = 'Kitchen' OR category = 'Fashion') AND price < 1000;
-- Steel Tiffin Box | Kitchen | 450
-- Cotton Kurta | Fashion | 899NULL 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
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.
SELECT id, customer_id, status
FROM orders
WHERE (status = 'Placed' OR status = 'Shipped')
AND order_date >= '2025-03-01';Practice (0/4 done)
From products, return name and price of the products that cost less than 500.
From employees (COMPANY), return name and salary of people in department 1 who earn more than 100000.
The manager wants Kitchen or Fashion products that cost less than 1000. Return name, category and price. Watch the brackets.
Which orders are not delivered yet? From orders, return id and status of every order whose status is not 'Delivered'.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- IN, BETWEEN, LIKE and NULLSELECT: filtering and sorting · same module
- ORDER BY, LIMIT and OFFSETSELECT: filtering and sorting · same module
- Operators and conditionsFrontend Web Development · JavaScript basics
- Decisions: if, else, switch and switch expressionsC# and .NET · Decisions, loops and methods
- Operators and expressionsJava Programming (Core to Spring Boot) · Getting started with Java
- Decisions with if and switchGo Programming (backend and cloud) · Control flow and functions
Questions people ask about this
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.
hasTicket is true and hasId is false. What is hasTicket && hasId?Related
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.
Integer.MAX_VALUE + 1 gives…Related
Answer: Integer.MIN_VALUE (-2147483648)
int overflow wraps around silently.
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
- 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.
- Module 2 exam: SELECT, filtering and sorting · 15 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Functions, CASE and NULL. 62 topics left in the course before the final exam.