Difference between MOD and REMAINDER function

In continuation of basics yet important topics of Oracle Database, with this post, I am trying to explain the difference between two similar looking function MOD and REMAINDER. MOD and REMAINDER both are used to calculate the remainder of a numeric value getting divided by another value. Both functions require two parameters: the value to be divided and the divisor. The main difference between these is that the MOD function uses the FLOOR function in its computation logic, and the REMAINDER function uses ROUND. For this reason, the values returned from the two functions can differ.

ngarg> select 25 value, level divisor, 
  2  MOD(25, level) MODULUS, REMAINDER(25, level) REMAINDER from dual
  3  connect by level <= 25;

     VALUE    DIVISOR    MODULUS  REMAINDER
---------- ---------- ---------- ----------
        25          1          0          0
        25          2          1          1
        25          3          1          1
        25          4          1          1
        25          5          0          0
        25          6          1          1
        25          7          4         -3
        25          8          1          1
        25          9          7         -2
        25         10          5          5
        25         11          3          3
        25         12          1          1
        25         13         12         -1
        25         14         11         -3
        25         15         10         -5
        25         16          9         -7
        25         17          8          8
        25         18          7          7
        25         19          6          6
        25         20          5          5
        25         21          4          4
        25         22          3          3
        25         23          2          2
        25         24          1          1
        25         25          0          0

25 rows selected.


Related Links
- ROUND, TRUNC, CEIL and FLOOR in Oracle Database
- Order By and Null values in Oracle Database
- SQL Interview Question Answers
- Order By and Null values in Oracle Database
- Playing With Truncate and Date
- Oracle: Some Important Date Queries
- Prefer Oracle Native Date Arithmetic over ANSI INTERVAL

2 comments:

  1. This is one of those functions you only google when u are debugging a production issue.

    ReplyDelete
  2. Hi Nimish ,

    Can you please help me sql query to get below output. i tried many ways but no luck.

    I have table emp(empno,ename,sal,deptno) and i want output like "list of employee details who's sal is same in diff department".

    eid ename sal deptid eid ename sal deptid
    2 b 200 10 6 g 200 20
    4 d 400 20 8 i 400 30
    5 e 500 20 7 h 500 10


    Thanks
    Sreenu

    ReplyDelete