// math reference

SQL % modulo operator

Returns the remainder of a division: 7 % 3 is 1, the same math as MOD(), written as an operator on four of the five engines.

Categoria
Math
Returns
integer (decimal where supported)
In this category
48 of 82

Sintaxe

7 % 3
dividend % divisor

Each block is a separate form. The support table and the examples show which engine takes which.

Suporte a mecanismos

MotorSupportNotas
SQL Server Sim % accepts integer, money and decimal operands (7.5 % 2 is 1.5); float operands raise an error
PostgreSQL Sim % is an operator; mod(n, d) also exists
MySQL Sim % and MOD(n, d) are interchangeable
SQLite Sim % casts both operands to integers (7.5 % 2 is 1.0); mod(n, d) on 3.35+ keeps fractions (1.5)
Oracle Different name or syntax No % operator; the MOD function is the only spelling

Exemplos

SQL Server

SELECT 7 % 3 AS remainder, 7.5 % 2 AS dec_rem;  -- 1, 1.5

Oracle

SELECT MOD(7, 3) AS remainder FROM dual;  -- 1; Oracle has no %

PostgreSQL

SELECT id % 2 = 0 AS is_even
FROM rows;

SQLite

SELECT 7.5 % 2 AS op, mod(7.5, 2) AS fn;  -- 1.0, 1.5

Bom saber

  • Fractions differ by engine: 7.5 % 2 is 1.5 on SQL Server, PostgreSQL and MySQL, but 1.0 on SQLite, whose % truncates both operands to integers first. SQL Server rejects float operands outright.
  • A zero divisor raises an error on SQL Server and PostgreSQL; MySQL and SQLite return NULL; Oracle's MOD(7, 0) returns 7.
  • The result takes the sign of the dividend on all five engines: -7 % 3 is -1, and Oracle's MOD(-7, 3) is -1 too.

Questions about %

Is % the same as MOD?

Yes wherever both exist: MySQL, PostgreSQL and SQLite accept both spellings with identical results. SQL Server only has %, Oracle only has MOD().

Does % work on decimals?

On SQL Server, PostgreSQL and MySQL, yes: 7.5 % 2 is 1.5. SQL Server refuses float operands ('The data types float and int are incompatible in the modulo operator'), and SQLite's % truncates to integers, so use mod() there.

What happens with a zero divisor?

SQL Server and PostgreSQL raise a division-by-zero error; MySQL and SQLite return NULL; Oracle's MOD(x, 0) returns x. Guard with NULLIF(d, 0) to get NULL everywhere.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.