MYSQL Tutorial



MySQL WHERE


MySQL WHERE Clause – Filter Data Like a Pro

The WHERE clause is used to filter records in SQL based on one or more conditions. It is often combined with SELECT, UPDATE, DELETE, and other commands.

πŸ”Ή Basic Syntax

SELECT column1, column2, ...
FROM table_name
WHERE condition;
  

πŸ”Ή Example Table: employees

Let’s use this sample table again:

| employee_id | first_name | last_name | department | salary | hire_date  |
|-------------|------------|-----------|------------|--------|------------|
| 1           | John       | Doe       | Sales      | 50000  | 2020-01-10 |
| 2           | Jane       | Smith     | Marketing  | 60000  | 2019-05-15 |
| 3           | Mike       | Johnson   | IT         | 70000  | 2021-03-01 |
  

πŸ”Ή Example 1: Filter by One Condition

Get employees from the Sales department:

SELECT * FROM employees WHERE department = 'Sales';
  

πŸ”Ή Example 2: Numeric Comparison

Get employees with salary greater than 55,000:

SELECT first_name, salary FROM employees WHERE salary > 55000;
  

πŸ”Ή Example 3: Multiple Conditions with AND

Get employees from the IT department and salary above 60,000:

SELECT * FROM employees WHERE department = 'IT' AND salary > 60000;
  

πŸ”Ή Example 4: Multiple Conditions with OR

Get employees from Sales or Marketing departments:

SELECT * FROM employees WHERE department = 'Sales' OR department = 'Marketing';
  

πŸ”Ή Example 5: Using IN for Multiple Values

Another way to write example 4 using IN:

SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
  

πŸ”Ή Example 6: Filtering by Date

Get employees hired after January 1, 2020:

SELECT * FROM employees WHERE hire_date > '2020-01-01';
  

πŸ”Ή Interactive Challenge

Write a query to find employees who earn less than 60,000 and are in the Marketing department.

Click here for the answer
SELECT * FROM employees WHERE salary < 60000 AND department = 'Marketing';
    

πŸ”Ή Summary

  • WHERE filters rows based on conditions.
  • Use =, <, >, <=, >=, <> for comparisons.
  • Combine conditions with AND and OR.
  • Use IN to test for multiple values.
  • Works with numbers, text, and dates.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review