🔍 WHERE Clause in SQL
Last Updated: 7th January 2026
The WHERE clause is used to filter rows in a query. It ensures that only required records are returned based on a condition.
- WHERE applies conditions to data
- It filters rows, not columns
- It works with comparison and logical operators
Hinglish Tip 🗣: WHERE ka kaam hai — kaunsa data chahiye aur kaunsa nahi, ye decide karna.
🧾 Basic Syntax of WHERE
SELECT column_name
FROM table_name
WHERE condition;
- condition decides which rows are selected
- Rows that do not satisfy the condition are excluded
⚖️ Comparison Operators in WHERE
Common operators used with WHERE:
- = equal to
- != or <> not equal to
- > greater than
- < less than
- >= greater than or equal to
- <= less than or equal to
SELECT name
FROM students
WHERE age >= 18;
Logical Operators in WHERE
WHERE can combine multiple conditions.
- AND → all conditions must be true
- OR → at least one condition must be true
- NOT → negates a condition
SELECT *
FROM students
WHERE marks > 70 AND age >= 18;
WHERE with NULL Values
- IS NULL → checks if a column has a NULL value
- IS NOT NULL → checks if a column does not have a NULL value
SELECT *
FROM students
WHERE age IS NULL;
SELECT *
FROM students
WHERE age IS NOT NULL;
🧪 WHERE with BETWEEN, IN, LIKE Operators
- IN and NOT IN operators
- Let say WHERE marks = 30 OR marks = 50 OR marks = 70
- Use When There is multiple condition join using OR operator
- e.g. WHERE marks IN (30, 50, 70)
- e.g. WHERE marks NOT IN (30, 50, 70)
- BETWEEN operator
- Let say WHERE marks >= 60 AND marks <= 80
- Use When There is a range of condition join using AND operator
- e.g. WHERE marks BETWEEN 60 AND 80
- LIKE And NOT LIKE operator
- It is used to match a pattern in a column
- %char end with given characters
- e.g. WHERE name LIKE '%A'
- char% start with given characters
- e.g. WHERE name LIKE 'A%'
- %char% contains given characters
- e.g. WHERE name LIKE '%A%'
- _char Two characters long and end with given characters
- e.g. WHERE name LIKE '_A'
Note: We will cover Regular Expressions (regex) in the next section.
Subqueries in WHERE
Syntax:
SELECT column_name
FROM table_name
WHERE column_name IN (SELECT column_name FROM table_name);
Example:
SELECT name
FROM students
WHERE age > (SELECT age FROM students WHERE name LIKE "A%");
