SQL WHERE Clause
The WHERE clause is used to filter records from a table. Instead of returning every row, WHERE allows specifying a condition, and only rows that meet that condition are returned.
Think of WHERE like a security checkpoint — only records that pass the condition are allowed through to the result.
The Reference Table
All examples in this topic use the following Students table:
| StudentID | StudentName | Age | City | JoinDate |
|---|---|---|---|---|
| 1 | Ravi Sharma | 20 | Delhi | 2024-01-10 |
| 2 | Priya Mehta | 22 | Mumbai | 2024-02-05 |
| 3 | Arjun Nair | 19 | Chennai | 2024-02-20 |
| 4 | Sneha Kapoor | 21 | Pune | 2024-03-01 |
| 5 | Rohit Das | 23 | Kolkata | 2024-03-15 |
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;Filtering by a Text (String) Value
Text values in a WHERE condition must be enclosed in single quotes.
SELECT * FROM Students
WHERE City = 'Mumbai';Result:
| StudentID | StudentName | Age | City | JoinDate |
|---|---|---|---|---|
| 2 | Priya Mehta | 22 | Mumbai | 2024-02-05 |
Only the row where City equals 'Mumbai' is returned.
Filtering by a Numeric Value
Numeric values do not need quotes in the condition.
SELECT * FROM Students
WHERE Age = 21;Result:
| StudentID | StudentName | Age | City | JoinDate |
|---|---|---|---|---|
| 4 | Sneha Kapoor | 21 | Pune | 2024-03-01 |
Comparison Operators in WHERE
The WHERE clause supports a variety of operators to build conditions:
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | WHERE Age = 20 |
!= or <> | Not equal to | WHERE City != 'Delhi' |
> | Greater than | WHERE Age > 20 |
< | Less than | WHERE Age < 22 |
>= | Greater than or equal to | WHERE Age >= 21 |
<= | Less than or equal to | WHERE Age <= 21 |
Example: Students Older Than 20
SELECT StudentName, Age FROM Students
WHERE Age > 20;Result:
| StudentName | Age |
|---|---|
| Priya Mehta | 22 |
| Sneha Kapoor | 21 |
| Rohit Das | 23 |
Example: Students Not From Delhi
SELECT StudentName, City FROM Students
WHERE City != 'Delhi';Result:
| StudentName | City |
|---|---|
| Priya Mehta | Mumbai |
| Arjun Nair | Chennai |
| Sneha Kapoor | Pune |
| Rohit Das | Kolkata |
Using WHERE With UPDATE
The WHERE clause is also critical when using UPDATE or DELETE. Without it, all rows would be affected.
UPDATE Students
SET City = 'Bangalore'
WHERE StudentID = 3;This updates the City of only the student with StudentID = 3. Without the WHERE clause, all students' cities would be changed to 'Bangalore'.
Using WHERE With DELETE
DELETE FROM Students
WHERE StudentID = 5;This deletes only the row where StudentID equals 5.
Filtering by Date
SELECT * FROM Students
WHERE JoinDate > '2024-02-01';Result: All students who joined after February 1st, 2024.
| StudentID | StudentName | Age | City | JoinDate |
|---|---|---|---|---|
| 2 | Priya Mehta | 22 | Mumbai | 2024-02-05 |
| 3 | Arjun Nair | 19 | Chennai | 2024-02-20 |
| 4 | Sneha Kapoor | 21 | Pune | 2024-03-01 |
| 5 | Rohit Das | 23 | Kolkata | 2024-03-15 |
Common Mistakes With WHERE
| Mistake | Issue | Correction |
|---|---|---|
WHERE City = Mumbai | Text not in quotes | WHERE City = 'Mumbai' |
WHERE Age = '20' | Number in quotes (may work but not best practice) | WHERE Age = 20 |
| Omitting WHERE in UPDATE/DELETE | All rows will be changed or deleted | Always use WHERE with UPDATE and DELETE |
Key Points to Remember
- The
WHEREclause filters rows — only rows matching the condition are included in the result. - Text and date values must be wrapped in single quotes in the condition.
- Numeric values are written without quotes.
WHEREcan be used withSELECT,UPDATE, andDELETE.- Always use
WHEREwithUPDATEandDELETEto avoid accidentally changing or removing all records.
Summary
The WHERE clause is a powerful filter that controls which rows are returned or affected by a SQL statement. It accepts a wide range of comparison operators and can be applied to text, numeric, and date values. Proper use of WHERE is essential for writing accurate and safe SQL queries.
