The MOD() function in MySQL is used to find the remainder of one number divided by another. The MOD() function returns the remainder of dividend divided by divisor. if the divisor is zero, it returns NULL.
Syntax:
MOD(N, M)
or
N % M
or
N MOD M
Parameter : MOD() function accepts two parameter as mentioned above and described below.
- N –The dividend i.e. a number or a numeric expression that will be divided by M.
- M –The divisor i.e. a number or a numeric expression by which to divide the dividend.
Returns : It returns the remainder of dividend divided by divisor.
Example-1 : Finding the remainder of 36 when it is divided by 6 using MOD Function.
SELECT MOD( 36, 6) AS Remainder;
Output :
Example-2 : Finding the remainder of -10 when it is divided by 3 using MOD Function.
SELECT MOD(-10, 3) AS Remainder;
Output : The MOD() function can also be used with negative numbers. In this case, the result will be negative if the dividend is negative, and positive if the dividend is positive.
Example-3: Finding the remainder of 27 when it is divided by 4 using the modulus operator(%).
SELECT 27 % 4 AS Remainder;
Output:
Example-4: Finding the remainder of a floating number using MOD Function.
SELECT 10.15 MOD 3 AS Remainder;
Output :
Example-5: Finding the remainder of a number using MOD Function when the divisor is 0.
SELECT MOD( 6, 0) AS Remainder;
Output :
Example-6 : MOD Function can also be used to find the Remainder values for the column data. In this example, we are going to find whether a student has appeared a total odd number of exams or even with the help of the MOD function. To demonstrate create a table named Student.
CREATE TABLE Student
(
Student_id INT AUTO_INCREMENT,
Student_name VARCHAR(100) NOT NULL,
Student_Class VARCHAR(20) NOT NULL,
TotalExamGiven INT NOT NULL,
PRIMARY KEY(Student_id )
);
Now inserting some data to the Student table is as follows:
INSERT INTO Student
(Student_name, Student_Class, TotalExamGiven )
VALUES
('Sayan', 'IX', 8 ),
('Nitin', 'X', 5 ),
('Aniket', 'XI', 6 ),
('Abdur', 'X', 7 ),
('Riya', 'IX', 4 ),
('Jony', 'X', 10 ),
('Deepak', 'X', 7 ),
('Ankana', 'XII', 5 ),
('Shreya', 'X', 8 ) ;
So, the Student Table is as follows:
mysql> SELECT * FROM Student;
+------------+--------------+---------------+----------------+
| Student_id | Student_name | Student_Class | TotalExamGiven |
+------------+--------------+---------------+----------------+
| 1 | Sayan | IX | 8 |
| 2 | Nitin | X | 5 |
| 3 | Aniket | XI | 6 |
| 4 | Abdur | X | 7 |
| 5 | Riya | IX | 4 |
| 6 | Jony | X | 10 |
| 7 | Deepak | X | 7 |
| 8 | Ankana | XII | 5 |
| 9 | Shreya | X | 8 |
+------------+--------------+---------------+----------------+
9 rows in set (0.00 sec)
Now, we are going to find whether a student has appeared total odd number of exam or even.
SELECT
Student_name,
Student_Class,
TotalExamGiven,
IF(MOD(TotalExamGiven, 2),
'Odd','Even')
OddOrEven FROM Student ;
Output:
+--------------+---------------+----------------+-----------+
| Student_name | Student_Class | TotalExamGiven | OddOrEven |
+--------------+---------------+----------------+-----------+
| Sayan | IX | 8 | Even |
| Nitin | X | 5 | Odd |
| Aniket | XI | 6 | Even |
| Abdur | X | 7 | Odd |
| Riya | IX | 4 | Even |
| Jony | X | 10 | Even |
| Deepak | X | 7 | Odd |
| Ankana | XII | 5 | Odd |
| Shreya | X | 8 | Even |
+--------------+---------------+----------------+-----------+
Similar Reads
MID() function in MySQL
MID() : This function in MySQL is used to extract a substring from a given input string. If the starting position is a positive number, then the substring of the given length will be extracted from the starting index. If negative, then the substring of the given length will be extracted from the end
2 min read
LOG() Function in MySQL
LOG() function in MySQL is used to calculate the natural logarithm of a specific number. The number must be >0 Otherwise it will return NULL. Syntax : LOG(X) Parameter : This method accepts one parameter as mentioned above and described below : X : A number whose logarithm value we want to calcul
3 min read
LPAD() Function in MySQL
LPAD() function in MySQL is used to pad or add a string to the left side of the original string. Syntax : LPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below - str - The actual string which is to be padded. If the length of the original str
2 min read
MySQL | MD5 Function
The MySQL MD5 function is used to return an MD5 128-bit checksum representation of a string. The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. The value returned by the MD5 function is a binary string of 32 hexadecimal digits, or NULL if the argument was
1 min read
LTRIM() Function in MySQL
LTRIM() : This function in MySQL is used to remove leading spaces from a string. Syntax : LTRIM(str) Parameter : It accepts one parameter as mentioned above and described below as follows. str â The string from which we want to remove leading spaces. Returns : It returns a string after truncating al
2 min read
MAKEDATE() function in MySQL
MAKEDATE() : This function in MySQL is used to create and return a date based on a year and a number of days value. The number of days must be greater than 0 otherwise it returns a NULL value. Syntax : MAKEDATE(year, day) Parameter : This function accepts two parameters as given below as follows. ye
2 min read
LEFT() Function in MySQL
The LEFT() function in MySQL is used to extract a specified number of characters from the left side of a given string. It uses its second argument to decide, how many characters it should return. Syntax: LEFT (str, len)Parameter: This function accepts two parameters as mentioned above and described
1 min read
MINUTE() Function in MySQL
MINUTE() function : This function in MySQL is used to return the minutes part for a specified time or date time value. The range of minute value is from 0 to 59. For example, if the specified time is "09:12:23", this function will return 12 minute. Syntax : MINUTE(datetime) Parameter : This method a
1 min read
MySQL IF( ) Function
The MySQL IF() function is a control flow function that returns different values based on the result of a condition. IF() Function in MySQLThe IF() function in MySQL returns a value if the condition is TRUE and another value if the condition is FALSE. The MySQL IF() function can return values that c
2 min read
MICROSECOND() Function in MySQL
MICROSECOND() : This function in MySQL is used to return the microsecond part for a specified time or date-time value. The range of microsecond value is from 0 to 999999. For example, if the specified time is â09:12:23.000123â, this function will return 123 microseconds. Syntax : MICROSECOND(datetim
2 min read