WHERE
Clause β Filter Data Like a ProThe 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.
SELECT column1, column2, ... FROM table_name WHERE condition;
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 |
Get employees from the Sales
department:
SELECT * FROM employees WHERE department = 'Sales';
Get employees with salary greater than 55,000:
SELECT first_name, salary FROM employees WHERE salary > 55000;
AND
Get employees from the IT department and salary above 60,000:
SELECT * FROM employees WHERE department = 'IT' AND salary > 60000;
OR
Get employees from Sales
or Marketing
departments:
SELECT * FROM employees WHERE department = 'Sales' OR department = 'Marketing';
IN
for Multiple ValuesAnother way to write example 4 using IN
:
SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
Get employees hired after January 1, 2020:
SELECT * FROM employees WHERE hire_date > '2020-01-01';
Write a query to find employees who earn less than 60,000 and are in the Marketing
department.
SELECT * FROM employees WHERE salary < 60000 AND department = 'Marketing';
WHERE
filters rows based on conditions.=, <, >, <=, >=, <>
for comparisons.AND
and OR
.IN
to test for multiple values.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!