Arithmetic modulo operator %

Last updated: 2026-11-04

Arithmetic modulo operator %

The modulo operator % returns the remainder of integer division and is equivalent to the mod() function. It is handy for "every Nth row" or bucketing by sequence number. Note that PostgreSQL truncates toward zero, so the remainder carries the sign of the dividend: -10 % 3 is -1. A NULL operand yields a NULL result.

Kindoperator
Example LanguageSQL

📝 Syntax

a % b

Precedence: `%` shares a level with `*` and `/` and is left-associative. The sign rule matches division — truncation toward zero, so -10 % 3 is -1 rather than 2. Use `abs()` or add one `b` when you need a non-negative remainder.

⚙️ Parameters

aThe dividend; if NULL the result is NULL.
bThe divisor; if NULL the result is NULL; 0 raises `division by zero`.

Returns:An integer remainder of the same type as integer division; NULL if either operand is NULL.

▶ 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 remainder, mod(a, b) AS mod_fn FROM t_mod ORDER BY id
id | remainder | mod_fn
---+-----------+-------
 1 |         1 |      1
 2 |      NULL |   NULL
 3 |        -1 |     -1
(3 rows)

SELECT a % b IS NULL AS null_is_absent FROM t_mod WHERE id = 2
null_is_absent
--------------
true          
(1 row)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QHow do % and mod() differ?
AThere is none: % is the operator spelling of mod(), both truncating toward zero. For a non-negative remainder use mod(a, b) + b or abs().
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%

🙏 帮我们做得更好

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

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