AND Operator in SQL
The AND operator in SQL is used to combine multiple conditions in a WHERE or HAVING clause. All the conditions combined with AND must be true for the overall condition to be true.
SQL Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND ...;
FROM table_name
WHERE condition1 AND condition2 AND ...;
Examples: Example with WHERE Clause Suppose we have a table employees with the following columns: employee_id, first_name, last_name, department_id, and salary. Table: employees
employee_id | first_name | last_name | department_id | salary |
---|---|---|---|---|
1 | John | Doe | 1 | 50000 |
2 | Jane | Smith | 2 | 60000 |
3 | Michael | O'Connor | 1 | 55000 |
4 | Sarah | O'Brien | 3 | 45000 |
5 | Laura | Wilson | 2 | 70000 |
We want to find employees who are in department 1 and have a salary greater than 50000.
SQL
SELECT first_name, last_name, department_id, salary FROM employees WHERE department_id = 1 AND salary > 50000;
Result
first_name | last_name | department_id | salary |
---|---|---|---|
Michael | O'Connor | 1 | 55000 |