Arithmetic subtraction operator -
Last updated: 2026-11-04
Arithmetic subtraction operator -
The subtraction operator - subtracts the right operand from the left, and also acts as a unary sign operator. It follows the same NULL propagation rule. Beyond numbers, PostgreSQL also overloads it for dates: date - date yields an integer number of days and timestamp - timestamp yields an interval.
| Kind | operator |
|---|---|
| Example Language | SQL |
📝 Syntax
a - b -- binary subtraction
-a -- unary sign
Precedence: binary `+`/`-` share a level and are left-associative (`10 - 3 - 2` is 5); unary `-` binds tighter than `^`, so `-2^2` is 4. `date - date` and `timestamp - timestamp` are type-specific overloads and do not conflict with numeric subtraction.
⚙️ Parameters
| a | The minuend; if NULL the result is NULL. |
|---|---|
| b | The subtrahend; if NULL the result is NULL. |
Returns:A numeric value for numeric subtraction; an integer day count for date - date; an interval for timestamp - timestamp.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT id, a - b AS diff, -a AS negated FROM t_minus ORDER BY id
id | diff | negated
---+------+--------
1 | 7 | -10
2 | NULL | -10
3 | -7 | -3
(3 rows)
SELECT a - b IS NULL AS null_is_absent FROM t_minus WHERE id = 2
null_is_absent
--------------
true
(1 row)
SELECT DATE '2026-01-10' - DATE '2026-01-01' AS days_between
days_between
------------
9
(1 row)
📌 Key Points
- NULL propagation: the result is NULL if either the minuend or the subtrahend is NULL.
- date - date returns an integer day count rather than an interval: 2026-01-10 minus 2026-01-01 is 9.
- Use `abs()` for absolute value instead of hand-rolling it.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QWhy is -2^2 equal to 4 and not -4?
AIn PostgreSQL unary minus binds tighter than `^`, so the expression parses as `(-2)^2` and yields 4. Write `-(2^2)` to get -4.