WHERE, AND, OR & NOT
WHERE keeps only rows that match a condition. Without WHERE, SELECT returns every row in the table.
WHERE — filter rows
WHERE keeps only rows that match a condition. Without WHERE, SELECT returns every row in the table.
Compare columns to values with =, <>, >, >=, <, <=. Use single quotes for text.
Real-life example: WHERE is the bouncer at a concert — only people with a valid ticket (condition) get in.
SELECT full_name, city
FROM students
WHERE grade_level = 10;
SELECT full_name, grade_level
FROM students
WHERE city = 'Delhi';AND — both conditions must be true
AND combines conditions. Every condition joined with AND must pass for the row to appear.
Real-life example: AND is like needing both a library card AND a student ID to borrow a textbook — missing either means no book.
SELECT full_name, city, grade_level
FROM students
WHERE grade_level = 10 AND city = 'Delhi';OR — at least one condition must be true
OR keeps rows where any listed condition is true. Use parentheses when mixing AND and OR so the database understands your intent.
Real-life example: OR is like a sale open to seniors OR honor-roll students — either badge gets you the discount.
SELECT full_name, city
FROM students
WHERE city = 'Delhi' OR city = 'Mumbai';
-- Mix AND / OR safely with parentheses
SELECT full_name, city, grade_level
FROM students
WHERE (city = 'Delhi' OR city = 'Mumbai') AND grade_level >= 10;NOT — reverse a condition
NOT flips a condition. WHERE NOT city = 'Delhi' means every city except Delhi.
You can write WHERE city <> 'Delhi' or WHERE city != 'Delhi' for the same idea on many databases.
Real-life example: NOT is like 'everyone except staff' on a guest list — you define who is excluded.
SELECT full_name, city
FROM students
WHERE NOT city = 'Delhi';
-- Same result, different style
SELECT full_name, city
FROM students
WHERE city <> 'Delhi';