PostgreSQL Operators

PostgreSQL Tutorial

PostgreSQL Operators

Operators in PostgreSQL are symbols or keywords used to perform operations on data in SQL queries. They are mainly used in SELECT, WHERE, UPDATE, and DELETE statements.


 Arithmetic Operators

Used for mathematical calculations.

OperatorDescriptionExample
+Additionsalary + 5000
-Subtractionsalary - 1000
*Multiplicationsalary * 2
/Divisionsalary / 12
%Modulussalary % 2

Comparison Operators

Used to compare values.

OperatorMeaning
=Equal
!= or <>Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Logical Operators

Used to combine conditions.

OperatorDescription
ANDAll conditions must be true
ORAny condition true
NOTNegates condition

LIKE / ILIKE Operators

Used for pattern matching.

Case-insensitive:


IN Operator

Matches values in a list.


BETWEEN Operator

Checks value in a range.


IS NULL / IS NOT NULL

Used to check NULL values.


EXISTS Operator

Checks if a subquery returns any row.


 ANY / ALL Operators


Bitwise Operators

OperatorDescription
&AND
``
#XOR
~NOT
<<Left shift
>>Right shift
SELECT 5 & 3;

 String Operators

OperatorUse
`

 Set Operators

Used to combine query results.

OperatorDescription
UNIONCombine unique rows
UNION ALLCombine all rows
INTERSECTCommon rows
EXCEPTRows in first, not second

 Operator Precedence (High → Low)

  1. Arithmetic (*, /)

  2. Arithmetic (+, -)

  3. Comparison (=, >, <)

  4. NOT

  5. AND

  6. OR

Use parentheses to control order:


 Best Practices

  •  Use parentheses for clarity
  •  Prefer ILIKE for case-insensitive search
  •  Avoid unnecessary complex expressions
  •  Index columns used with operators

You may also like...