SQL

SQL AND Operator: Syntax, Usage, and Examples

The AND operator combines multiple conditions in a WHERE clause and returns rows only when all conditions are true. Use it when you want tighter filters instead of broad results.


How to Use the AND operator

You use the AND operator inside WHERE, HAVING, or join conditions. Each condition on both sides of AND must evaluate to true for a row to be included.

Basic syntax

SELECT column1, column2
FROM table_name
WHERE condition1AND condition2;

Syntax with multiple conditions

SELECT column1, column2
FROM table_name
WHERE condition1
AND condition2
AND condition3;

A quick mental shortcut: AND means “keep the row only if it passes every test.”


When to Use the AND operator

The AND operator is useful anytime one condition isn’t enough. Here are common situations where it makes your query more precise.

1) Filtering by more than one column

A single filter can be too broad. For example, looking for customers in a country might return thousands of results. Adding a second condition like “signed up this month” makes the result list actually useful.

2) Creating strict rules for eligibility

A lot of queries work like checklists:

  • order must be paid and shipped
  • student must be active and enrolled
  • product must be in stock and not discontinued

That logic maps perfectly to AND.

3) Narrowing down time-based data

Time filters often work best in pairs:

  • a date must be after a certain start date and before an end date
  • a session must be longer than 30 seconds and shorter than 1 hour

4) Keeping analytics segments clean

In reporting queries, you might define “real users” like this:

  • not a test account and
  • not an internal email domain and
  • has at least one completed event

Without AND, you end up mixing in noise.


Examples of the AND operator

Let’s go through several examples you’ll see all the time in real projects.

Example 1: Filter by category and price

You want products that match a category and stay under a price limit.

SELECT product_id, product_name, price
FROM products
WHERE category='Books'AND price<20;

This returns only items that pass both rules.


Example 2: Filter by country and signup date

You want users from Germany who joined in 2026.

SELECT user_id, full_name, country, signup_date
FROM users
WHERE country='Germany'AND signup_date>='2026-01-01';

The AND operator keeps the results focused.


Example 3: Find active orders that are also paid

You want orders that are still active and already paid.

SELECT order_id, customer_id, status, payment_status
FROM orders
WHERE status='Active'AND payment_status='Paid';

This prevents “Active but unpaid” orders from showing up.


Example 4: Combine numeric ranges (age filter)

You want customers between 18 and 30 years old.

SELECT customer_id, full_name, age
FROM customers
WHERE age>=18AND age<=30;

This reads like a clean rule: age must be at least 18 and at most 30.


Example 5: AND with a calculated condition

You can use AND with expressions too, not only direct comparisons.

SELECT order_id, total_amount, discount_amount
FROM orders
WHERE total_amount>50AND (discount_amount/ total_amount)>=0.10;

The parentheses keep the math grouped properly, so the logic stays readable.


Learn More About the AND operator

The basics are simple, but there are a few details worth knowing so your queries don’t surprise you.

AND vs OR

This is one of the most common beginner mistakes.

  • AND means all conditions must be true
  • OR means at least one condition must be true

Compare the difference:

-- Narrow results
SELECT*
FROM orders
WHERE status='Active'AND payment_status='Paid';

-- Broad results
SELECT*
FROM orders
WHERE status='Active'OR payment_status='Paid';

The second query can include:

  • Active but not paid
  • Paid but not active
  • Active and paid

So if your results suddenly look “too big,” OR is often the reason.


Operator precedence and parentheses

SQL evaluates AND before OR in most databases. That means this:

WHERE condition1OR condition2AND condition3

is treated like:

WHERE condition1OR (condition2AND condition3)

If you meant something else, use parentheses so your query says what you mean.

Example:

You want users who are either admins or moderators, but only if they are active:

✅ Correct:

SELECT user_id, role, is_active
FROM users
WHERE (role='Admin'OR role='Moderator')
AND is_active=1;

Without parentheses, you could accidentally include inactive admins, which might be a security problem depending on the data.


AND with LIKE (text filtering)

You can combine multiple text filters using AND, especially when searching names, descriptions, or tags.

SELECT product_id, product_name
FROM products
WHERE product_nameLIKE'%wireless%'
AND product_nameLIKE'%mouse%';

This returns products that contain both “wireless” and “mouse” in the name.


AND with NULL values

NULL means “missing” or “unknown,” so comparisons can behave differently than expected.

Example:

SELECT*
FROM users
WHERE age>18AND email_verified=1;

If age is NULL, age > 18 is not true, and the row is filtered out.

If you want to include rows with missing values, you must handle them explicitly:

SELECT*
FROM users
WHERE (age>18OR ageISNULL)
AND email_verified=1;

That reads like: “adult or unknown age, and verified email.”


AND inside HAVING

AND is just as useful after grouping, especially when you filter aggregated results.

Example: you want customers who placed at least 3 orders and spent more than $200 total.

SELECT customer_id,COUNT(*)AS order_count,SUM(total_amount)AS total_spent
FROM orders
GROUPBY customer_id
HAVINGCOUNT(*)>=3ANDSUM(total_amount)>200;

This is a great example of using the AND operator for business rules.


AND in JOIN conditions

Sometimes AND belongs in your ON clause, especially when you match tables using multiple keys.

Example: join order items to orders, but only for completed orders:

SELECT o.order_id, oi.product_id, oi.quantity
FROM orders o
JOIN order_items oi
ON o.order_id= oi.order_id
AND o.status='Completed';

This approach can be cleaner than joining everything and filtering later.


Common AND mistakes (and how to avoid them)

1) Forgetting parentheses with OR

If you mix AND and OR, always stop and ask yourself: “Which part should be grouped?”

A good habit: add parentheses even when you think SQL will interpret it correctly. Future you will thank you.

2) Writing contradictory conditions

This returns nothing:

WHERE price>100AND price<50

That’s a logical dead end. Happens more often than you’d expect when people copy filters around.

3) Filtering too aggressively

AND makes queries strict. If your results suddenly go from 5,000 rows to 0 rows, one condition is probably too limiting, or the column has missing values.


Summary

The AND operator helps you combine conditions so SQL returns only rows that match every rule you set. You’ll use it constantly for filtering data, creating strict eligibility requirements, and writing clean analytics queries. The main things to remember are how AND behaves compared to OR, how parentheses affect grouping, and how NULL values can quietly remove rows from your results.