mod(-7, 3) returns -1 in PostgreSQL, not 2. PostgreSQL MOD returns the remainder of a division, and the result keeps the sign of the first number.
The other catch: there’s no MOD for double precision. A float column fails with “function mod(double precision, integer) does not exist”.
The six examples cover minutes to hours, even rows, batches, decimals, negatives and division by zero. Tested on PostgreSQL 18.6 for Windows.
PostgreSQL MOD Function Syntax
Here’s the syntax:
mod ( y numeric_type, x numeric_type ) → numeric_type
y % x
div ( y numeric, x numeric ) → numeric
y(required): the dividend. NULL gives NULL.x(required): the divisor. 0 raisesdivision by zero; NULL gives NULL.
Return value: the input type (smallint, integer, bigint or numeric). div always returns numeric.
Availability: all supported releases. Unsure of a type? See PostgreSQL data types.
When to Use the MOD Function in PostgreSQL
Use MOD when you need to:
- split minutes into hours
- tag even or odd rows, or keep every Nth row
- spread rows over worker batches
- pull the fraction out of a decimal
Run this once; every example uses it:
DROP TABLE IF EXISTS shipments;
CREATE TABLE shipments (
shipment_id int PRIMARY KEY,
transit_min int,
boxes int,
per_pallet int,
miles double precision
);
INSERT INTO shipments VALUES
(1001, 135, 50, 12, 12.4), (1002, 48, 20, 10, 3.75),
(1004, 260, 37, 8, 41.2), (1005, 95, 12, 0, 8.6),
(1007, 182, 64, 16, 19.05), (1008, 60, 9, NULL, 5.5),
(1010, 315, 45, 12, 27.3), (1011, 72, 30, 10, 6.8);
Example 1: Convert Minutes to Hours with MOD, % and DIV
First, a common job: I want 135 minutes as 2 hours and 15 minutes.
SELECT shipment_id,
mod(transit_min, 60) AS mod_min,
transit_min % 60 AS pct_min,
div(transit_min, 60) AS div_hours
FROM shipments
WHERE shipment_id IN (1001, 1004);
Output:
shipment_id | mod_min | pct_min | div_hours
-------------+---------+---------+-----------
1001 | 15 | 15 | 2
1004 | 20 | 20 | 4
(2 rows)

For shipment 1001, this returns 15 from MOD and % (the same operation) and 2 from DIV, which returns numeric.
Example 2: Find Even and Odd Rows in PostgreSQL
IDs 1003 and 1006 are missing, so mod(shipment_id, 2) mislabels rows. The goal is parity by position:
SELECT shipment_id, rn, mod(rn, 2) = 0 AS even_row
FROM (SELECT shipment_id,
row_number() OVER (ORDER BY shipment_id) AS rn
FROM shipments) s
WHERE shipment_id < 1005;
Output:
shipment_id | rn | even_row
-------------+----+----------
1001 | 1 | f
1002 | 2 | t
1004 | 3 | f
(3 rows)

Shipment 1004 is row 3, so this returns f.
Run the SELECT again with mod(rn, 3) = 0 and you should see 1004 and 1008, every third row. I have a separate guide on PostgreSQL ROW_NUMBER.
Example 3: Split Rows into 4 Batches with the % Operator
Say you need four workers to share the shipments:
SELECT shipment_id % 4 AS batch,
string_agg(shipment_id::text, ', ' ORDER BY shipment_id) AS ids
FROM shipments
GROUP BY batch
ORDER BY batch;
Output:
batch | ids
-------+------------
0 | 1004, 1008
1 | 1001, 1005
2 | 1002, 1010
3 | 1007, 1011
(4 rows)
Batch 0 gets 1004 and 1008; each batch gets two rows. See PostgreSQL GROUP BY for the grouping.
Example 4: MOD with Decimals and Double Precision
Now decimals. miles is double precision, and I want each trip’s fraction:
SELECT mod(miles, 1) FROM shipments;
Output:
psql:mod-decimal-double-precision-error.sql:1: ERROR: function mod(double precision, integer) does not exist
LINE 1: SELECT mod(miles, 1) FROM shipments;
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

% fails the same way. Cast to numeric; a numeric literal like 10.5 works as is:
SELECT mod(10.5, 3) AS numeric_mod;
SELECT miles, mod(miles::numeric, 1) AS fraction
FROM shipments
WHERE shipment_id IN (1002, 1007);
Output:
numeric_mod
-------------
1.5
(1 row)
miles | fraction
-------+----------
3.75 | 0.75
19.05 | 0.05
(2 rows)
mod(10.5, 3) returns 1.5, and 3.75 miles returns 0.75. PostgreSQL ROUND needs the same cast on floats.
Example 5: How Does MOD Handle Negative Numbers?
Back to the intro’s catch. The goal is MOD and DIV on negatives, plus a positive remainder:
SELECT y, x,
mod(y, x) AS mod_result,
div(y, x) AS div_result,
((y % x) + x) % x AS positive_mod
FROM (VALUES (-7, 3), (-7, 2)) AS t(y, x);
Output:
y | x | mod_result | div_result | positive_mod
----+---+------------+------------+--------------
-7 | 3 | -1 | -2 | 2
-7 | 2 | -1 | -3 | 1
(2 rows)

For -7 and 3, this returns -1, and the fix returns 2. div(-7, 2) gives -3 because it truncates toward zero. The fix assumes a positive divisor.
Example 6: Avoid the Division by Zero Error
Shipment 1005 has per_pallet = 0. You want loose boxes after full pallets:
SELECT mod(boxes, per_pallet) FROM shipments;
Output:
psql:mod-division-by-zero-error.sql:1: ERROR: division by zero
One zero stops the query. NULLIF turns 0 into NULL:
\pset null '(null)'
SELECT shipment_id,
mod(boxes, NULLIF(per_pallet, 0)) AS loose_boxes
FROM shipments
WHERE shipment_id IN (1001, 1005, 1008);
Output:
shipment_id | loose_boxes
-------------+-------------
1001 | 2
1005 | (null)
1008 | (null)
(3 rows)
Shipment 1001 returns 2; 1005 and 1008 return NULL, not an error.
Tips & Common Mistakes
- NULL in, NULL out:
mod(7, NULL)returned NULL. - Float input errors; cast to numeric. SQL Server has its own float modulo error.
- Mixed types widen:
mod(10, 3.0)returned 1.0.
Wrapping Up
y % x covers most jobs; Example 5 handles negatives. Next step: label batches with PostgreSQL CASE. The complete list of PostgreSQL functions and the PostgreSQL 18 math functions page have the rest.
Two PostgreSQL pages that pair well with MOD:
Frequently Asked Questions
Does integer division truncate in PostgreSQL?
Yes. -7 / 2 returned -3. Use 7 / 2.0 for 3.5.
What does mod(0, 5) return in PostgreSQL?
0. Only a zero divisor raises an error.
Does PostgreSQL have Oracle’s REMAINDER function?
No. mod() and % truncate; Oracle’s REMAINDER rounds.

Kumar Saurabh is a freelance software engineer who builds web and mobile apps with Next.js, React, Flutter and Python. From 2021 to 2024, Saurabh worked as an IT analyst and developer at TSInfo Technologies, writing technical articles on SQL and PostgreSQL. Saurabh writes the PostgreSQL, MySQL, MariaDB and MongoDB tutorials on DatabaseFAQs.com and holds a Master of Computer Applications (MCA) from Chandigarh University.