MySQL WHERE Clause

MySQL WHERE Clause
In MySQL WHERE Clause is used to filter records from a table based on a specific condition. It helps retrieve only those rows that match the criteria defined in the query.
Basic Syntax
Example Table
Assume we have a table named employees:
| id | name | department | salary |
|---|---|---|---|
| 1 | Raj | HR | 30000 |
| 2 | Priya | IT | 50000 |
| 3 | Aman | Sales | 40000 |
| 4 | Neha | IT | 55000 |
Example Queries
Selecting rows based on a number condition:
Result:
| name | salary |
|---|---|
| Priya | 50000 |
| Neha | 55000 |
Filter using a text value:
Using NOT operator:
Comparison Operators used with WHERE
| Operator | Meaning | Example |
|---|---|---|
| = | Equal | salary = 30000 |
| <> or != | Not equal | salary <> 40000 |
| > | Greater than | salary > 50000 |
| < | Less than | salary < 25000 |
| >= | Greater than or equal | salary >= 30000 |
| <= | Less than or equal | salary <= 45000 |
Logical Operators
AND — Both conditions must be true
OR — At least one condition must be true
BETWEEN — Range condition
IN — Match multiple values
LIKE — Pattern matching (used for text)
| Pattern | Meaning |
|---|---|
% | Zero or more characters |
_ | One character |
Examples:
Using WHERE with ORDER BY & LIMIT
Summary
| Feature | Example |
|---|---|
| Filter numeric values | WHERE salary > 40000 |
| Filter text | WHERE department = 'IT' |
| Use multiple conditions | AND, OR, NOT |
| Range check | BETWEEN 30000 AND 60000 |
| Match specific values | IN ('IT', 'Sales') |
| Pattern search | LIKE '%abc%' |
The WHERE clause helps retrieve exactly the data you need — not the entire table.
