What is the SQL AND Operator?
The AND operator in SQL is a logical operator used in the WHERE or HAVING clause to combine two or more conditions. It returns TRUE if all the conditions separated by AND are true. If any condition is FALSE, the entire AND statement evaluates to FALSE.
Syntax of SQL AND Operator
Key Features of the AND Operator
- Multiple Conditions: Combine multiple conditions to filter data precisely.
- Strict Evaluation: All conditions must be true for the row to be included in the result set.
- Can be Combined with Other Logical Operators: Combine
ANDwithORandNOTfor more complex conditions.
Examples of SQL AND Operator
1. Basic Usage
Retrieve employees from the "IT" department earning more than 5000.
2. Multiple Conditions
Retrieve employees from the "Sales" department who earn between 4000 and 8000.
3. AND with Date Filters
Retrieve orders placed after January 1, 2024, by customers from the USA.
4. Combining AND with Other Operators
Retrieve products that are in stock and have a price of less than 100.
Combining AND with Other Logical Operators
1. AND with OR
Retrieve employees who are in the "IT" department and earn more than 5000, or employees in the "HR" department.
2. AND with NOT
Retrieve employees who are not in the "Sales" department but earn more than 7000.
Order of Evaluation for AND
In SQL, AND is evaluated after NOT but before OR. Use parentheses to explicitly specify the evaluation order and improve readability.
Example Without Parentheses
In this case:
NOT department = 'Sales' AND salary > 7000is evaluated first.- Then, the result is combined with
OR department = 'HR'.
Example With Parentheses
To change the order of evaluation, use parentheses.
Performance Considerations
- Optimize Conditions: Place the most restrictive condition first to reduce the number of rows processed by subsequent conditions.
- Indexes: Use indexed columns in
ANDconditions for better performance. - Avoid Redundancy: Ensure no duplicate conditions in the query.
Common Use Cases for AND
1. Filtering Data by Multiple Attributes
Retrieve students enrolled in "Math" with a grade higher than 80.
2. Filtering with Ranges
Retrieve products with prices between 50 and 100 and in stock.
3. Filtering by Date and Location
Retrieve orders placed in January 2025 by customers from "New York."
Best Practices for Using AND
- Use Parentheses for Clarity: Always use parentheses when combining
ANDwithORfor better readability and accurate evaluation. - Simplify Complex Conditions: Break long conditions into manageable parts for easier debugging and maintenance.
- Index Columns Used in
AND: Queries with indexed columns inANDconditions perform faster.
Conclusion
The SQL AND operator is a critical tool for refining queries by combining multiple conditions. Whether you’re filtering by numeric ranges, dates, or text attributes, mastering the AND operator allows you to retrieve precise and meaningful results from your database.

