SQL WHERE Clause Tutorial
Example Table
We will use the following table named employees
for our examples:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (employee_id, name, department, salary) VALUES
(1, 'Alice', 'HR', 50000.00),
(2, 'Bob', 'IT', 60000.00),
(3, 'Charlie', 'Finance', 55000.00);
Employees Table
employee_id | name | department | salary |
---|---|---|---|
1 | Alice | HR | 50000.00 |
2 | Bob | IT | 60000.00 |
3 | Charlie | Finance | 55000.00 |
Using the SQL WHERE Clause
The SQL WHERE
clause is used to filter records.
Example: Filtering by Department
To select employees from the IT department:
SELECT * FROM employees
WHERE department = 'IT';
Result:
Query | SELECT * FROM employees WHERE department = 'IT'; |
---|---|
Result |
|
Example: Filtering by Salary
To select employees with a salary greater than 55000.00:
SELECT * FROM employees
WHERE salary > 55000.00;
Result:
Query | SELECT * FROM employees WHERE salary > 55000.00; |
---|---|
Result |
|
Example: Combining Conditions
To select employees from the HR department with a salary less than 60000.00:
SELECT * FROM employees
WHERE department = 'HR' AND salary < 60000.00;
Result:
Query | SELECT * FROM employees WHERE department = 'HR' AND salary < 60000.00; |
---|---|
Result |
|