ABS and MOD functions in SQL - Time & Space Complexity
We want to understand how the time it takes to run ABS and MOD functions changes as we use them on more data.
How does the work grow when we apply these functions to many rows?
Analyze the time complexity of the following code snippet.
SELECT
ABS(salary) AS absolute_salary,
MOD(employee_id, 10) AS mod_result
FROM employees;
This query calculates the absolute value of salaries and the remainder when employee IDs are divided by 10 for every employee.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying ABS and MOD functions to each row in the employees table.
- How many times: Once for each employee row.
Each row requires a fixed amount of work to compute ABS and MOD. So, as the number of rows grows, the total work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 function calls |
| 100 | 200 function calls |
| 1000 | 2000 function calls |
Pattern observation: The total work grows in a straight line with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows directly with the number of rows processed.
[X] Wrong: "ABS and MOD functions take the same time no matter how many rows there are."
[OK] Correct: Each row needs its own calculation, so more rows mean more total work.
Understanding how simple functions like ABS and MOD scale helps you explain query performance clearly and confidently.
"What if we added a nested query that calls ABS and MOD multiple times per row? How would the time complexity change?"
Practice
ABS(-15) return?Solution
Step 1: Understand the ABS function
The ABS function returns the absolute value, which means it removes any negative sign from a number.Step 2: Apply ABS to -15
Applying ABS to -15 gives 15 because it converts the negative number to positive.Final Answer:
15 -> Option AQuick Check:
ABS(-15) = 15 [OK]
- Thinking ABS changes positive numbers
- Confusing ABS with MOD
- Expecting ABS to return negative values
Solution
Step 1: Identify the correct function for remainder
In standard SQL, the MOD function returns the remainder after division.Step 2: Check syntax correctness
MOD takes two arguments: dividend and divisor, soMOD(17, 5)is correct syntax.Final Answer:
SELECT MOD(17, 5); -> Option AQuick Check:
MOD syntax = MOD(dividend, divisor) [OK]
- Using ABS instead of MOD
- Using % which is not standard SQL
- Using REMAINDER which is not standard SQL
SELECT ABS(-7) + MOD(20, 6) AS result;
Solution
Step 1: Calculate ABS(-7)
ABS(-7) returns 7 because it converts negative to positive.Step 2: Calculate MOD(20, 6)
20 divided by 6 is 3 with remainder 2, so MOD(20, 6) = 2.Step 3: Add the two results
7 + 2 = 9.Final Answer:
9 -> Option DQuick Check:
ABS(-7) + MOD(20, 6) = 9 [OK]
- Adding MOD result incorrectly
- Confusing ABS with MOD
- Ignoring negative sign in ABS
SELECT ABS(10, -5) FROM dual;
Solution
Step 1: Check ABS function argument count
ABS accepts only one argument, but here it has two (10, -5), which is invalid.Step 2: Understand correct usage
To get remainder, MOD should be used, but the question asks about error in ABS usage.Final Answer:
ABS function takes only one argument -> Option BQuick Check:
ABS single argument only [OK]
- Using multiple arguments in ABS
- Confusing ABS with MOD
- Ignoring function argument rules
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?Solution
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.Step 2: Translate conditions into SQL
ABS(value) < 10 ensures absolute value less than 10; MOD(value, 3) = 1 ensures remainder 1.Step 3: Check options
WHERE ABS(value) < 10 AND MOD(value, 3) = 1 matches both conditions exactly with AND.Final Answer:
WHERE ABS(value) < 10 AND MOD(value, 3) = 1 -> Option CQuick Check:
Use AND to combine ABS and MOD conditions [OK]
- Using OR instead of AND
- Mixing up MOD values
- Applying MOD to ABS result incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
