Arithmetic division operator /
Last updated: 2026-11-04
Arithmetic division operator /
The division operator / behaves differently depending on operand types: integer divided by integer performs integer division truncated toward zero, while numeric division keeps the fractional part. Dividing by zero raises an error for both. A NULL operand yields a NULL result.
| Kind | operator |
|---|---|
| Example Language | SQL |
📝 Syntax
a / b
Precedence: `/` shares a level with `*` and `%` and is left-associative (`10 / 4 * 2` is 4 — divide first). Type determines precision: `total::int / n` truncates, while `total / n` on numeric keeps decimals. Dividing by zero aborts the statement rather than returning NULL, an important difference from MySQL.
⚙️ Parameters
| a | The dividend; NULL if NULL, and 0 when the divisor is non-zero. |
|---|---|
| b | The divisor; NULL if NULL, and an error "division by zero" when 0. |
Returns:Integer division returns integer truncated toward zero; numeric division returns numeric; dividing by zero raises an error.
▶ 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, total / n AS numeric_div, total::int / n AS int_div FROM t_div ORDER BY id
id | numeric_div | int_div
---+--------------------+--------
1 | 2.5000000000000000 | 2
2 | NULL | NULL
(2 rows)
SELECT total / n IS NULL AS null_is_absent FROM t_div WHERE id = 2
null_is_absent
--------------
true
(1 row)
📌 Key Points
- NULL propagation: the quotient is NULL if either operand is NULL.
- Integer division drops the fraction: 10 / 4 is 2, not 2.5. Cast an operand to numeric when you need decimals.
- Dividing by zero raises `division by zero`; it does not return NULL or Infinity.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QDoes integer division lose precision?
AYes. 10 / 4 is 2 with the fraction truncated. Write 10.0 / 4 or total::numeric / n to get 2.5.