The WHERE
clause in MySQL is used to filter records from a table based on specified conditions. It allows you to fetch only the rows that satisfy the condition(s) provided. You can combine the WHERE
clause with various operators to refine the query further.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
condition
: Specifies the criteria to filter rows. You can use comparison, logical, or pattern-matching operators.
Operators in WHERE
Clause
1. Comparison Operators
Operator | Description | Example |
---|---|---|
= | Equal to | WHERE Country = 'USA' |
!= or <> | Not equal to | WHERE Country != 'USA' |
> | Greater than | WHERE Age > 25 |
< | Less than | WHERE Age < 18 |
>= | Greater than or equal to | WHERE Age >= 21 |
<= | Less than or equal to | WHERE Age <= 65 |
Example Table: Employees
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
1 | John | Smith | 30 | Sales | 50000 | USA |
2 | Jane | Doe | 45 | HR | 65000 | UK |
3 | Sarah | Johnson | 28 | IT | 55000 | USA |
4 | Michael | Brown | 50 | Sales | 70000 | Canada |
1. Using =
Find employees from the USA:
SELECT * FROM Employees WHERE Country = 'USA';
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
1 | John | Smith | 30 | Sales | 50000 | USA |
3 | Sarah | Johnson | 28 | IT | 55000 | USA |
2. Using !=
or <>
Find employees not from the USA:
SELECT * FROM Employees WHERE Country != 'USA';
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
2 | Jane | Doe | 45 | HR | 65000 | UK |
4 | Michael | Brown | 50 | Sales | 70000 | Canada |
3. Using >
and <
Find employees older than 40:
SELECT * FROM Employees WHERE Age > 40;
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
2 | Jane | Doe | 45 | HR | 65000 | UK |
4 | Michael | Brown | 50 | Sales | 70000 | Canada |
4. Logical Operators
AND
Find employees in the USA who are older than 25:
SELECT * FROM Employees WHERE Country = 'USA' AND Age > 25;
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
1 | John | Smith | 30 | Sales | 50000 | USA |
OR
Find employees in either Sales or HR department:
SELECT * FROM Employees WHERE Department = 'Sales' OR Department = 'HR';
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
1 | John | Smith | 30 | Sales | 50000 | USA |
2 | Jane | Doe | 45 | HR | 65000 | UK |
4 | Michael | Brown | 50 | Sales | 70000 | Canada |
NOT
Find employees who are not in the IT department:
SELECT * FROM Employees WHERE NOT Department = 'IT';
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
1 | John | Smith | 30 | Sales | 50000 | USA |
2 | Jane | Doe | 45 | HR | 65000 | UK |
4 | Michael | Brown | 50 | Sales | 70000 | Canada |
5. Using BETWEEN
Find employees with a salary between 50,000 and 70,000:
SELECT * FROM Employees WHERE Salary BETWEEN 50000 AND 70000;
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
1 | John | Smith | 30 | Sales | 50000 | USA |
3 | Sarah | Johnson | 28 | IT | 55000 | USA |
6. Using LIKE
(Pattern Matching)
Find employees whose first name starts with “J”:
SELECT * FROM Employees WHERE FirstName LIKE 'J%';
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
1 | John | Smith | 30 | Sales | 50000 | USA |
2 | Jane | Doe | 45 | HR | 65000 | UK |
7. Using IN
Find employees who work in the IT or HR department:
SELECT * FROM Employees WHERE Department IN ('IT', 'HR');
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
2 | Jane | Doe | 45 | HR | 65000 | UK |
3 | Sarah | Johnson | 28 | IT | 55000 | USA |
8. Using IS NULL
Find employees with no department assigned:
SELECT * FROM Employees WHERE Department IS NULL;
Result:
(No result since all employees have departments in this example.)
9. Combining Multiple Conditions
Find employees who work in the IT department, earn more than 50000, and are younger than 40:
SELECT * FROM Employees WHERE Department = 'IT' AND Salary > 50000 AND Age < 40;
Result:
EmployeeID | FirstName | LastName | Age | Department | Salary | Country |
---|---|---|---|---|---|---|
3 | Sarah | Johnson | 28 | IT | 55000 | USA |
Conclusion
The WHERE
clause is a powerful feature in MySQL that helps filter data based on specific criteria. By combining it with operators such as AND
, OR
, LIKE
, BETWEEN
, and IN
, you can create advanced queries to retrieve precisely the data you need.