Request a demo

BigQuery · Math function

BigQuery MOD

MOD(x, y) returns the remainder of x divided by y, with the sign of x: MOD(25, 7) is 4 and MOD(-25, 7) is -4. BigQuery has no % operator, so MOD is how you write modulo, for even/odd checks, every-Nth-row filters and hash buckets.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

MOD returns what is left after dividing X by Y. The result has the sign of the dividend, so MOD(-25, 7) is -4, not 3 as in Python. For a remainder that is never negative, use MOD(MOD(x, n) + n, n).

BigQuery doesn't support the % operator that PostgreSQL, MySQL, Snowflake and Redshift use for modulo: x % 2 fails with a syntax error: Expected end of input but got "%". MOD accepts INT64, NUMERIC and BIGNUMERIC but not FLOAT64, so cast floats first: MOD(CAST(price AS NUMERIC), 1).

A zero divisor raises a division by zero error (division by zero: MOD(25, 0)); SAFE.MOD(x, 0) returns NULL instead. Paired with DIV, MOD splits durations into hours, minutes and seconds, and ABS(MOD(FARM_FINGERPRINT(id), n)) assigns rows to n stable buckets.

Syntax

MOD(X, Y)

Arguments and return value

ArgumentDescription
XThe dividend: INT64, NUMERIC or BIGNUMERIC. The result takes its sign.
YThe divisor: INT64, NUMERIC or BIGNUMERIC. Zero raises an error.

Returns: INT64 when both arguments are INT64, NUMERIC when either is NUMERIC and neither is BIGNUMERIC, BIGNUMERIC when either is BIGNUMERIC.

Examples

Negative operands

The remainder follows the sign of the first argument. Adding n and taking MOD again gives the non-negative remainder.

SELECT
  MOD(25, 7) AS positive,
  MOD(-25, 7) AS negative_dividend,
  MOD(25, -7) AS negative_divisor,
  MOD(MOD(-25, 7) + 7, 7) AS non_negative;
Result
positivenegative_dividendnegative_divisornon_negative
4-443

Seconds to hours, minutes and seconds

WITH jobs AS (
  SELECT 'nightly_load' AS job, 3725 AS runtime_s UNION ALL
  SELECT 'hourly_sync', 59
)
SELECT
  job,
  DIV(runtime_s, 3600) AS hours,
  MOD(DIV(runtime_s, 60), 60) AS minutes,
  MOD(runtime_s, 60) AS seconds
FROM jobs
ORDER BY job;
Result
jobhoursminutesseconds
hourly_sync0059
nightly_load125

Stable shards from a hash

FARM_FINGERPRINT returns a signed INT64, so MOD can be negative. ABS outside MOD maps it to 0 through 3 without the overflow risk of ABS(FARM_FINGERPRINT(...)).

WITH users AS (
  SELECT user_id FROM UNNEST(['u-1001', 'u-1005', 'u-1007', 'u-1018']) AS user_id
)
SELECT
  user_id,
  ABS(MOD(FARM_FINGERPRINT(user_id), 4)) AS shard
FROM users
ORDER BY user_id;
Result
user_idshard
u-10013
u-10051
u-10072
u-10180

Remainders of decimal values

MOD has no FLOAT64 version. NUMERIC works and keeps the fraction.

SELECT
  MOD(NUMERIC '19.99', 1) AS fractional_part,
  MOD(NUMERIC '7.5', 2) AS remainder;
Result
fractional_partremainder
0.991.5

Common mistakes

  • Writing x % n. BigQuery has no % operator and the query fails with a syntax error; use MOD(x, n).
  • Expecting a non-negative result for negative input, as in Python. MOD(-1, 7) is -1; use MOD(MOD(x, 7) + 7, 7).
  • Passing FLOAT64. MOD(7.5, 2) fails with No matching signature for function MOD, even with literals; use MOD(NUMERIC '7.5', 2).
  • Dividing by zero. MOD(x, 0) fails the whole query; use SAFE.MOD(x, y) or MOD(x, NULLIF(y, 0)).
  • Bucketing with MOD(ABS(FARM_FINGERPRINT(id)), n). ABS fails with an integer overflow if the hash is the smallest INT64; ABS(MOD(FARM_FINGERPRINT(id), n)) avoids it.

Cost and performance

  • A filter such as MOD(customer_id, 10) = 0 reduces rows, not bytes scanned. On an integer-range partitioned table it also reads every partition, because BigQuery doesn't prune partitions for functions over the partitioning column.

FAQ

What is the modulo operator in BigQuery?

There isn't one. Use MOD(x, y); x % y is a syntax error in GoogleSQL.

How do I check if a number is even in BigQuery?

MOD(n, 2) = 0 is TRUE for even numbers. For negative odd numbers MOD(n, 2) is -1, so test odd numbers with MOD(n, 2) != 0 rather than = 1.

Why does MOD return a negative number?

MOD takes the sign of the dividend, as in C, Java and PostgreSQL. Use MOD(MOD(x, n) + n, n) to get a result between 0 and n - 1.

Errors you may hit

More mathematical functions

All BigQuery mathematical functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.