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

OperatorDescriptionExample
=Equal toWHERE Country = 'USA'
!= or <>Not equal toWHERE Country != 'USA'
>Greater thanWHERE Age > 25
<Less thanWHERE Age < 18
>=Greater than or equal toWHERE Age >= 21
<=Less than or equal toWHERE Age <= 65

Example Table: Employees

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
3SarahJohnson28IT55000USA
4MichaelBrown50Sales70000Canada

1. Using =

Find employees from the USA:

SELECT * FROM Employees WHERE Country = 'USA';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
3SarahJohnson28IT55000USA

2. Using != or <>

Find employees not from the USA:

SELECT * FROM Employees WHERE Country != 'USA';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

3. Using > and <

Find employees older than 40:

SELECT * FROM Employees WHERE Age > 40;

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

4. Logical Operators

AND

Find employees in the USA who are older than 25:

SELECT * FROM Employees WHERE Country = 'USA' AND Age > 25;

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA

OR

Find employees in either Sales or HR department:

SELECT * FROM Employees WHERE Department = 'Sales' OR Department = 'HR';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

NOT

Find employees who are not in the IT department:

SELECT * FROM Employees WHERE NOT Department = 'IT';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

5. Using BETWEEN

Find employees with a salary between 50,000 and 70,000:

SELECT * FROM Employees WHERE Salary BETWEEN 50000 AND 70000;

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
3SarahJohnson28IT55000USA

6. Using LIKE (Pattern Matching)

Find employees whose first name starts with “J”:

SELECT * FROM Employees WHERE FirstName LIKE 'J%';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK

7. Using IN

Find employees who work in the IT or HR department:

SELECT * FROM Employees WHERE Department IN ('IT', 'HR');

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
3SarahJohnson28IT55000USA

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:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
3SarahJohnson28IT55000USA

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.

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

OperatorDescriptionExample
=Equal toWHERE Country = 'USA'
!= or <>Not equal toWHERE Country != 'USA'
>Greater thanWHERE Age > 25
<Less thanWHERE Age < 18
>=Greater than or equal toWHERE Age >= 21
<=Less than or equal toWHERE Age <= 65

Example Table: Employees

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
3SarahJohnson28IT55000USA
4MichaelBrown50Sales70000Canada

1. Using =

Find employees from the USA:

SELECT * FROM Employees WHERE Country = 'USA';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
3SarahJohnson28IT55000USA

2. Using != or <>

Find employees not from the USA:

SELECT * FROM Employees WHERE Country != 'USA';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

3. Using > and <

Find employees older than 40:

SELECT * FROM Employees WHERE Age > 40;

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

4. Logical Operators

AND

Find employees in the USA who are older than 25:

SELECT * FROM Employees WHERE Country = 'USA' AND Age > 25;

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA

OR

Find employees in either Sales or HR department:

SELECT * FROM Employees WHERE Department = 'Sales' OR Department = 'HR';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

NOT

Find employees who are not in the IT department:

SELECT * FROM Employees WHERE NOT Department = 'IT';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK
4MichaelBrown50Sales70000Canada

5. Using BETWEEN

Find employees with a salary between 50,000 and 70,000:

SELECT * FROM Employees WHERE Salary BETWEEN 50000 AND 70000;

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
3SarahJohnson28IT55000USA

6. Using LIKE (Pattern Matching)

Find employees whose first name starts with “J”:

SELECT * FROM Employees WHERE FirstName LIKE 'J%';

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
1JohnSmith30Sales50000USA
2JaneDoe45HR65000UK

7. Using IN

Find employees who work in the IT or HR department:

SELECT * FROM Employees WHERE Department IN ('IT', 'HR');

Result:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
2JaneDoe45HR65000UK
3SarahJohnson28IT55000USA

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:

EmployeeIDFirstNameLastNameAgeDepartmentSalaryCountry
3SarahJohnson28IT55000USA

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.