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.

Kindoperator
Example LanguageSQL

📝 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

aThe minuend; if NULL the result is NULL.
bThe 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏