Explain SQL AND Operator
The AND
operator in SQL is used to combine multiple conditions in a WHERE
clause. It ensures that all conditions must be true for a record to be included in the result set. Here’s a basic example:
Example Scenario
Suppose you have a table called Employees
with the following columns:
EmployeeID
FirstName
LastName
Age
Department
And let's say the table has the following data:
EmployeeID |
FirstName |
LastName |
Age |
Department |
1 |
John |
Doe |
28 |
Sales |
2 |
Jane |
Smith |
32 |
Marketing |
3 |
Bob |
Brown |
45 |
Sales |
4 |
Alice |
Johnson |
38 |
HR |
5 |
Charlie |
Davis |
29 |
Marketing |
SQL Query with AND
Operator
Suppose you want to find employees who are in the Sales
department and are older than 30. You would write a query like this:
SELECT *
FROM Employees
WHERE Department = 'Sales'
AND Age > 30;
Explanation
Department = 'Sales'
checks if the employee is in the Sales department.
Age > 30
checks if the employee is older than 30.
- The
AND
operator ensures both conditions must be true.
Result
Based on the sample data provided, the query would produce the following output:
EmployeeID |
FirstName |
LastName |
Age |
Department |
3 |
Bob |
Brown |
45 |
Sales |
Only Bob meets both conditions (he is in the Sales department and is older than 30).