In a Database Management System (DBMS), tables may contain a large amount of data. When retrieving or modifying data, users often need only specific records instead of the entire table. The WHERE clause is used to filter records based on a specified condition.
The WHERE clause is commonly used with SQL commands such as SELECT, UPDATE, and DELETE to work with particular rows in a table.
The WHERE clause is used in SQL to specify conditions for retrieving, updating, or deleting specific records from a table.
Only the rows that satisfy the given condition are affected by the query.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Where:
Suppose we have a table named students.
| id | name | age | course |
|---|---|---|---|
| 1 | Rahul | 20 | BCA |
| 2 | Anita | 21 | BBA |
| 3 | Rohit | 19 | BCA |
| 4 | Priya | 22 | BCom |
SELECT * FROM students
WHERE course = 'BCA';
| id | name | age | course |
|---|---|---|---|
| 1 | Rahul | 20 | BCA |
| 3 | Rohit | 19 | BCA |
Only students enrolled in BCA are displayed.
SELECT * FROM students
WHERE age > 20;
| id | name | age | course |
|---|---|---|---|
| 2 | Anita | 21 | BBA |
| 4 | Priya | 22 | BCom |
| Operator | Meaning |
|---|---|
| = | Equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| <> or != | Not equal to |
Example:
SELECT * FROM students
WHERE age >= 21;
The WHERE clause can also use logical operators to combine conditions.
| Operator | Description |
|---|---|
| AND | Both conditions must be true |
| OR | At least one condition must be true |
| NOT | Reverses the condition |
SELECT * FROM students
WHERE course = 'BCA' AND age > 19;
SELECT * FROM students
WHERE course = 'BCA' OR course = 'BBA';
UPDATE students
SET course = 'BCA'
WHERE id = 2;
Only the record with id = 2 will be updated.
DELETE FROM students
WHERE id = 3;
This will delete the record where id = 3.
WHERE clause is used to filter records based on conditions.The WHERE clause is an essential part of SQL that allows users to retrieve, update, or delete specific records based on conditions. By filtering data according to requirements, it makes database operations more precise and efficient.