The AND operator in SQL combines two or more conditions in a WHERE clause, and a row is returned only if every condition is true. For example, SELECT * FROM products WHERE price > 10 AND category = 'Electronics' returns only products that are both priced above 10 and in the Electronics category.
What Is the AND Operator?
In SQL, AND is a logical operator used to filter records based on multiple criteria. It works with any data type—numbers, text, dates, or even subqueries—and is evaluated left to right. The result of an AND expression is TRUE only if all operands are TRUE; if any operand is FALSE, the whole expression is FALSE. If any operand is NULL and no operand is FALSE, the result is NULL (which behaves like FALSE in a WHERE clause).
How AND Works in a WHERE Clause
The most common use of AND is in a WHERE clause to narrow down results. The database engine evaluates each condition for every row and includes the row only if all conditions are true. For instance, consider a table employees with columns department and salary:
SELECT first_name, last_name
FROM employees
WHERE department = 'Sales' AND salary > 50000;
This query returns employees who work in Sales and earn more than 50,000. If you remove the AND and use OR, you would get a much broader set.
Using AND with Other Operators
AND is often combined with OR, NOT, BETWEEN, IN, and LIKE. When combining AND and OR, use parentheses to control precedence because AND is evaluated before OR in most SQL dialects. For example:
SELECT * FROM orders
WHERE (status = 'Shipped' OR status = 'Delivered')
AND total > 100;
Without parentheses, the query would be interpreted as status = 'Shipped' OR (status = 'Delivered' AND total > 100), which is likely not what you want.
Common Mistakes and Pitfalls
- Using AND with NULL: A condition like
column = NULLnever returns true; you must useIS NULL. Similarly,ANDwith a NULL operand can give unexpected results. - Operator precedence: Forgetting parentheses when mixing
ANDandORleads to wrong logic. Always group conditions explicitly. - Over-filtering: Adding too many
ANDconditions can return zero rows. Test each condition separately to debug. - Case sensitivity: String comparisons may be case-sensitive depending on the database collation. Use
LOWER()orUPPER()if needed.
AND vs. OR vs. NOT
AND is the strictest logical operator: it requires all conditions to be true. OR requires at least one condition to be true, and NOT negates a condition. In practice, you often combine them. For instance, to find products that are either cheap or on sale, but not discontinued:
SELECT * FROM products
WHERE (price < 20 OR sale = TRUE)
AND NOT discontinued = TRUE;
Note that NOT has higher precedence than AND, so parentheses are essential.
Performance Considerations
Using AND can affect query performance. When multiple conditions are combined, the database may use indexes to speed up evaluation. For best results:
- Place the most selective condition first (the one that filters out the most rows) to reduce the number of rows checked.
- Create indexes on columns used frequently in
ANDconditions, especially in large tables. - Avoid wrapping columns in functions (e.g.,
YEAR(date)) because that can prevent index usage.
AND in JOINs and Other Clauses
While AND is most common in WHERE, it also appears in JOIN conditions, HAVING clauses, and CASE expressions. For example, in a JOIN:
SELECT *
FROM orders o
JOIN customers c
ON o.customer_id = c.id
AND o.order_date > '2024-01-01';
Here, the AND adds an extra condition to the join, which can be more efficient than putting it in WHERE in some databases.
Frequently Asked Questions
Can I use AND with more than two conditions?
Yes, you can chain as many conditions as needed: WHERE a = 1 AND b = 2 AND c = 3. All must be true for the row to be included.
What is the difference between AND and &&?
In standard SQL, AND is the correct keyword. Some databases like MySQL also support && as an alias, but it is not portable. Stick to AND for compatibility.
Does AND work with NULL values?
If any condition evaluates to NULL, the result of AND is NULL unless another condition is FALSE. In a WHERE clause, NULL is treated as false, so the row is excluded. Use IS NULL or IS NOT NULL to handle nulls explicitly.
Can I use AND in an UPDATE or DELETE statement?
Yes, AND works in any WHERE clause, including those in UPDATE and DELETE statements. For example: UPDATE products SET price = 0 WHERE category = 'Clearance' AND stock < 5;
How do I combine AND with OR correctly?
Use parentheses to group OR conditions together, then combine with AND. For example: WHERE (color = 'Red' OR color = 'Blue') AND size = 'Large'. Without parentheses, AND binds first, changing the meaning.
Is there a limit to the number of AND conditions?
There is no practical limit in most SQL engines, but extremely long WHERE clauses can become hard to read and may hit parser limits. Break complex logic into subqueries or use IN lists where appropriate.
Related
- Google Sheets Count If Not Empty: 4 Easy Formulas
- Traffic Signs Manual Chapter 8: Key Points
- Federal Reserve Bank Balance Sheet Explained
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Create a Signature Block in Microsoft OutlookTo create a signature block in Outlook, open the signature settings for your version, make a signature, and…
- ISNUMBER in Excel: What It Checks and How to Use ItIn Excel, ISNUMBER checks whether a value is stored as a number and returns either TRUE or FALSE.…
- Power Query Remove Duplicates Keep First: How-ToYes, Power Query’s Remove Duplicates command keeps the first occurrence of each duplicate row and removes subsequent ones.…
- Replace Excel Formatting: Find and Change Cell StylesTo replace formatting in Excel for Microsoft 365 on Windows, press Ctrl+H, choose Options, set a format under…
- Border Lines in Excel: Add, Change, and Remove ThemTo add border lines in Excel for Microsoft 365 on Windows, select the cells, go to Home >…
- How to Split a String in C++ (with Code Examples)Splitting a string into smaller pieces is one of the most common tasks in C++ programming, yet the…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.