Bird
Raised Fist0
MySQLquery~10 mins

ABS and MOD in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ABS and MOD
Start with a number
↓
Get absolute value
↓
Return result
ABS returns the positive value of a number. MOD returns the remainder after division.
Execution Sample
MySQL
SELECT ABS(-7) AS abs_val, MOD(17, 5) AS mod_val;
This query calculates the absolute value of -7 and the remainder of 17 divided by 5.
Execution Table
StepFunctionInputCalculationResult
1ABS-7Remove negative sign7
2MOD17, 517 divided by 5 is 3 remainder 22
3END-All calculations doneQuery returns abs_val=7, mod_val=2
💡 All functions evaluated, results ready to return.
Variable Tracker
VariableStartAfter ABSAfter MODFinal
abs_val-7777
mod_val17, 517, 522
Key Moments - 2 Insights
Why does ABS(-7) return 7 and not -7?
ABS removes the negative sign to give the positive value, as shown in execution_table row 1.
How does MOD(17, 5) calculate the remainder?
MOD divides 17 by 5, finds the quotient 3, then calculates remainder 2, as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of ABS(-7) at step 1?
A7
B-7
C0
D14
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does MOD(17, 5) return the value 2?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Function' and 'Result' columns in execution_table row 2.
If the input to MOD was MOD(20, 6), what would the result be?
A3
B2
C4
D5
💡 Hint
Think about dividing 20 by 6 and finding the remainder.
Concept Snapshot
ABS(num): Returns the positive value of num.
MOD(num, divisor): Returns remainder of num divided by divisor.
Use ABS to remove negative signs.
Use MOD to find remainders after division.
Both return numeric results.
Full Transcript
This lesson shows how ABS and MOD functions work in MySQL. ABS takes a number and returns its positive value by removing any negative sign. MOD takes two numbers, divides the first by the second, and returns the remainder. For example, ABS(-7) returns 7 because it removes the negative sign. MOD(17, 5) returns 2 because 17 divided by 5 is 3 with remainder 2. The execution table traces these steps clearly. Understanding these helps when you want to work with absolute values or find remainders in your database queries.

Practice

(1/5)
1. What does the ABS function do in MySQL?
easy
A. Returns the positive value of a number
B. Returns the remainder of a division
C. Returns the square root of a number
D. Returns the largest number in a column

Solution

  1. Step 1: Understand the purpose of ABS

    The ABS function converts any number to its positive equivalent, removing any negative sign.
  2. Step 2: Compare with other options

    MOD returns remainder, SQRT returns square root, and MAX returns largest value, so they are incorrect.
  3. Final Answer:

    Returns the positive value of a number -> Option A
  4. Quick Check:

    ABS(-5) = 5 [OK]
Hint: ABS always makes numbers positive, no matter the sign [OK]
Common Mistakes:
  • Confusing ABS with MOD function
  • Thinking ABS returns square root
  • Assuming ABS finds max value
2. Which of the following is the correct syntax to get the remainder of 17 divided by 5 in MySQL?
easy
A. SELECT 17 / 5;
B. SELECT ABS(17, 5);
C. SELECT REMAINDER(17, 5);
D. SELECT MOD(17, 5);

Solution

  1. Step 1: Identify correct function for remainder

    MOD is the MySQL function that returns the remainder after division.
  2. Step 2: Check syntax correctness

    MOD takes two arguments: dividend and divisor. '/' operator performs division (17/5 = 3.4), ABS takes one argument, REMAINDER is not a MySQL function.
  3. Final Answer:

    SELECT MOD(17, 5); -> Option D
  4. Quick Check:

    MOD(17, 5) = 2 [OK]
Hint: Use MOD(a, b) to get remainder in MySQL [OK]
Common Mistakes:
  • Using ABS instead of MOD for remainder
  • Trying to use '/' operator for remainder
  • Using non-existent REMAINDER function
3. What is the output of this query?
SELECT ABS(-12) AS abs_val, MOD(29, 6) AS mod_val;
medium
A. abs_val = 12, mod_val = 5
B. abs_val = -12, mod_val = 5
C. abs_val = 12, mod_val = 23
D. abs_val = -12, mod_val = 23

Solution

  1. Step 1: Calculate ABS(-12)

    ABS returns the positive value, so ABS(-12) = 12.
  2. Step 2: Calculate MOD(29, 6)

    29 divided by 6 is 4 remainder 5, so MOD(29, 6) = 5.
  3. Final Answer:

    abs_val = 12, mod_val = 5 -> Option A
  4. Quick Check:

    ABS(-12) = 12, MOD(29,6) = 5 [OK]
Hint: ABS removes sign; MOD gives remainder after division [OK]
Common Mistakes:
  • Forgetting ABS makes negative positive
  • Mixing up MOD result with division quotient
  • Assuming MOD returns division result
4. Identify the error in this MySQL query:
SELECT ABS(-7, 3), MOD(10);
medium
A. Both functions are used correctly
B. ABS function has too many arguments; MOD function missing second argument
C. ABS function missing second argument; MOD function has too many arguments
D. MOD function should be ABS

Solution

  1. Step 1: Check ABS function arguments

    ABS takes only one argument, but here it has two (-7, 3), which is incorrect.
  2. Step 2: Check MOD function arguments

    MOD requires two arguments, but only one (10) is provided, so it's missing the second argument.
  3. Final Answer:

    ABS function has too many arguments; MOD function missing second argument -> Option B
  4. Quick Check:

    ABS(-7) valid; MOD(10, x) needs two args [OK]
Hint: ABS needs 1 arg; MOD needs 2 args [OK]
Common Mistakes:
  • Passing multiple arguments to ABS
  • Using MOD with only one argument
  • Assuming MOD can be called like ABS
5. You want to select all rows from a table numbers where the absolute value of the column val divided by 4 leaves a remainder of 1. Which query correctly achieves this?
hard
A. SELECT * FROM numbers WHERE ABS(MOD(val, 4)) = 1;
B. SELECT * FROM numbers WHERE MOD(val, 4) = 1;
C. SELECT * FROM numbers WHERE MOD(ABS(val), 4) = 1;
D. SELECT * FROM numbers WHERE ABS(val) / 4 = 1;

Solution

  1. Step 1: Understand the condition

    We want the remainder after dividing the absolute value of val by 4 to be 1.
  2. Step 2: Analyze each option

    SELECT * FROM numbers WHERE MOD(val, 4) = 1; uses val directly, ignoring absolute value. SELECT * FROM numbers WHERE MOD(ABS(val), 4) = 1; correctly applies ABS(val) inside MOD. SELECT * FROM numbers WHERE ABS(MOD(val, 4)) = 1; applies ABS after MOD, which can give wrong results if val is negative. SELECT * FROM numbers WHERE ABS(val) / 4 = 1; uses '/' which performs division, not remainder.
  3. Final Answer:

    SELECT * FROM numbers WHERE MOD(ABS(val), 4) = 1; -> Option C
  4. Quick Check:

    MOD(ABS(val), 4) = 1 filters correctly [OK]
Hint: Apply ABS inside MOD to get remainder of positive value [OK]
Common Mistakes:
  • Applying ABS after MOD instead of before
  • Using '/' operator for remainder
  • Ignoring absolute value in condition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard