WHERE Clause
WHERE filters rows so only those matching your condition are returned. It works with SELECT, UPDATE, and DELETE — making it the most critical clause for controlling which data you read or modify. Always use WHERE with UPDATE and DELETE.
WHERE Operators
- = Equal to: WHERE status = 'active'
- != or <> Not equal: WHERE category != 'Electronics'
- < > <= >= Comparisons: WHERE price > 500
- AND — both conditions true: WHERE price > 100 AND stock > 0
- OR — at least one true: WHERE status = 'pending' OR status = 'shipped'
- NOT — reverses a condition: WHERE NOT status = 'cancelled'
- Precedence: NOT > AND > OR — use parentheses to be explicit
WHERE Examples on ecommerce_db
-- Filter products by price
SELECT name, price FROM products WHERE price > 500;
-- Result: Laptop (999.99), Phone (599.99)
-- Filter orders by status
SELECT id, total_amount, status FROM orders WHERE status = 'delivered';
-- Combine conditions
SELECT name, price FROM products
WHERE price < 1000 AND stock > 10;
-- OR condition
SELECT * FROM orders
WHERE status = 'pending' OR status = 'shipped';
-- NOT equal
SELECT name FROM products WHERE name != 'Laptop';
-- WHERE in UPDATE — ALWAYS use WHERE!
UPDATE products SET stock = stock - 1 WHERE id = 1;
-- Without WHERE: updates EVERY product's stock!
-- WHERE in DELETE — ALWAYS use WHERE!
DELETE FROM orders WHERE id = 3;
-- Without WHERE: deletes ALL orders!Quick Quiz
Tip
Tip
Practice WHERE Clause in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Relationships define how tables connect through foreign keys
Common Mistake
Warning
A common mistake with WHERE Clause is skipping edge case testing — empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready sql code.
Practice Task
Note
Practice Task — (1) Write a working example of WHERE Clause from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Key Takeaways
- WHERE filters rows so only those matching your condition are returned.
- = Equal to: WHERE status = 'active'
- != or <> Not equal: WHERE category != 'Electronics'
- < > <= >= Comparisons: WHERE price > 500