MOD()

SQL · Math Functions

Theory

MOD returns the remainder left over after dividing one whole number by another. MOD(10, 3) is 1 because 10 = 3 × 3 + 1, MOD(12, 4) is 0 because 4 divides 12 exactly, and MOD(id, 2) is 0 for even ids and 1 for odd ids. Many databases also write it as the % operator: 10 % 3.

Remainders are surprisingly useful in SQL. MOD(id, 2) splits rows into even and odd; MOD(id, 3) deals rows into three repeatable buckets for an A/B/C test or for spreading work across three batch jobs; WHERE MOD(id, 10) = 0 keeps every tenth row as a quick, stable sample; and MOD(year, 4) = 0 is the first step of a leap-year check. Spellings differ: PostgreSQL, MySQL and Snowflake support both MOD() and %, Oracle has MOD() only, SQL Server has % only, and SQLite always supports %. With negative numbers the sign of the result usually follows the dividend, so -7 % 3 is -1, not 2.

Why we use it

Remainders give a simple, repeatable way to split, cycle or sample rows, and to test divisibility. Unlike RANDOM, the same id always lands in the same bucket.

When to use it

Use MOD for even/odd checks, round-robin buckets, every-Nth-row samples and divisibility rules. Use RANDOM when you want a fresh random sample each time, and FLOOR(x / n) when you need the quotient rather than the remainder.

How it works

  1. Dividend and divisor, e.g. (10, 3)
  2. Is the divisor zero? Yes: Error or NULL — division by zero. No: Continue.
  3. Divide and drop the whole-number part of the result
  4. Return the remainder (1)
  5. Use it to bucket, filter or test divisibility

Syntax

MOD(dividend, divisor)

Example

SELECT MOD(10, 3);
-- 1

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic