Bird
Raised Fist0
SQLquery~10 mins

ABS and MOD functions in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ABS and MOD functions
Input number
↓
ABS
↓
Output result
The flow starts with an input number, then chooses ABS or MOD function. ABS returns the absolute value, MOD returns the remainder of division, then outputs the result.
Execution Sample
SQL
SELECT ABS(-7) AS abs_val, MOD(17, 5) AS mod_val;
This query calculates the absolute value of -7 and the remainder when 17 is divided by 5.
Execution Table
StepFunctionInputCalculationResult
1ABS-7Absolute value of -77
2MOD17, 517 divided by 5 is 3 remainder 22
3END---
💡 All functions processed, query returns results.
Variable Tracker
VariableStartAfter ABSAfter MODFinal
abs_valNULL777
mod_valNULLNULL22
Key Moments - 2 Insights
Why does ABS(-7) return 7 instead of -7?
ABS returns the positive distance from zero, so it converts negative numbers to positive. See execution_table row 1 where ABS(-7) calculates absolute value 7.
What does MOD(17, 5) return and why?
MOD returns the remainder after division. 17 divided by 5 is 3 with remainder 2, so MOD(17,5) returns 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?
A0
B-7
C7
DNULL
💡 Hint
Check the 'Result' column in execution_table row 1.
At which step does the MOD function calculate the remainder?
AStep 2
BStep 1
CStep 3
DNo step
💡 Hint
Look at the 'Function' column in execution_table to find MOD.
If MOD(17, 5) was changed to MOD(20, 6), what would be the new result?
A2
B4
C3
D5
💡 Hint
Calculate remainder of 20 divided by 6; check how MOD works in execution_table row 2.
Concept Snapshot
ABS(number): Returns the absolute (positive) value of number.
MOD(number, divisor): Returns remainder after dividing number by divisor.
Use ABS to remove negative sign.
Use MOD to find leftover after division.
Both return numeric results.
Example: ABS(-7)=7, MOD(17,5)=2.
Full Transcript
This visual execution shows how ABS and MOD functions work in SQL. ABS takes a number and returns its absolute value, turning negatives into positives. MOD takes two numbers and returns the remainder after dividing the first by the second. The example query SELECT ABS(-7), MOD(17,5) returns 7 and 2 respectively. Step-by-step, ABS(-7) calculates absolute value 7, MOD(17,5) calculates remainder 2. Variables abs_val and mod_val track these results. Common confusions include why ABS returns positive and how MOD calculates remainder. The quiz tests understanding of these steps and results.

Practice

(1/5)
1. What does the SQL function ABS(-15) return?
easy
A. 15
B. -15
C. 0
D. Error

Solution

  1. Step 1: Understand the ABS function

    The ABS function returns the absolute value, which means it removes any negative sign from a number.
  2. Step 2: Apply ABS to -15

    Applying ABS to -15 gives 15 because it converts the negative number to positive.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    ABS(-15) = 15 [OK]
Hint: ABS always returns a positive number or zero [OK]
Common Mistakes:
  • Thinking ABS changes positive numbers
  • Confusing ABS with MOD
  • Expecting ABS to return negative values
2. Which of the following is the correct syntax to get the remainder of 17 divided by 5 using SQL?
easy
A. SELECT MOD(17, 5);
B. SELECT REMAINDER(17, 5);
C. SELECT 17 % 5;
D. SELECT ABS(17, 5);

Solution

  1. Step 1: Identify the correct function for remainder

    In standard SQL, the MOD function returns the remainder after division.
  2. Step 2: Check syntax correctness

    MOD takes two arguments: dividend and divisor, so MOD(17, 5) is correct syntax.
  3. Final Answer:

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

    MOD syntax = MOD(dividend, divisor) [OK]
Hint: Use MOD(a, b) to get remainder in SQL [OK]
Common Mistakes:
  • Using ABS instead of MOD
  • Using % which is not standard SQL
  • Using REMAINDER which is not standard SQL
3. What is the result of this SQL query?
SELECT ABS(-7) + MOD(20, 6) AS result;
medium
A. 2
B. 8
C. 14
D. 9

Solution

  1. Step 1: Calculate ABS(-7)

    ABS(-7) returns 7 because it converts negative to positive.
  2. Step 2: Calculate MOD(20, 6)

    20 divided by 6 is 3 with remainder 2, so MOD(20, 6) = 2.
  3. Step 3: Add the two results

    7 + 2 = 9.
  4. Final Answer:

    9 -> Option D
  5. Quick Check:

    ABS(-7) + MOD(20, 6) = 9 [OK]
Hint: Calculate ABS and MOD separately, then add [OK]
Common Mistakes:
  • Adding MOD result incorrectly
  • Confusing ABS with MOD
  • Ignoring negative sign in ABS
4. Identify the error in this SQL query:
SELECT ABS(10, -5) FROM dual;
medium
A. MOD function should be used instead
B. ABS function takes only one argument
C. Missing FROM clause
D. ABS cannot be used with negative numbers

Solution

  1. Step 1: Check ABS function argument count

    ABS accepts only one argument, but here it has two (10, -5), which is invalid.
  2. Step 2: Understand correct usage

    To get remainder, MOD should be used, but the question asks about error in ABS usage.
  3. Final Answer:

    ABS function takes only one argument -> Option B
  4. Quick Check:

    ABS single argument only [OK]
Hint: ABS needs exactly one number inside parentheses [OK]
Common Mistakes:
  • Using multiple arguments in ABS
  • Confusing ABS with MOD
  • Ignoring function argument rules
5. You want to find all rows in a table numbers where the absolute value of value is less than 10 and the remainder when value is divided by 3 is 1. Which SQL WHERE clause is correct?
hard
A. WHERE MOD(ABS(value), 3) = 1 AND value < 10
B. WHERE ABS(value) > 10 OR MOD(value, 3) = 1
C. WHERE ABS(value) < 10 AND MOD(value, 3) = 1
D. WHERE ABS(value) < 10 AND MOD(value, 3) = 0

Solution

  1. Step 1: Understand the conditions

    We want rows where absolute value of 'value' is less than 10 and remainder of 'value' divided by 3 equals 1.
  2. Step 2: Translate conditions into SQL

    ABS(value) < 10 ensures absolute value less than 10; MOD(value, 3) = 1 ensures remainder 1.
  3. Step 3: Check options

    WHERE ABS(value) < 10 AND MOD(value, 3) = 1 matches both conditions exactly with AND.
  4. Final Answer:

    WHERE ABS(value) < 10 AND MOD(value, 3) = 1 -> Option C
  5. Quick Check:

    Use AND to combine ABS and MOD conditions [OK]
Hint: Combine ABS and MOD with AND for multiple conditions [OK]
Common Mistakes:
  • Using OR instead of AND
  • Mixing up MOD values
  • Applying MOD to ABS result incorrectly
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