SQL Keywords

SQL DIV

What is the SQL DIV operator?

DIV performs integer division in MySQL, returning the whole-number quotient and discarding any fractional part.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL DIV: Supported: MySQL, MariaDB. Not supported natively in PostgreSQL, SQL Server, Oracle, SQLite.

SQL DIV Full Explanation

DIV is a MySQL arithmetic operator that executes integer division between two numeric expressions. Unlike the standard / operator, which returns a precise (possibly fractional) result, DIV always truncates toward zero, yielding an integer. If either operand is NULL, the result is NULL. If the divisor is 0, MySQL returns NULL and raises a warning or error depending on sql_mode (e.g., ERROR_FOR_DIVISION_BY_ZERO). Because DIV is evaluated with the same precedence as * and /, parentheses can clarify more complex expressions. The return type is BIGINT for integer operands and DECIMAL if either operand is DECIMAL.

SQL DIV Syntax

<numerator> DIV <denominator>
-- Typical usage inside a query
SELECT <numerator> DIV <denominator> AS quotient;

SQL DIV Parameters

  • numerator (numeric) - dividend in the integer division
  • denominator (numeric) - divisor in the integer division (must not be 0)

Example Queries Using SQL DIV

-- Basic integer division
SELECT 10 DIV 3 AS result;  -- returns 3

-- Using columns
SELECT order_id,
       total_cents DIV 100 AS dollars
FROM   orders;

-- Avoid divide-by-zero
SELECT clicks,
       IF(impressions = 0, 0, clicks DIV impressions) AS ctr_whole
FROM   ads_stats;

Expected Output Using SQL DIV

  • Each query returns an integer quotient
  • Fractional parts are truncated; 10 DIV 3 yields 3
  • If denominator is 0, result is NULL and MySQL issues a warning or error per sql_mode

Use Cases with SQL DIV

  • Converting cents to dollars by dropping cents
  • Calculating whole-number ratios like items per box
  • Paginating records when only full pages matter
  • Avoiding floating-point math where only integers are needed

Common Mistakes with SQL DIV

  • Expecting a fractional result; DIV always truncates.
  • Forgetting divide-by-zero checks.
  • Assuming DIV works in other RDBMS like PostgreSQL or SQL Server.
  • Mixing DIV with / without parentheses, causing precedence confusion.

Related Topics

MOD, %, / (division), FLOOR, CEIL, ROUND, sql_mode

First Introduced In

MySQL 3.23

Frequently Asked Questions

What does the SQL DIV operator do?

DIV performs integer division, returning only the whole-number part of the quotient.

Does DIV round or truncate the result?

It truncates toward zero. For example, 7 DIV 3 returns 2, not 2.33.

What happens if the denominator is zero?

MySQL returns NULL and issues a warning or error depending on sql_mode.

Is DIV available in PostgreSQL or SQL Server?

No. In those systems you can mimic integer division with expressions like FLOOR(numerator / denominator).

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!