Lesson

MySQL DIV Operator

10 min reading
Free Course

MySQL DIV Operator: Performing Integer Division in SQL

In MySQL, DIV is a binary operator—not a function—that performs integer division between two numbers. You write a DIV b, and MySQL discards any fractional remainder toward zero.

Unlike the regular / operator, which returns a DECIMAL or DOUBLE with fractional digits, DIV always produces a whole integer. It pairs naturally with MOD when splitting totals into full groups and remainders.

Syntax

dividend DIV divisor
  • dividend: Left-hand numeric expression.
  • divisor: Right-hand numeric expression. Do NOT write DIV(a, b) with parentheses.

Practical Example

Suppose demo_orders tracks item quantities ordered, and your warehouse packs 12 units per full carton. Using DIV alongside MOD shows both full cartons and loose items in one query.

SELECT
    order_id,
    quantity,
    quantity DIV 12 AS full_cartons,
    quantity MOD 12 AS loose_items
FROM demo_orders
WHERE order_id BETWEEN 601 AND 604;
order_id quantity full_cartons loose_items
601 29 2 5
602 24 2 0
603 7 0 7
604 NULL NULL NULL

Order 601 has 29 units: 29 DIV 12 gives 2 full cartons, and 29 MOD 12 leaves 5 loose units.

Common Mistakes & NULL Behavior

  • Calling DIV() like a function: Writing SELECT DIV(10, 3) causes a MySQL syntax error. Always place DIV between the two operands: SELECT 10 DIV 3.
  • Division by zero: Evaluating 10 DIV 0 returns NULL in permissive SQL modes. However, if ERROR_FOR_DIVISION_BY_ZERO is active during INSERT or UPDATE in strict mode, MySQL raises an error. Guard variable divisors with NULLIF(divisor, 0).
  • Negative number truncation: DIV truncates toward zero, not negative infinity. For example, -7 DIV 2 returns -3, whereas FLOOR(-7 / 2) returns -4.
  • NULL propagation: If either operand is NULL, DIV evaluates to NULL.

Frequently Asked Questions

What is the difference between DIV and / in MySQL?

The / operator performs exact decimal or floating-point division (7 / 2 returns 3.5000), while DIV discards the fraction and returns an integer (7 DIV 2 returns 3).

Does DIV work on decimal operands?

Yes. MySQL converts operands to DECIMAL before dividing and drops the fractional result. For instance, 10.8 DIV 2.5 returns 4.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum