Mathematical functions (ROUND, CEIL, FLOOR, ABS) in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to run mathematical functions changes as we use them on more data.
How does the work grow when applying functions like ROUND, CEIL, FLOOR, or ABS to many numbers?
Analyze the time complexity of the following code snippet.
SELECT
ROUND(price, 2) AS rounded_price,
CEILING(discount) AS ceil_discount,
FLOOR(tax) AS floor_tax,
ABS(balance) AS absolute_balance
FROM sales_data;
This code applies four mathematical functions to each row in the sales_data table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying each mathematical function to every row in the table.
- How many times: Once per row, repeated for all rows in sales_data.
Each row requires a fixed amount of work to compute these functions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 40 (4 functions x 10 rows) |
| 100 | 400 (4 functions x 100 rows) |
| 1000 | 4000 (4 functions x 1000 rows) |
Pattern observation: The total work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run these functions grows in a straight line as the number of rows increases.
[X] Wrong: "Mathematical functions like ROUND or ABS take longer as the numbers get bigger."
[OK] Correct: These functions do a fixed amount of work per number regardless of its size, so the time depends only on how many numbers you process, not their values.
Understanding how simple functions scale with data size helps you explain query performance clearly and confidently.
"What if we applied these functions inside a nested query that runs multiple times? How would the time complexity change?"
Practice
ABS function do in PostgreSQL?Solution
Step 1: Understand the ABS function
The ABS function returns the absolute value, which means it converts any number to its positive form.Step 2: Compare with other functions
ROUND changes decimals, CEIL and FLOOR deal with integers, so they do not return absolute values.Final Answer:
Returns the positive value of a number -> Option DQuick Check:
ABS(-5) = 5 [OK]
- Confusing ABS with ROUND
- Thinking ABS changes decimal places
- Mixing ABS with CEIL or FLOOR
Solution
Step 1: Recall ROUND function syntax
ROUND takes two arguments: the number and the number of decimal places as an integer.Step 2: Check each option
ROUND(3.14159, 2) uses correct syntax ROUND(number, decimals). Others have syntax errors or wrong argument types.Final Answer:
ROUND(3.14159, 2) -> Option AQuick Check:
ROUND(3.14159, 2) = 3.14 [OK]
- Using dot instead of comma between arguments
- Passing decimals as string
- Using 'TO' keyword incorrectly
SELECT CEIL(-3.7), FLOOR(-3.7), ROUND(-3.7), ABS(-3.7);Solution
Step 1: Calculate CEIL and FLOOR for -3.7
CEIL(-3.7) is the smallest integer >= -3.7, which is -3. FLOOR(-3.7) is the largest integer <= -3.7, which is -4.Step 2: Calculate ROUND and ABS
ROUND(-3.7) rounds to nearest integer, which is -4. ABS(-3.7) returns positive value 3.7.Final Answer:
(-3, -4, -4, 3.7) -> Option CQuick Check:
CEIL(-3.7) = -3, FLOOR(-3.7) = -4, ROUND(-3.7) = -4, ABS(-3.7) = 3.7 [OK]
- Mixing CEIL and FLOOR results for negative numbers
- Assuming ROUND always truncates
- Forgetting ABS returns positive
SELECT ROUND(5.678, '1');Solution
Step 1: Check ROUND function arguments
ROUND expects the second argument to be an integer specifying decimal places, not a string.Step 2: Validate other options
ROUND works with decimals, FROM clause is optional in SELECT without tables, and ROUND can take two arguments.Final Answer:
The second argument should be an integer, not a string -> Option BQuick Check:
ROUND(5.678, 1) is correct, ROUND(5.678, '1') causes error [OK]
- Passing decimals as quoted strings
- Thinking FROM is mandatory for simple SELECT
- Assuming ROUND only takes one argument
Solution
Step 1: Calculate price with tax
Multiply price by 1.075 (100% + 7.5%) to get total price including tax.Step 2: Round to 2 decimal places
Use ROUND function with second argument 2 to round to two decimals.Step 3: Check other functions
CEIL and FLOOR do not accept decimals argument, ABS is for absolute value, so they are incorrect here.Final Answer:
ROUND(123.4567 * 1.075, 2) -> Option AQuick Check:
ROUND(123.4567 * 1.075, 2) = 132.72 [OK]
- Using CEIL or FLOOR with decimal places argument
- Using ABS instead of ROUND for rounding
- Forgetting to multiply by 1.075 for tax
Ready to prove it?
Pass the test and this pattern moves unknown
