The SQL AND Operator
The SQL AND operator is used to combine multiple conditions or records in a SQL query to retrieve rows from a database table that satisfy all of the specified conditions.
When you use the AND operator, both conditions must be true for a row to be included in the result set.
Here is the basic syntax of the AND operator in SQL, and the WHERE clause may have one or more AND operators.
SELECT column1, column2 FROM table_name
WHERE condition1 AND condition2;
Let’s explain with an example using the “Employees” table:
Just assume we have an “Employees” table with the following data:
EmployeeID FirstName LastName Department Salary
1 John Verma HR 50000
2 Jane Sharma Sales 55000
3 Robert Johnson IT 60000
4 Lisa Pandit Marketing 52000
5 Sarah Lee HR 48000
Now, suppose you want to retrieve the employees who work in the HR department and have a salary greater than or equal to 50,000. You can use the AND operator to combine these conditions:
SQL AND Operator example
SELECT EmployeeID, FirstName, LastName, Department, Salary FROM Employees
WHERE Department = ‘HR’ AND Salary >= 50,000;
The result of the above SQL query will be:
EmployeeID FirstName LastName Department Salary
1 John Verma HR 50000
5 Sarah Lee HR 48000
In the above SQL AND Operator example, only employees who satisfy both conditions (working in the HR department and having a salary greater than or equal to 50,000) are included in the result set.
The sql AND operator ensures that both conditions must be true for a row to be selected.