đ The WHERE clause is used to filter records in an SQL query. It returns only the rows that satisfy a specified condition, allowing you to retrieve exactly the data you need instead of all records in a table. The WHERE clause is commonly used with SELECT, UPDATE, DELETE, and other SQL statements.
đ What is the WHERE Clause?
The WHERE clause specifies one or more conditions that rows must meet before they are included in the result. If a row satisfies the condition, it is returned; otherwise, it is ignored.
Information
đ Basic Syntax
General Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;đ Sample Table
Assume the following Students table:
| StudentID | Name | Department | Marks | City |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | Chennai |
| 102 | Bob | Mathematics | 85 | Coimbatore |
| 103 | Charlie | Physics | 88 | Madurai |
| 104 | David | Computer Science | 95 | Chennai |
đ¯ Filtering Records
Retrieve students who belong to the Computer Science department.
Filter by Department
SELECT *
FROM Students
WHERE Department = 'Computer Science';âī¸ Using Comparison Operators
The WHERE clause commonly uses comparison operators to compare values.
| Operator | Description | Example |
|---|---|---|
| = | Equal to | Marks = 90 |
| <> or != | Not equal to | Department <> 'Physics' |
| > | Greater than | Marks > 80 |
| < | Less than | Marks < 50 |
| >= | Greater than or equal to | Marks >= 90 |
| <= | Less than or equal to | Marks <= 60 |
Using a Comparison Operator
SELECT Name, Marks
FROM Students
WHERE Marks >= 90;đ Using Logical Operators
Logical operators allow you to combine multiple conditions.
1ī¸âŖ AND
Using AND
SELECT *
FROM Students
WHERE Department = 'Computer Science'
AND Marks > 90;2ī¸âŖ OR
Using OR
SELECT *
FROM Students
WHERE Department = 'Physics'
OR Department = 'Mathematics';3ī¸âŖ NOT
Using NOT
SELECT *
FROM Students
WHERE NOT City = 'Chennai';đ Using Special Operators
1ī¸âŖ BETWEEN
Retrieves values within a specified range (inclusive).
Using BETWEEN
SELECT *
FROM Students
WHERE Marks BETWEEN 85 AND 95;2ī¸âŖ IN
Checks whether a value matches any value in a list.
Using IN
SELECT *
FROM Students
WHERE Department IN ('Computer Science', 'Physics');3ī¸âŖ LIKE
Searches for values using wildcard patterns.
Using LIKE
SELECT *
FROM Students
WHERE Name LIKE 'A%';Tip
4ī¸âŖ IS NULL
Checks whether a column contains a NULL value.
Using IS NULL
SELECT *
FROM Students
WHERE City IS NULL;âī¸ WHERE with UPDATE
The WHERE clause ensures that only the intended rows are updated.
Update Specific Records
UPDATE Students
SET Department = 'Information Technology'
WHERE StudentID = 101;đī¸ WHERE with DELETE
The WHERE clause ensures that only matching rows are deleted.
Delete Specific Records
DELETE FROM Students
WHERE StudentID = 104;Warning
đ Common WHERE Operators
| Operator | Purpose |
|---|---|
| = | Checks equality. |
| <> / != | Checks inequality. |
| >, <, >=, <= | Compare numeric or date values. |
| BETWEEN | Checks whether a value falls within a range. |
| IN | Matches one of several values. |
| LIKE | Performs pattern matching. |
| IS NULL | Checks for missing values. |
đŧ Real-World Example
Imagine a school management system where an administrator wants to display only students who scored more than 90 marks in the Computer Science department.
Real-World Query
SELECT StudentID,
Name,
Marks
FROM Students
WHERE Department = 'Computer Science'
AND Marks > 90
ORDER BY Marks DESC;â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ WHERE filters rows based on specified conditions.
- đ It can be used with SELECT, UPDATE, and DELETE.
- đ Comparison, logical, and special operators make filtering more powerful.
- đ Use IS NULL to check for missing values.
- đ Omitting WHERE in UPDATE or DELETE affects all rows.