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:

StudentIDStudentNameAgeCityJoinDate
1Ravi Sharma20Delhi2024-01-10
2Priya Mehta22Mumbai2024-02-05
3Arjun Nair19Chennai2024-02-20
4Sneha Kapoor21Pune2024-03-01
5Rohit Das23Kolkata2024-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:

StudentIDStudentNameAgeCityJoinDate
2Priya Mehta22Mumbai2024-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:

StudentIDStudentNameAgeCityJoinDate
4Sneha Kapoor21Pune2024-03-01

Comparison Operators in WHERE

The WHERE clause supports a variety of operators to build conditions:

OperatorMeaningExample
=Equal toWHERE Age = 20
!= or <>Not equal toWHERE City != 'Delhi'
>Greater thanWHERE Age > 20
<Less thanWHERE Age < 22
>=Greater than or equal toWHERE Age >= 21
<=Less than or equal toWHERE Age <= 21

Example: Students Older Than 20

SELECT StudentName, Age FROM Students
WHERE Age > 20;

Result:

StudentNameAge
Priya Mehta22
Sneha Kapoor21
Rohit Das23

Example: Students Not From Delhi

SELECT StudentName, City FROM Students
WHERE City != 'Delhi';

Result:

StudentNameCity
Priya MehtaMumbai
Arjun NairChennai
Sneha KapoorPune
Rohit DasKolkata

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.

StudentIDStudentNameAgeCityJoinDate
2Priya Mehta22Mumbai2024-02-05
3Arjun Nair19Chennai2024-02-20
4Sneha Kapoor21Pune2024-03-01
5Rohit Das23Kolkata2024-03-15

Common Mistakes With WHERE

MistakeIssueCorrection
WHERE City = MumbaiText not in quotesWHERE City = 'Mumbai'
WHERE Age = '20'Number in quotes (may work but not best practice)WHERE Age = 20
Omitting WHERE in UPDATE/DELETEAll rows will be changed or deletedAlways use WHERE with UPDATE and DELETE

Key Points to Remember

  • The WHERE clause 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.
  • WHERE can be used with SELECT, UPDATE, and DELETE.
  • Always use WHERE with UPDATE and DELETE to 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.

Leave a Comment

Your email address will not be published. Required fields are marked *