MySQL MOD Function: Find Remainder

The MOD() function in MySQL is a mathematical function that returns the remainder of a division operation. In other words, it calculates the modulus of two numbers.

Syntax:

MOD(number, divisor)

Here, number is the number for which to find the modulus, and divisor is the number by which to divide the number.

Alternatively, you can use the % operator to perform the modulus operation:

number % divisor

Examples:

1. Using MOD() Function:

Let's say you want to find the remainder of the division of 10 by 3. You would write:

SELECT MOD(10, 3);

This will return 1, because when 10 is divided by 3, the quotient is 3 and the remainder is 1.

2. Using % Operator:

The same operation can be performed using the % operator:

SELECT 10 % 3;

This will also return 1.

Usage in a Table:

Consider the following orders table:

OrderIDCustomerIDQuantity
11015
210212
31037
410415
51059

If you want to select all orders where the Quantity is odd, you can use the MOD() function to check the remainder of the division of the Quantity by 2:

SELECT *
FROM orders
WHERE MOD(Quantity, 2) = 1;

This will return all rows where Quantity is an odd number:

OrderIDCustomerIDQuantity
11015
31037
410415
51059

Remember that the MOD() function can only be used with numeric data types. If it's used with non-numeric data types, MySQL will attempt to convert the data to numbers before performing the operation.

  1. MySQL MOD Function Example:

    • Description: The MOD function in MySQL is used to find the remainder of a division operation.
    • Example Code:
      SELECT MOD(dividend, divisor) AS remainder
      FROM table_name;
      
  2. How to Use MOD Function in MySQL:

    • Description: Use the MOD function to find the remainder of a division operation in MySQL.
    • Example Code:
      SELECT MOD(15, 4) AS remainder;
      
  3. Finding the Remainder with MOD in MySQL:

    • Description: MOD can be used to find the remainder when one number is divided by another.
    • Example Code:
      SELECT MOD(27, 5) AS remainder;
      
  4. Using MOD for Modulo Arithmetic in MySQL:

    • Description: MOD is commonly used for modulo arithmetic, such as checking if a number is even or odd.
    • Example Code:
      SELECT number, MOD(number, 2) AS is_even
      FROM numbers_table;
      
  5. MOD Function with WHERE Clause in MySQL:

    • Description: Apply the MOD function with a WHERE clause to filter records based on the remainder condition.
    • Example Code:
      SELECT order_id, total_amount
      FROM orders
      WHERE MOD(order_id, 2) = 0; -- Select orders with even order_id
      
  6. Handling Negative Numbers with MOD in MySQL:

    • Description: MOD can handle negative numbers by returning a negative remainder if the dividend is negative.
    • Example Code:
      SELECT MOD(-8, 3) AS negative_remainder;
      
  7. Examples of Using MOD Function in MySQL Queries:

    • Description: Various examples showcasing the application of MOD function in different scenarios.
    • Example Code:
      SELECT product_id, stock_quantity
      FROM products
      WHERE MOD(stock_quantity, 5) = 0; -- Select products with stock quantity divisible by 5
      
      SELECT employee_id, salary
      FROM employees
      WHERE MOD(employee_id, 2) = 1; -- Select employees with odd employee_id
      
  8. Applying MOD to Date and Time Values in MySQL:

    • Description: MOD can be applied to date and time values to extract specific components or patterns.
    • Example Code:
      SELECT order_id, order_date
      FROM orders
      WHERE MOD(MONTH(order_date), 2) = 0; -- Select orders with even month