MySQL
ORACLE
POSTGRESQL
SQL
SQL SERVER

Understanding and Using the MOD Function in SQL

intro

Let’s explore the MOD function in SQL and see how and when to use it to perform the mathematical modulo operation in your database.

Tools used in the tutorial
Tool Description Link
Dbvisualizer DBVISUALIZER
TOP RATED DATABASE MANAGEMENT TOOL AND SQL CLIENT

When developing applications, the required mathematical operations usually go beyond the basic four (i.e., addition +, subtraction , multiplication *, and division /). No surprise, programming languages provide many other useful operations, including the powerful modulo operator.

In computing, the modulo operator is as important as the basic arithmetic operators. The same applies to databases, where the MOD function in SQL plays a key role in performing modulus operations.

Read this article to learn everything you need to know about the SQL modulo operations using MOD or the % operator (which is the same character used as a wildcard in SQL LIKE operator).

Let’s dive in!

What Is the SQL MOD Function?

In SQL, the MOD function is a mathematical function that returns the remainder of a division operation. In other words, it performs the modulo operation (hence the name “MOD”) known in computing and mathematics.

For example, MOD(11, 3) returns 2 because 11 divided by 3 equals 3 with a remainder of 2. The MOD function in SQL is particularly useful for detecting repetition, alignment, or offsets. Essentially, you should use it when you need to check if a number fits a repeating pattern or cycle.

Common MOD SQL function use cases include:

  • Checking even/odd numbers (MOD(n, 2) = 0 for even).
  • Grouping data into cycles (e.g., scheduling tasks every 7 days with MOD(day_number, 7)).
  • Distributing records evenly across partitions or servers (e.g., MOD(user_id, number_of_partitions)).
  • Extracting periodic patterns in time-series data (e.g., finding the day of week with MOD(day_number, 7)).

How to Use the MOD Function in SQL

The MOD operation is part of the SQL standard, meaning most databases implement it in the same way. However, some specific DBMSs may deviate from the standard and use a custom implementation. Thus, it is worth reviewing how the SQL MOD function works in various database management systems, including MySQL, PostgreSQL, SQL Server, and Oracle.

MOD in MySQL

The MySQL MOD function follows this syntax:

Copy
        
1 MOD(N, M)

It returns the remainder of N divided by M, where N and M can be either integers or floating-point values.

Notes:

  • If N or M is NULL, MOD(N, M) returns NULL.
  • MOD(N, M) is equivalent to N % M or N MOD M.
  • MOD is safe to use with BIGINT values.
  • MOD(N, 0) returns NULL.
  • It works with fractional values and returns the exact remainder (e.g., SELECT MOD(24.2, 8) returns 0.2).
  • The same syntax and notes apply to MariaDB. (Learn more about the two DBMS systems in our MariaDB vs MySQL comparison.)

MOD in PostgreSQL

The Postgres modulo function uses the following syntax:

Copy
        
1 MOD(y, x)

It returns the remainder of y / x, where x and y can be smallint, integer, bigint, or numeric PostgreSQL data types.

Notes:

  • If x or y is NULL, MOD(y, x) returns NULL.
  • MOD(y, x) is equivalent to y % x.
  • MOD(y, 0) raises an “ERROR: division by zero” error.
  • In case of fractional values, it returns the exact remainder (e.g., SELECT MOD(37.5, 9) returns 1.5).

MOD in SQL Server

SQL Server does not provide a built-in MOD function. Attempting to use it will result in the error:

Copy
        
1 'MOD' is not a recognized built-in function name.

Specifically, the SQL Server MOD function equivalent is the % operator (also called “Modulus” or “modulo operator”). Its syntax is:

Copy
        
1 dividend % divisor

It returns the remainder of one number divided by another. Both dividend and divisor must be valid expressions of integer, monetary, or numeric data types.

MOD in Oracle

You can call the Oracle MOD function like this:

Copy
        
1 MOD(n2, n1)

This returns the remainder of n2 divided by n1. If n1 is 0, it returns n2.

This function takes as arguments any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

Notes:

  • The function accepts any numeric data type or any non-numeric data type that can be implicitly converted to a numeric type.
  • Oracle identifies the argument between n2 and n1 with the highest numeric precedence. Then, it automatically converts the other arguments to that type and returns a result of that same data type.
  • If the product of n1* and n2* is negative, the Oracle MOD function behaves differently from the mathematical modulus function. To replicate the mathematical behavior, you must apply this formula instead: n2 - n1 * FLOOR(n2/n1).

SQL Modulo Function: Complete Example

Note: The following example is built around the MySQL MOD function, but you can easily adapt it to PostgreSQL or Oracle. The queries will be executed in a multi-database client like DbVisualizer, though you are free to use your preferred database client.

Assume you are a college instructor and want to assign all students enrolled in your class (ID 101, title “Computer Science”) into three groups in a round-robin fashion. You have:

  • A students table containing student information.
  • An enrollments table containing student-class enrollment details.

Each student in the class is assigned a sequential, incremental enrollment_number based on their enrollment order.

You can verify the assumptions with this query:

Copy
        
1 SELECT 2 student_id, 3 s.full_name, 4 e.enrollment_number 5 FROM students S 6 JOIN enrollments E ON S.id = E.student_id 7 WHERE E.class_id = 101 8 ORDER BY enrollment_number;

Execute the above query in DbVisualizer on your database with college info:

Executing the initial query in DbVisualizer
Executing the initial query in DbVisualizer

The enrollment_number column in the resulting table shows that students have incremental enrollment numbers for class 101.

Now, the MOD function in SQL allows you to distribute students evenly into three groups:

  • Result 0 → Group 1
  • Result 1 → Group 2
  • Result 2 → Group 3

To implement this logic, add a calculated column called group_number using the SQL MOD function:

Copy
        
1 SELECT 2 student_id, 3 s.full_name, 4 e.enrollment_number, 5 MOD(e.enrollment_number - 1, 3) + 1 AS group_number 6 FROM students S 7 JOIN enrollments E ON S.id = E.student_id 8 WHERE E.class_id = 101 9 ORDER BY enrollment_number;

The -1 ensures that the first enrollment number (1) maps to 0 in MOD. Adding +1 converts it to start at Group 1.

Running the query in DbVisualizer:

Note the loop of group numbers
Note the loop of group numbers

See how it assigns students evenly across Group 1, Group 2, and Group 3, cycling through the groups repeatedly as desired.

Awesome! Mission complete.

As an extra, suppose you want to see the full names of students in each group. To achieve that, you can use a GROUP BY query with a nested subquery:

Copy
        
1 SELECT 2 SG.group_number, 3 GROUP_CONCAT(SG.full_name SEPARATOR ', ') AS student_names 4 FROM ( 5 SELECT 6 student_id, 7 s.full_name, 8 e.enrollment_number, 9 MOD(e.enrollment_number - 1, 3) + 1 AS group_number 10 FROM students S 11 JOIN enrollments E ON S.id = E.student_id 12 WHERE E.class_id = 101 13 ORDER BY enrollment_number 14 ) SG 15 GROUP BY SG.group_number 16 ORDER BY SG.group_number;

The MySQL GROUP_CONCAT function concatenates all student names in each group into a single comma-separated string.

This time, the results will be:

Visualizing the students in each group
Visualizing the students in each group

You now have a complete list of students for each group, evenly distributed using the SQL MOD function.

Thanks to DbVisualizer’s SQL editor autocomplete, writing complex queries gets easier. You do not need to remember all column names or aliases. Just press Ctrl+Space (Command(⌘)+Space on macOS) to access autocomplete and get contextual tips while building your query, as shown below:

The autocomplete feature in action in DbVisualizer
The autocomplete feature in action in DbVisualizer

Conclusion

In this blog post, you learned more about the MOD function in SQL. Specifically, you saw how it allows you to perform the modulo operator within SQL queries. You also discovered how to use it in MySQL, PostgreSQL, SQL Server, and Oracle.

As shown here, DbVisualizer simplifies writing and executing SQL queries. It supports over 50 databases and offers powerful features like SQL formatting, ER diagrams, and query optimization tools. Download DbVisualizer for free today!

FAQ

Is MOD in SQL part of the standard?

Yes, the MOD function has been part of the ANSI/ISO SQL standard since SQL:1999 (formerly known as SQL3). In detail, it is included as part of the optional ANSI/ISO SQL feature T441. Still, not all databases support it. For example, the SQL Server MOD function does not exist.

Is there a modulo operator in SQL?

Yes, the modulo operator (MOD in SQL:1999, also available as the % alias in SQL:2011) is part of the standard ANSI SQL. This means that most databases provide and support it as one of their standard mathematical operators.

What is the difference between the modulo operator in SQL and the MOD function?

In SQL, the MOD function and the modulo operator % perform the same basic operation: they calculate the modulus of two numbers, which is the remainder of a division. Typically, MOD and % are interchangeable, with one that may be serving as a shorthand for the other, depending on the SQL dialect.

Which databases support the MOD SQL function?

DatabaseMOD supportNotes
MySQLFully supported with numeric and fractional values.
PostgreSQLSupports integers and numeric types; fractional remainders allowed.
SQL ServerUse the % operator instead
OracleAccepts numeric and convertible types; returns the highest precedence type.
MariaDBCompatible with MySQL syntax.

What is the difference between the modulo function in SQL and the modulus function in SQL?

There is no real difference between the “modulo function” and the “modulus function” in SQL. Both expressions refer to the same operation, which is getting the remainder of a division between two numbers.

Dbvis download link img
About the author
Antonello Zanini

Antonello is a software engineer, and often refers to himself as a technology bishop. His mission is to spread knowledge through writing.

The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

Ensuring HIPAA Compliance in a Changing Data Landscape

author Lukas Vileikis tags SQL 5 min 2026-08-24
title

What Is a Composite Key in SQL and When to Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2026-08-17
title

SQL Server Full-Text Search: A Practical Guide

author Antonello Zanini tags Full text search SQL SERVER 11 min 2026-08-10
title

Best Practices for Using Git with Your Database

author Lukas Vileikis tags SQL 6 min 2026-07-27
title

Top 10 Features in DbVisualizer Not Supported by phpMyAdmin

author Lukas Vileikis tags MARIADB MySQL 6 min 2026-07-20
title

Setting Up MySQL HeatWave: A Guided Tutorial

author Lukas Vileikis tags MySQL 4 min 2026-06-29
title

Understanding SQL Index Maintenance in Open Source Databases

author Lukas Vileikis tags SQL 6 min 2026-06-22
title

INSERT INTO … SELECT Statement: What You Need to Know

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2026-06-15
title

Parsing Data with SUBSTRING_INDEX: A Complete Guide

author Lukas Vileikis tags MARIADB MySQL SQL 5 min 2026-06-08
title

SQL DROP TABLE Statement: Everything You Need To Know

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2026-06-01

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.