Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
LPAD() Function in MySQL
Next article icon

MOD() Function in MySQL

Last Updated : 24 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 :

Remainder 
0

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. 

Remainder
-1

Example-3: Finding the remainder of 27 when it is divided by 4 using the modulus operator(%).

SELECT 27 % 4 AS Remainder;

Output:

Remainder 
3

Example-4: Finding the remainder of a floating number using MOD Function.

SELECT 10.15 MOD 3  AS Remainder;

Output :

Remainder 
1.15

Example-5: Finding the remainder of a  number using  MOD Function when the divisor is 0.

SELECT MOD( 6, 0) AS Remainder;

Output :

Remainder
NULL

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      | +--------------+---------------+----------------+-----------+


Next Article
LPAD() Function in MySQL
author
jana_sayantan
Improve
Article Tags :
  • SQL
  • DBMS-SQL
  • mysql

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
  • MONTH() function in MySQL
    MySQL MONTH() function returns the month from the given date. It returns a month value between 1 and 12 or returns 0 when the month part of the date is 0. It's a useful SQL function for date manipulation and analysis, particularly when dealing with reports or queries that require month-based groupin
    3 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
  • LN() Function in MySQL
    LN() function : It is the function in MySQL is used to calculate the natural logarithm of a specific number with base e . The number must be greater than 0, otherwise it will return NULL. Syntax : LN(X) Parameter : LN() function accepts one parameter as mentioned above in the syntax and described be
    2 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
  • LOG2() Function in MySQL
    In this article, we are going to cover the LOG2() function that means it will calculate the logarithm of a specific number with base 2. Pre-requisite :LOG function LOG2() function in MySQL is used to calculate the natural logarithm of a specific number with base 2. The number must be >0 Otherwise
    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
  • LOG10() Function in MySQL
    LOG10() function in MySQL is used to calculate the natural logarithm of a specific number with base 10. The number must be greater than 0, otherwise it will return NULL. Syntax : LOG10(X) Parameter : This method accepts one parameter as mentioned above in the syntax and described below : X - A numbe
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences