SQL Tutorial SQL Advanced SQL Database SQL References

MySQL DIV() Function



The MySQL DIV() function is used for integer division where x is divided by y and an integer value is returned. In special cases it returns the following:

  • If the number y is 0, then NULL is returned.
  • If x or y or both are NULL, then NULL is returned.

Syntax

x DIV y

Parameters

x Required. Specify the value that will be divided by y.
y Required. Specify the value that will be divided into x.

Return Value

Returns the integer division result of x divided by y.

Example 1:

The example below shows the usage of DIV() function.

mysql> SELECT 12 DIV 3;
Result: 4

mysql> SELECT 14 DIV 3;
Result: 4

mysql> SELECT 10.8 DIV 3.1;
Result: 3

mysql> SELECT 10.8 DIV -3.1;
Result: -3

mysql> SELECT -10.8 DIV -3.1;
Result: 3

mysql> SELECT 14 DIV 0;
Result: NULL

Example 2:

Consider a database table called Sample with the following records:

Dataxy
Data 1105
Data 2206
Data 3307
Data 4408
Data 5509

To perform the integer division operation, where records of column x is divided by records of column y, the following query can be used:

SELECT *, x DIV y AS DIV_Value FROM Sample;

This will produce the result as shown below:

DataxyDIV_Value
Data 11052
Data 22063
Data 33074
Data 44085
Data 55095

❮ MySQL Functions