SQL And
Example Table
We will use the following table named employees for our examples:
CREATE TABLE employees (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
INSERT INTO employees (id, first_name, last_name, department, salary) VALUES
(1, 'John', 'Doe', 'Engineering', 75000.00),
(2, 'Jane', 'Smith', 'Marketing', 65000.00),
(3, 'Sam', 'Brown', 'Engineering', 80000.00),
(4, 'Sue', 'Johnson', 'HR', 60000.00),
(5, 'Mike', 'Davis', 'Marketing', 70000.00);
| id | first_name | last_name | department | salary |
|---|---|---|---|---|
| 1 | John | Doe | Engineering | 75000.00 |
| 2 | Jane | Smith | Marketing | 65000.00 |
| 3 | Sam | Brown | Engineering | 80000.00 |
| 4 | Sue | Johnson | HR | 60000.00 |
| 5 | Mike | Davis | Marketing | 70000.00 |
Using SQL AND
The SQL AND operator is used to combine multiple conditions in a WHERE clause. All conditions must be true for the row to be included in the result set.
Example 1: Filtering by Department and Salary
SELECT first_name, last_name, department, salary FROM employees WHERE department = 'Engineering' AND salary > 75000;
Result:
| first_name | Sam |
|---|---|
| last_name | Brown |
| department | Engineering |
| salary | 80000.00 |
Example 2: Filtering by Department and Last Name
SELECT first_name, last_name, department, salary FROM employees WHERE department = 'Marketing' AND last_name = 'Davis';
Result:
| first_name | Mike |
|---|---|
| last_name | Davis |
| department | Marketing |
| salary | 70000.00 |