ย MySQL WHERE Clause
The WHERE clause in MySQL 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
1๏ธโฃ Selecting rows based on a number condition:
Result:
| name |
salary |
| Priya |
50000 |
| Neha |
55000 |
2๏ธโฃ Filter using a text value:
3๏ธโฃ 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.