SQL MOD
The function-call form of the % operator — MOD(a, b) does exactly the same job as a % b.
What & Why
MOD(a, b) is functionally identical to a % b — both return the remainder after division. This is the same pattern seen throughout this track: an operator and an equivalent function existing side by side, differing only in syntax.
See How It Works
BUSINESS QUESTION
Growth wants users assigned to one of four deterministic review buckets.
| id | created_at | country | channel | plan | activated_at | churned_at |
|---|---|---|---|---|---|---|
| 101 | 2024-01-15 09:10:00+00 | US | organic | pro | 2024-01-16 14:25:00+00 | NULL |
| 102 | 2024-02-10 11:05:00+00 | CA | paid | free | NULL | 2024-03-20 10:00:00+00 |
| 103 | 2024-03-12 16:35:00+00 | GB | referral | pro | 2024-03-13 08:15:00+00 | NULL |
| 104 | 2024-04-01 13:20:00+00 | US | organic | free | 2024-04-03 12:00:00+00 | NULL |
EXAMPLE QUERY
SELECT
id AS user_id,
MOD(id, 4) AS review_bucket
FROM growth.users
ORDER BY user_id;RESULT — exact output from the displayed Queryflo rows
| user_id | review_bucket |
|---|---|
| 101 | 1 |
| 102 | 2 |
| 103 | 3 |
| 104 | 0 |
MOD assigns the displayed users to deterministic review buckets.
Now You Try
Practice this concept
Growth wants users assigned to one of four deterministic review buckets.
Available schema
growthPrefix tables with growth.table_name.
user_idreview_bucketgrowth.users| Column | Type |
|---|---|
| id | integer |
| created_at | timestamp with time zone |
| country | text |
| channel | text |
| plan | text |
| activated_at | timestamp with time zone |
| churned_at | timestamp with time zone |
| legacy_user_code | text |
query.sql
Sign up free to try it on a real business scenario