ABS and MOD in MySQL - Time & Space Complexity
We want to understand how the time it takes to run ABS and MOD functions changes as the amount of data grows.
How does the work needed grow when we apply these functions to many numbers?
Analyze the time complexity of the following code snippet.
SELECT ABS(salary) AS absolute_salary, MOD(employee_id, 10) AS remainder
FROM employees;
This code calculates the absolute value of salaries and the remainder when employee IDs are divided by 10 for all employees.
- Primary operation: Applying ABS and MOD functions to each row in the employees table.
- How many times: Once for every employee record (row) in the table.
Each employee row requires two simple calculations. As the number of employees grows, the total work grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 (2 operations x 10 rows) |
| 100 | 200 (2 operations x 100 rows) |
| 1000 | 2000 (2 operations x 1000 rows) |
Pattern observation: The total work grows directly with the number of rows; doubling rows doubles work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of rows processed.
[X] Wrong: "ABS and MOD take the same time no matter how many rows there are because they are simple functions."
[OK] Correct: While each function is simple, they must be applied to every row, so more rows mean more total work.
Understanding how simple functions scale with data size helps you explain query performance clearly and confidently.
"What if we applied ABS and MOD only to a filtered subset of rows? How would that affect the time complexity?"
Practice
ABS function do in MySQL?Solution
Step 1: Understand the purpose of ABS
The ABS function converts any number to its positive equivalent, removing any negative sign.Step 2: Compare with other options
MOD returns remainder, SQRT returns square root, and MAX returns largest value, so they are incorrect.Final Answer:
Returns the positive value of a number -> Option AQuick Check:
ABS(-5) = 5 [OK]
- Confusing ABS with MOD function
- Thinking ABS returns square root
- Assuming ABS finds max value
Solution
Step 1: Identify correct function for remainder
MOD is the MySQL function that returns the remainder after division.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.Final Answer:
SELECT MOD(17, 5); -> Option DQuick Check:
MOD(17, 5) = 2 [OK]
- Using ABS instead of MOD for remainder
- Trying to use '/' operator for remainder
- Using non-existent REMAINDER function
SELECT ABS(-12) AS abs_val, MOD(29, 6) AS mod_val;
Solution
Step 1: Calculate ABS(-12)
ABS returns the positive value, so ABS(-12) = 12.Step 2: Calculate MOD(29, 6)
29 divided by 6 is 4 remainder 5, so MOD(29, 6) = 5.Final Answer:
abs_val = 12, mod_val = 5 -> Option AQuick Check:
ABS(-12) = 12, MOD(29,6) = 5 [OK]
- Forgetting ABS makes negative positive
- Mixing up MOD result with division quotient
- Assuming MOD returns division result
SELECT ABS(-7, 3), MOD(10);
Solution
Step 1: Check ABS function arguments
ABS takes only one argument, but here it has two (-7, 3), which is incorrect.Step 2: Check MOD function arguments
MOD requires two arguments, but only one (10) is provided, so it's missing the second argument.Final Answer:
ABS function has too many arguments; MOD function missing second argument -> Option BQuick Check:
ABS(-7) valid; MOD(10, x) needs two args [OK]
- Passing multiple arguments to ABS
- Using MOD with only one argument
- Assuming MOD can be called like ABS
numbers where the absolute value of the column val divided by 4 leaves a remainder of 1. Which query correctly achieves this?Solution
Step 1: Understand the condition
We want the remainder after dividing the absolute value of val by 4 to be 1.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.Final Answer:
SELECT * FROM numbers WHERE MOD(ABS(val), 4) = 1; -> Option CQuick Check:
MOD(ABS(val), 4) = 1 filters correctly [OK]
- Applying ABS after MOD instead of before
- Using '/' operator for remainder
- Ignoring absolute value in condition
Ready to prove it?
Pass the test and this pattern moves unknown
