What is the WHERE Clause ?
Answer : The WHERE Clause is used to filter records. It is used to extract only those records that fulfill a specified conditions.
Syntax
SELECT column_name FROM table_name WHERE conditions;
Example
Suppose we have an Employees table:
| Employee_ID | Employee_Name | Salary |
|---|---|---|
| 101 | Anjali | 50000 |
| 102 | Priya | 45000 |
| 103 | Amit | 55000 |
If we want to find employees whose salary is greater than 50000:
SELECT *
FROM Employees
WHERE Salary > 50000;
Operators Used with the WHERE Clause
1. Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | Salary = 50000 |
<> or != | Not equal to | Salary <> 50000 |
> | Greater than | Salary > 50000 |
< | Less than | Salary < 50000 |
>= | Greater than or equal to | Salary >= 50000 |
<= | Less than or equal to | Salary <= 50000 |
2. Logical Operators
| Operator | Meaning | Example |
|---|---|---|
AND | Both conditions must be true | Salary > 40000 AND Salary < 60000 |
OR | At least one condition must be true | Salary > 50000 OR Employee_ID = 101 |
NOT | Reverses a condition | NOT Salary > 50000 |
3. Other Common Operators
| Operator | Purpose | Example |
|---|---|---|
BETWEEN | Checks a range | Salary BETWEEN 40000 AND 60000 |
IN | Matches multiple values | Employee_ID IN (101, 102) |
LIKE | Searches for a pattern | Employee_Name LIKE 'R%' |
IS NULL | Checks for NULL values | Salary IS NULL |
IS NOT NULL | Checks for non-NULL values | Salary IS NOT NULL |
To fetch the record of the employee whose name is “Priya”:
SELECT *
FROM Employees
WHERE Employee_Name = ‘Priya’;