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:
Data | x | y |
---|---|---|
Data 1 | 10 | 5 |
Data 2 | 20 | 6 |
Data 3 | 30 | 7 |
Data 4 | 40 | 8 |
Data 5 | 50 | 9 |
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:
Data | x | y | DIV_Value |
---|---|---|---|
Data 1 | 10 | 5 | 2 |
Data 2 | 20 | 6 | 3 |
Data 3 | 30 | 7 | 4 |
Data 4 | 40 | 8 | 5 |
Data 5 | 50 | 9 | 5 |
❮ MySQL Functions