WHERE
A SELECT without a filter returns every row of the table. That is fine for a table of twelve products, but a real shop can have lakhs of orders. You almost always want only some of them: today's orders, customers from Pune, products under ₹1,000.
The WHERE clause does that filtering. In this lesson you will compare numbers, text and dates, learn every comparison operator, and see the quoting mistakes that trip up most beginners.
Filtering rows with WHERE
WHERE comes after FROM and holds a condition: a test that is either true or false for each row. MySQL checks the condition row by row and keeps only the rows where it is true.
SELECT first_name, last_name, city
FROM customers
WHERE city = 'Rohtak';first_name | last_name | city -----------+-----------+------- Aarav | Sharma | Rohtak Vikram | Yadav | Rohtak (2 rows)
Two of the twelve customers live in Rohtak. Notice the single quotes around 'Rohtak'. Text values (called strings) always go inside single quotes; numbers do not.
Comparison operators
SQL has six comparison operators:
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | stock = 0 |
<> | Not equal to (!= also works in MySQL) | status <> 'delivered' |
< | Less than | price < 1000 |
> | Greater than | salary > 100000 |
<= | Less than or equal to | quantity <= 2 |
>= | Greater than or equal to | price >= 5000 |
Note that SQL uses a single = for "equal to". There is no == as in Python or JavaScript.
SELECT product_name, price
FROM products
WHERE price >= 5000;product_name | price ----------------------------+------ 27-inch Monitor | 15999 Noise-Cancelling Headphones | 8999 1TB External SSD | 6499 (3 rows)
The Graphics Tablet costs ₹4,599, so it is just below the line and does not appear. With >=, a product priced exactly ₹5,000 would be included; with >, it would not.
Here is "not equal to" in action. The support team wants every order that has not been delivered yet, or never will be:
SELECT order_id, customer_id, status
FROM orders
WHERE status <> 'delivered';order_id | customer_id | status ---------+-------------+---------- 105 | 4 | cancelled 108 | 7 | shipped 111 | 9 | pending 113 | 3 | shipped 115 | 1 | pending (5 rows)
Filtering by date
In MySQL a date is written as a string in the form 'YYYY-MM-DD': year, month, day. Because the biggest unit comes first, dates sort and compare correctly, and you can use the same operators as for numbers.
SELECT order_id, order_date, status
FROM orders
WHERE order_date >= '2025-04-01';order_id | order_date | status ---------+------------+---------- 110 | 2025-04-01 | delivered 111 | 2025-04-10 | pending 112 | 2025-04-18 | delivered 113 | 2025-05-02 | shipped 114 | 2025-05-11 | delivered 115 | 2025-05-20 | pending (6 rows)
These are all the orders placed on or after 1 April 2025. Always use this year-first format. A date such as '01-04-2025' is not understood as a date and will not compare the way you expect.
Conditions can use calculations
The left and right sides of a comparison can be any expression, not just a column name. This query finds products where the stock is worth more than ₹1,50,000:
SELECT product_name, price, stock
FROM products
WHERE price * stock > 150000;product_name | price | stock ----------------------------+-------+------ Mechanical Keyboard | 3499 | 45 27-inch Monitor | 15999 | 18 Noise-Cancelling Headphones | 8999 | 25 1TB External SSD | 6499 | 30 (4 rows)
MySQL works out price * stock for every row, then compares it with 150000. The calculated value does not have to appear in the SELECT list.
Common mistakes
- Forgetting the quotes around text.
WHERE city = Rohtakmakes MySQL look for a column called Rohtak:ERROR 1054 (42S22): Unknown column 'Rohtak' in 'where clause'. Put text in single quotes. - Using double quotes for text. MySQL often accepts
"Rohtak", but in standard SQL double quotes mean a column or table name, and some MySQL settings follow that rule. Single quotes always work. - Writing
==. MySQL does not know==and reports a syntax error. Use a single=. - A name with an apostrophe. To search for
D'Souza, write the apostrophe twice:WHERE last_name = 'D''Souza'. A single apostrophe ends the string early and breaks the query. In application code, use prepared statements, which handle this for you. - Using
= NULL.WHERE email = NULLnever finds anything. Missing values needIS NULL, explained in NULL Values.
Key points
WHEREkeeps only the rows for which its condition is true.- The operators are
=,<>(or!=),<,>,<=and>=. - Put text and dates in single quotes; leave numbers unquoted.
- Write dates as
'YYYY-MM-DD'so they compare correctly. - A condition can contain calculations such as
price * stock. - To combine several conditions, use
AND,ORandNOT, covered in AND, OR & NOT.
Exercise
Write three separate queries. First, list the product_name and price of every product in the Storage category. Second, list the first_name and salary of employees who earn less than ₹50,000 a month. Third, list the orders placed before 1 February 2025.
Show solution
SELECT product_name, price FROM products WHERE category = 'Storage';
SELECT first_name, salary FROM employees WHERE salary < 50000;
SELECT order_id, order_date FROM orders WHERE order_date < '2025-02-01';The first query returns the 1TB External SSD and the 64GB Pen Drive. The second returns Rahul, Nisha and Tanvi. The third returns orders 101, 102 and 103, all placed in January 2025.
Quick Quiz
1. Which operator means "not equal to" in standard SQL?
<> is the standard "not equal" operator; MySQL also accepts !=.
2. Which condition is written correctly?
Text goes in single quotes and SQL uses a single = for comparison.
3. How should you write 5 March 2025 in a MySQL condition?
MySQL dates use the year-month-day format, which also compares in the right order.
4. What does WHERE price * stock > 150000 do?
Conditions can use any expression; MySQL works it out for each row before comparing.
Learn SQL with a mentor
Free lessons get you started. Our PHP & MySQL Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
