SQL Tutorial › Querying Data › WHERE

SQL WHERE Clause

BeginnerLesson 9 of 6910 min read8 queries run on MySQL 8.4

Most questions you ask a database are about some rows, not all of them: the orders from January, the products that are out of stock, the employees in Pune. The WHERE clause filters rows so that only the ones matching a condition reach the result.

Advertisement
In this lesson
  • Filter rows with comparison operators
  • Compare numbers, text and dates correctly
  • Use WHERE with UPDATE and DELETE safely
  • Spot the classic WHERE mistakes

WHERE syntax

MySQL
SELECT column1, column2FROM table_nameWHERE condition;

The condition is checked for every row. Rows where it is true are kept; rows where it is false or unknown (because of NULL) are dropped.

Sample data

products · 9 rows
product_idproduct_namecategorypricestock
1Wireless MouseElectronics799120
2Mechanical KeyboardElectronics349945
3USB-C HubElectronics19990
4Office ChairFurniture899912
5Standing DeskFurniture189995
6Notebook PackStationery249300
7Gel PensStationery199250
8Desk LampFurniture149930
9Monitor ArmFurniture279918

Filter on a number

Which products cost less than 1,000?

SQL
SELECT product_name, priceFROM productsWHERE price < 1000;
ResultMySQL 8.4
product_nameprice
Wireless Mouse799
Notebook Pack249
Gel Pens199
3 rows in set
How this query works, step by step
  1. FROM products reads the 9 products.
  2. WHERE price < 1000 tests each row: 799, 249 and 199 pass; the other six fail.
  3. SELECT returns the name and price of the 3 rows that passed.

Comparison operators

OperatorMeaningExample
=equalcategory = 'Furniture'
<> or !=not equalstatus <> 'Delivered'
>greater thanprice > 2000
<less thanstock < 10
>=greater than or equalsalary >= 100000
<=less than or equalquantity <= 2

Products that are out of stock:

SQL
SELECT product_id, product_name, stockFROM productsWHERE stock = 0;
ResultMySQL 8.4
product_idproduct_namestock
3USB-C Hub0
1 row in set

Orders that are not delivered yet:

SQL
SELECT order_id, customer_id, statusFROM ordersWHERE status <> 'Delivered';
ResultMySQL 8.4
order_idcustomer_idstatus
10043Cancelled
10065Shipped
10081Pending
3 rows in set
How this query works, step by step
  1. <> means “not equal to”.
  2. Seven orders are Delivered, so the other three come back: one Cancelled, one Shipped and one Pending.

Filter on text

Text values go in single quotes. With MySQL’s default collation the comparison ignores case:

SQL
SELECT first_name, last_name, cityFROM employeesWHERE city = 'Pune';
ResultMySQL 8.4
first_namelast_namecity
PriyaNairPune
KavyaIyerPune
RohanJoshiPune
3 rows in set

Filter on dates

Write dates as 'YYYY-MM-DD' text. MySQL compares them as dates, so > means “later than”:

SQL
SELECT first_name, hire_dateFROM employeesWHERE hire_date >= '2022-01-01'ORDER BY hire_date;
ResultMySQL 8.4
first_namehire_date
Neha2022-03-14
Karan2022-08-29
Arjun2023-06-05
Rohan2024-01-22
Amit2025-06-02
5 rows in set
How this query works, step by step
  1. hire_date >= '2022-01-01' keeps people hired on or after 1 January 2022.
  2. Five employees joined in 2022 or later.
  3. ORDER BY hire_date lists them from the earliest to the latest hire.

Filter on a calculated value

The condition can use expressions. Which order lines are worth more than 5,000?

MySQL
SELECT oi.order_id, p.product_name, oi.quantity, p.price,       oi.quantity * p.price AS line_totalFROM order_items oiJOIN products p ON p.product_id = oi.product_idWHERE oi.quantity * p.price > 5000;
ResultMySQL 8.4
order_idproduct_namequantitypriceline_total
1002Office Chair189998999
1008Office Chair2899917998
1004Standing Desk11899918999
3 rows in set
How this query works, step by step
  1. The JOIN pairs each order line with its product so the price is available (INNER JOIN).
  2. WHERE calculates quantity * price for each line and keeps the lines above 5,000.
  3. The alias line_total cannot be used in WHERE (it is created later, in SELECT), so the expression is repeated.

WHERE with UPDATE and DELETE

WHERE decides which rows an UPDATE or DELETE touches. Without it, every row changes:

SQL
UPDATE productsSET stock = 50WHERE product_id = 3; SELECT product_id, product_name, stock FROM products WHERE product_id = 3;
ResultMySQL 8.4
mysql> UPDATE products
Query OK, 1 row affected
mysql> SELECT product_id, product_name, stock FROM products WHERE product_id …
product_idproduct_namestock
3USB-C Hub50
1 row in set
Watch out

Before running an UPDATE or DELETE, run a SELECT with the same WHERE and check that it returns exactly the rows you mean to change.

Common WHERE mistakes

  • Comparing with NULL using =. WHERE commission = NULL never matches. Use IS NULL (NULL Values).
  • Forgetting quotes around text. WHERE city = Pune makes MySQL look for a column called Pune.
  • Quoting numbers. WHERE price < '1000' works, but MySQL has to convert the text first; keep numbers unquoted.
  • Using a SELECT alias in WHERE. WHERE line_total > 5000 fails, because WHERE runs before SELECT.
MySQL
SELECT first_name FROM employees WHERE city = Pune;
ResultMySQL 8.4
ERROR 1054 (42S22): Unknown column 'Pune' in 'where clause'

Predict the result

How many rows does this return?

SELECT first_name, salaryFROM employeesWHERE salary <> 72000;

Exercise: Recent orders

Show order_id, customer_id and order_date for orders placed on or after 1 April 2025.

Expected resultMySQL 8.4
order_idcustomer_idorder_date
100722025-04-10
100812025-04-28
100932025-05-15
101052025-06-01
4 rows in set
Hint 1

Compare order_date with the text '2025-04-01' using >=.

Exercise: Furniture under 5,000

List the product_name and price of products in the Furniture category. (Filtering on two conditions at once comes in the next lesson; here, just the category.)

Expected resultMySQL 8.4
product_nameprice
Office Chair8999
Standing Desk18999
Desk Lamp1499
Monitor Arm2799
4 rows in set
Hint 1

WHERE category = 'Furniture'

Quick quiz

1. Which operator means “not equal” in SQL?

2. How should a date be written in a MySQL WHERE clause?

3. What happens to a row when the WHERE condition is unknown because of NULL?

4. What does UPDATE products SET stock = 0; without WHERE do?

WHERE clause FAQ

What is the WHERE clause in SQL?

WHERE filters rows. It follows FROM and contains a condition; only rows for which the condition is true are returned, updated or deleted.

Can I use WHERE with GROUP BY?

Yes. WHERE filters rows before they are grouped. To filter the groups themselves, for example “departments with more than 3 employees”, use HAVING.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and can use aggregates such as COUNT(*).

Is the WHERE clause case sensitive in MySQL?

With the default utf8mb4_0900_ai_ci collation, text comparisons are case and accent insensitive, so 'pune' matches 'Pune'. Use a binary or _cs collation for case-sensitive matching.