TExcelTAD
Home › SQL › SQL AND

SQL AND

A WHERE clause can combine several conditions. With AND, a row is kept only when all conditions are true.

sql
SELECT columns
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

Two conditions

Electronics that cost less than 30:

sql · try it yourself
SELECT name, price
FROM products
WHERE category_id = 6 AND price < 30
ORDER BY price;
name           | price
---------------+------
USB-C Cable    | 9.99
Wireless Mouse | 24.99
Power Bank     | 29.5
(3 rows)

Three or more conditions

Add as many conditions as you need. Delivered orders handled by employee 6 in the first half of 2025:

sql · try it yourself
SELECT order_id, order_date, status
FROM orders
WHERE employee_id = 6
  AND status = 'delivered'
  AND order_date < '2025-07-01'
ORDER BY order_date;
order_id | order_date | status
---------+------------+----------
2        | 2025-01-02 | delivered
7        | 2025-02-20 | delivered
12       | 2025-03-01 | delivered
13       | 2025-04-08 | delivered
20       | 2025-05-15 | delivered
22       | 2025-06-06 | delivered
(6 rows)

AND as a range

Two comparisons on the same column with AND describe a range — products priced from 5 up to 10:

sql · try it yourself
SELECT name, price
FROM products
WHERE price >= 5 AND price <= 10
ORDER BY price, name;
name             | price
-----------------+------
Sourdough Loaf   | 5.5
Salted Almonds   | 5.99
Butter Croissant | 6.25
Swiss Cheese     | 7.8
Green Tea        | 8.75
USB-C Cable      | 9.99
(6 rows)

The BETWEEN operator (covered later) is a shorter way to write exactly this.

Truth table

A B A AND B
true true true
true false false
false true false
false false false
true NULL NULL
false NULL false

SQL uses three-valued logic: a comparison with NULL is neither true nor false but unknown (NULL). WHERE keeps only rows where the whole condition is true, so unknown rows are dropped.

sql · try it yourself
SELECT name, email, country
FROM customers
WHERE country = 'Mexico' AND email = 'nobody@example.com';

SELECT name, email
FROM customers
WHERE country = 'Mexico' AND email IS NULL;
name | email | country
-----+-------+--------
(0 rows)

name        | email
------------+------
Felix Reyes | NULL
(1 row)

The first query returns nothing; the second finds Felix, whose email is missing.

Tip: Put each condition on its own line starting with AND. It makes long filters easy to read and easy to comment out while testing.

Test yourself

When is a row returned for WHERE a AND b?

  1. When a is true
  2. When either is true
  3. When both are true
  4. When both are false

What is true AND NULL?

  1. true
  2. false
  3. NULL (unknown)
  4. An error