{"id":40730,"date":"2024-12-11T03:22:12","date_gmt":"2024-12-10T17:22:12","guid":{"rendered":"https:\/\/database.guide\/?p=40730"},"modified":"2024-12-11T03:22:12","modified_gmt":"2024-12-10T17:22:12","slug":"understanding-sqlites-mod-function","status":"publish","type":"post","link":"https:\/\/database.guide\/understanding-sqlites-mod-function\/","title":{"rendered":"Understanding SQLite&#8217;s MOD() Function"},"content":{"rendered":"\n<p class=\"\">The <code>MOD()<\/code> function in SQLite is used to calculate the remainder when one number is divided by another. This operation is commonly known as the modulo operation.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>MOD(numerator, denominator)<\/code><\/pre>\n\n\n\n<p class=\"\">Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><em><code>numerator<\/code><\/em> is the number to be divided.<\/li>\n\n\n\n<li class=\"\"><em><code>denominator<\/code><\/em> is the number by which the numerator will be divided.<\/li>\n<\/ul>\n\n\n\n<p class=\"\">The function returns the remainder of the division of the numerator by the denominator. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 1<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s a simple example to demonstrate:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT 11 \/ 3,\n       MOD(11, 3);<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+------------+<br>| 11 \/ 3 | MOD(11, 3) |<br>+--------+------------+<br>| 3      | 2.0        |<br>+--------+------------+<\/pre>\n\n\n\n<p class=\"\">Here we can see that dividing 11 by 2 equals 3, with a remainder of 2. <\/p>\n\n\n\n<p class=\"\">In other words, 3 multiplied by 3 equals 9, which means we need another 2 in order to get to 11. The 2 part is the &#8220;remainder&#8221;, which the <code>MOD()<\/code> function returns for us. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example 2<\/h2>\n\n\n\n<p class=\"\">Let&#8217;s say we have a table <code>transactions<\/code> with a column <code>amount<\/code>, and we want to find the remainder when the <code>amount<\/code> is divided by 10.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Create the 'transactions' table\nCREATE TABLE transactions (\n    id INTEGER PRIMARY KEY AUTOINCREMENT,\n    amount INTEGER\n);\n\n-- Insert sample data into the 'transactions' table\nINSERT INTO transactions (amount)\nVALUES\n    (23),\n    (57),\n    (88);<\/code><\/pre>\n\n\n\n<p class=\"\">Now let&#8217;s run a query to calculate the remainder when <code>amount<\/code> is divided by 10:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n       amount, \n       amount \/ 10 AS division_result,\n       MOD(amount, 10) AS remainder\nFROM transactions;<\/code><\/pre>\n\n\n\n<p class=\"\">In this example, for each row in the <code>transactions<\/code> table, SQLite will calculate the remainder of the <code>amount<\/code> when divided by 10 and return it as a new column named <code>remainder<\/code>.<\/p>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+-----------------+-----------+<br>| amount | division_result | remainder |<br>+--------+-----------------+-----------+<br>| 23     | 2               | 3.0       |<br>| 57     | 5               | 7.0       |<br>| 88     | 8               | 8.0       |<br>+--------+-----------------+-----------+<\/pre>\n\n\n\n<p class=\"\">Here, for the first row, 23 divided by 10 leaves a remainder of 3, for the second row, 57 divided by 10 leaves a remainder of 7, and for the third row, 88 divided by 10 leaves a remainder of 8.<\/p>\n\n\n\n<p class=\"\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The MOD() function in SQLite is used to calculate the remainder when one number is divided by another. This operation is commonly known as the modulo operation.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[93,70,20],"class_list":["post-40730","post","type-post","status-publish","format-standard","hentry","category-sqlite","tag-functions","tag-mathematical-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/40730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=40730"}],"version-history":[{"count":2,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/40730\/revisions"}],"predecessor-version":[{"id":40736,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/40730\/revisions\/40736"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=40730"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=40730"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=40730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}