// math reference

SQL integer overflow

What happens when integer arithmetic exceeds the type's range, per engine: an error, a silent switch to float, or (for Oracle NUMBER) nothing until 1e126.

카테고리
Math
Returns
integer / float / error
In this category
63 of 82

구문

2147483647 + 1
CAST(x AS BIGINT) + 1

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

엔진 지원

엔진Support메모
SQL Server 예 Arithmetic overflow error: the statement fails; CAST to BIGINT first
PostgreSQL 예 ERROR: integer out of range for int; bigint overflows raise 'bigint out of range'
MySQL Different name or syntax Integer expressions are evaluated as 64-bit BIGINT, so 2147483647 + 1 is fine; past BIGINT it raises 'BIGINT value is out of range'. Clamping happens only when storing into a narrower column in non-strict mode
SQLite Different name or syntax Silently promotes to REAL: 9223372036854775807 + 1 becomes 9.22337203685478e+18, losing precision
Oracle Not built in NUMBER holds 38 digits and overflows only near 1e126 (ORA-01426); PLS_INTEGER in PL/SQL does overflow at 2^31

예시

SQL Server

SELECT CAST(2147483647 AS BIGINT) + 1 AS safe;  -- 2147483648

SQLite

SELECT 9223372036854775807 + 1 AS promoted;  -- 9.22337203685478e+18 (REAL)

MySQL

SELECT 2147483647 + 1 AS fine, 9223372036854775807 + 1 AS boom;  -- ERROR: BIGINT value is out of range

PostgreSQL

SELECT 2147483647 + 1 AS boom;  -- ERROR: integer out of range

알아 두면 좋은 정보

  • SQLite's promotion is the sneakiest: the result looks like a number but is a float, so equality with the true value fails and digits past 15 are gone.
  • The fix is to widen before operating: CAST(a AS BIGINT) * b on SQL Server and PostgreSQL. The operand types decide overflow, not the column the result is stored in. SQL Server's SUM(int_col) returns INT and overflows the same way; use SUM(CAST(x AS BIGINT)).
  • ABS of the smallest bigint overflows even on widened types: there is no positive counterpart two's complement can represent.

Questions about INT overflow

Why does my sum overflow when the column is bigint?

Usually the column is bigint but the expression feeding it is not. SQL Server's SUM over an INT column returns INT and fails past 2147483647; PostgreSQL's SUM(int) returns bigint and MySQL's returns DECIMAL, so they do not. CAST operands to BIGINT before the arithmetic, not after.

Does SQLite overflow integers?

It never errors: the moment an operation exceeds the 64-bit range, SQLite promotes the result to float, silently losing precision past about 15 digits.

How do I avoid integer overflow in SQL?

Widen before computing: CAST to BIGINT or NUMERIC on the operands. SQL Server and PostgreSQL error at the INT boundary, MySQL at the BIGINT boundary, and SQLite silently switches to float past BIGINT. An early cast to NUMERIC/DECIMAL avoids all of it.

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.

// 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.