MySQL DIV Function

 

The DIV function is used in MySQL for integer division, which means the result will be the quotient without the remainder (i.e., it rounds down the result to the nearest integer).


Syntax

x DIV y

Definition and Usage

  • The DIV function performs integer division between two numbers, returning the integer part of the division (the quotient) and discarding the remainder.
  • Note: The result of the division is an integer, so any fractional part is ignored.

Parameter Values

Parameter Description
x Required. The dividend (the number to be divided).
y Required. The divisor (the number that divides x).

Technical Details

  • Works in: From MySQL 4.0
  • Result: Returns the integer quotient after dividing x by y.

Examples

Example 1: Integer division of 10 by 5

SELECT 10 DIV 5;

Result:

10 DIV 5: 2

Explanation: 10 ÷ 5 = 2. The result is an integer 2, with no remainder.


Example 2: Integer division of 8 by 3

SELECT 8 DIV 3;

Result:

8 DIV 3: 2

Explanation: 8 ÷ 3 = 2.666..., but the fractional part is discarded, and the result is 2.


Example 3: Integer division with negative numbers

SELECT -8 DIV 3;

Result:

-8 DIV 3: -3

Explanation: -8 ÷ 3 = -2.666..., but since it is integer division, the result is rounded down to -3 (toward negative infinity).


Usage Notes

  • The DIV operator is helpful when you need to perform division and only care about the integer part of the result, ignoring any remainder or fractional part.
  • It behaves similarly to FLOOR(x/y) but specifically for division operations.

This function is useful for scenarios like computing the number of full groups or chunks when dividing a quantity, and you don't need the decimal or fractional values.


Was this article helpful?