MySQL WHERE Clause

MySQL Tutorial

 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:

idnamedepartmentsalary
1RajHR30000
2PriyaIT50000
3AmanSales40000
4NehaIT55000

 Example Queries

 Selecting rows based on a number condition:

Result:

namesalary
Priya50000
Neha55000

 Filter using a text value:


 Using NOT operator:


 


Comparison Operators used with WHERE

OperatorMeaningExample
=Equalsalary = 30000
<> or !=Not equalsalary <> 40000
>Greater thansalary > 50000
<Less thansalary < 25000
>=Greater than or equalsalary >= 30000
<=Less than or equalsalary <= 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)

PatternMeaning
%Zero or more characters
_One character

Examples:


 Using WHERE with ORDER BY & LIMIT


Summary

FeatureExample
Filter numeric valuesWHERE salary > 40000
Filter textWHERE department = 'IT'
Use multiple conditionsAND, OR, NOT
Range checkBETWEEN 30000 AND 60000
Match specific valuesIN ('IT', 'Sales')
Pattern searchLIKE '%abc%'

 The WHERE clause helps retrieve exactly the data you need — not the entire table.

You may also like...