All Products
Search
Document Center

AnalyticDB:Logical operators

Last Updated:Mar 28, 2026

AnalyticDB for PostgreSQL supports the same logical operators as PostgreSQL. Use AND, OR, and NOT to build complex filter conditions in SQL queries.

For the full PostgreSQL specification, see Logical operators.

Operators

OperatorSyntaxDescription
ANDa AND bReturns TRUE when both operands are TRUE
ORa OR bReturns TRUE when at least one operand is TRUE
NOTNOT aInverts the Boolean value of the operand

Operator precedence (highest to lowest):

  1. NOT

  2. AND

  3. OR

Use parentheses to override the default precedence when combining multiple operators in a single condition.

NULL behavior

Logical operations involving NULL do not always return NULL. For example, NULL AND FALSE evaluates to FALSE, and NULL OR TRUE evaluates to TRUE.

The complete truth tables are shown below.

AND and OR truth tables

aba AND ba OR b
TRUETRUETRUETRUE
TRUEFALSEFALSETRUE
TRUENULLNULLTRUE
FALSEFALSEFALSEFALSE
FALSENULLFALSENULL
NULLNULLNULLNULL

NOT

aNOT a
TRUEFALSE
FALSETRUE
NULLNULL

Examples

The examples below use a sample table to show how each operator filters rows.

CREATE TABLE orders (id INT, amount INT, status VARCHAR);
INSERT INTO orders VALUES
  (1, 8,  'shipped'),
  (2, 25, 'pending'),
  (3, 15, 'pending'),
  (4, 47, 'shipped');

AND — both conditions must be true

SELECT * FROM orders WHERE amount > 20 AND status = 'pending';

Result:

 id | amount | status
----+--------+---------
  2 |     25 | pending

OR — either condition can be true

SELECT * FROM orders WHERE amount > 20 OR status = 'pending';

Result:

 id | amount | status
----+--------+---------
  2 |     25 | pending
  3 |     15 | pending
  4 |     47 | shipped

Precedence — AND binds tighter than OR

The following query returns rows where status = 'pending', OR where amount = 8 AND status = 'shipped'. AND is evaluated first.

SELECT * FROM orders WHERE status = 'pending' OR amount = 8 AND status = 'shipped';

Result:

 id | amount | status
----+--------+---------
  1 |      8 | shipped
  2 |     25 | pending
  3 |     15 | pending

To evaluate OR first, add parentheses:

SELECT * FROM orders WHERE (status = 'pending' OR amount = 8) AND status = 'shipped';

Result:

 id | amount | status
----+--------+---------
  1 |      8 | shipped

NOT — negates a condition

SELECT * FROM orders WHERE NOT status = 'shipped';

Result:

 id | amount | status
----+--------+---------
  2 |     25 | pending
  3 |     15 | pending