Explain WHERE Clause in SQL
The WHERE
clause in SQL is used to filter records that meet specific conditions. It allows you to specify criteria for selecting records from a table.
Basic Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example Table
Let's use the same Employees
table:
ID | Name | Department | Age | Salary |
---|---|---|---|---|
1 | John | Sales | 30 | 50000 |
2 | Jane | Marketing | 25 | 60000 |
3 | John | Sales | 35 | 55000 |
4 | Alice | HR | 28 | 52000 |
5 | Bob | Sales | 40 | 58000 |
6 | Jane | Marketing | 30 | 62000 |
Query to Filter by Department
To get all employees from the "Sales" department, you can use the following query:
SELECT *
FROM Employees
WHERE Department = 'Sales';
Output:
ID | Name | Department | Age | Salary |
---|---|---|---|---|
1 | John | Sales | 30 | 50000 |
3 | John | Sales | 35 | 55000 |
5 | Bob | Sales | 40 | 58000 |
Query to Filter by Age
To get all employees who are 30 years old, you can use the following query:
SELECT *
FROM Employees
WHERE Age = 30;
Output:
ID | Name | Department | Age | Salary |
---|---|---|---|---|
1 | John | Sales | 30 | 50000 |
6 | Jane | Marketing | 30 | 62000 |
Query to Filter by Salary
To get all employees with a salary greater than 55000, you can use the following query:
SELECT *
FROM Employees
WHERE Salary > 55000;
Output:
ID | Name | Department | Age | Salary |
---|---|---|---|---|
2 | Jane | Marketing | 25 | 60000 |
5 | Bob | Sales | 40 | 58000 |
6 | Jane | Marketing | 30 | 62000 |
Query to Filter by Multiple Conditions
To get all employees from the "Marketing" department who are older than 25, you can use the following query:
SELECT *
FROM Employees
WHERE Department = 'Marketing' AND Age > 25;
Output:
ID | Name | Department | Age | Salary |
---|---|---|---|---|
6 | Jane | Marketing | 30 | 62000 |
Query to Filter Using OR Condition
To get all employees who are either in the "HR" department or have a salary greater than 55000, you can use the following query:
SELECT *
FROM Employees
WHERE Department = 'HR' OR Salary > 55000;
Output:
ID | Name | Department | Age | Salary |
---|---|---|---|---|
2 | Jane | Marketing | 25 | 60000 |
4 | Alice | HR | 28 | 52000 |
5 | Bob | Sales | 40 | 58000 |
6 | Jane | Marketing | 30 | 62000 |
In these examples, the WHERE
clause is used to specify the conditions that the records must meet in order to be included in the results. This allows for more precise data retrieval based on specific criteria.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.