SQL Modulo

The % operator returns the REMAINDER left over after division — useful for checking evenness, cycling through a fixed number of buckets, or spotting patterns in IDs.

What & Why

a % b returns whatever is left over after dividing a by b as many whole times as possible. A classic use: id % 2 = 0 checks whether a value is even.

See How It Works

BUSINESS QUESTION

Growth wants each user assigned to one of four deterministic analysis buckets.

idcreated_atcountrychannelplanactivated_atchurned_at
1012024-01-15 09:10:00+00USorganicpro2024-01-16 14:25:00+00NULL
1022024-02-10 11:05:00+00CApaidfreeNULL2024-03-20 10:00:00+00
1032024-03-12 16:35:00+00GBreferralpro2024-03-13 08:15:00+00NULL
1042024-04-01 13:20:00+00USorganicfree2024-04-03 12:00:00+00NULL
EXAMPLE QUERY
SELECT
  id AS user_id,
  MOD(id, 4) AS user_bucket
FROM growth.users
ORDER BY user_id;
RESULT — exact output from the displayed Queryflo rows
user_iduser_bucket
1011
1022
1033
1040

The four user IDs occupy all four modulo buckets.

Intermediate business practice

Sign up free to try it on a real business scenario