PostgreSQL Operators
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.
1️⃣ Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example |
|---|---|---|
+ |
Addition | salary + 5000 |
- |
Subtraction | salary - 1000 |
* |
Multiplication | salary * 2 |
/ |
Division | salary / 12 |
% |
Modulus | salary % 2 |
2️⃣ Comparison Operators
Used to compare values.
| Operator | Meaning |
|---|---|
= |
Equal |
!= or <> |
Not equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
3️⃣ Logical Operators
Used to combine conditions.
| Operator | Description |
|---|---|
AND |
All conditions must be true |
OR |
Any condition true |
NOT |
Negates condition |
4️⃣ LIKE / ILIKE Operators
Used for pattern matching.
Case-insensitive:
5️⃣ IN Operator
Matches values in a list.
6️⃣ BETWEEN Operator
Checks value in a range.
7️⃣ IS NULL / IS NOT NULL
Used to check NULL values.
8️⃣ EXISTS Operator
Checks if a subquery returns any row.
9️⃣ ANY / ALL Operators
🔟 Bitwise Operators
| Operator | Description |
|---|---|
& |
AND |
|
|
# |
XOR |
~ |
NOT |
<< |
Left shift |
>> |
Right shift |
1️⃣1️⃣ String Operators
| Operator | Use |
|---|---|
| ` |
1️⃣2️⃣ Set Operators
Used to combine query results.
| Operator | Description |
|---|---|
UNION |
Combine unique rows |
UNION ALL |
Combine all rows |
INTERSECT |
Common rows |
EXCEPT |
Rows in first, not second |
1️⃣3️⃣ Operator Precedence (High → Low)
-
Arithmetic (
*,/) -
Arithmetic (
+,-) -
Comparison (
=,>,<) -
NOT
-
AND
-
OR
Use parentheses to control order:
1️⃣4️⃣ Best Practices
✔ Use parentheses for clarity
✔ Prefer ILIKE for case-insensitive search
✔ Avoid unnecessary complex expressions
✔ Index columns used with operators
