ROUND, CEIL, FLOOR in SQL - Time & Space Complexity
We want to understand how the time it takes to run ROUND, CEIL, and FLOOR functions changes as the amount of data grows.
How does the execution time grow when these functions are applied to many rows?
Analyze the time complexity of the following code snippet.
SELECT
ROUND(price, 2) AS rounded_price,
CEIL(quantity) AS ceil_quantity,
FLOOR(discount) AS floor_discount
FROM sales_data;
This query applies ROUND, CEIL, and FLOOR functions to columns in each row of the sales_data table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying each function (ROUND, CEIL, FLOOR) once per row.
- How many times: Once for every row in the sales_data table.
Each row requires a fixed number of function calls, so the total work grows as the number of rows grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 30 (3 functions x 10 rows) |
| 100 | 300 (3 functions x 100 rows) |
| 1000 | 3000 (3 functions x 1000 rows) |
Pattern observation: The total operations increase 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: "These functions take longer on bigger numbers or decimals, so time grows faster than the number of rows."
[OK] Correct: Each function runs in constant time per row regardless of the number size; only the number of rows affects total time.
Understanding how simple functions scale with data size helps you explain query performance clearly and confidently.
"What if we added a nested subquery that also uses ROUND on each row? How would the time complexity change?"
Practice
CEIL(4.3) return?Solution
Step 1: Understand CEIL function behavior
The CEIL function rounds a number up to the nearest integer.Step 2: Apply CEIL to 4.3
Since 4.3 is between 4 and 5, CEIL rounds it up to 5.Final Answer:
5 -> Option AQuick Check:
CEIL(4.3) = 5 [OK]
- Confusing CEIL with FLOOR
- Thinking CEIL rounds to nearest integer
- Assuming CEIL returns decimal part
Solution
Step 1: Recall ROUND function syntax
ROUND(number, decimal_places) is the correct syntax in SQL.Step 2: Check each option
Only ROUND(123.456, 2) uses ROUND with a comma separating number and decimal places.Final Answer:
ROUND(123.456, 2) -> Option BQuick Check:
ROUND(number, decimals) syntax [OK]
- Using dot instead of comma
- Using semicolon instead of comma
- Adding extra keywords like TO
SELECT FLOOR(7.9), CEIL(7.1), ROUND(7.5) FROM dual;Solution
Step 1: Evaluate FLOOR(7.9)
FLOOR rounds down to 7.Step 2: Evaluate CEIL(7.1)
CEIL rounds up to 8.Step 3: Evaluate ROUND(7.5)
ROUND rounds to nearest integer; 7.5 rounds up to 8.Final Answer:
(7, 8, 8) -> Option DQuick Check:
FLOOR=7, CEIL=8, ROUND=8 [OK]
- Mixing FLOOR and CEIL results
- Assuming ROUND always rounds down
- Confusing ROUND behavior at .5
SELECT CEIL(5.7 2) FROM dual;Solution
Step 1: Check CEIL function syntax
CEIL takes only one argument, a number, with no second argument.Step 2: Analyze given query
The query has '5.7 2' without a comma, which is invalid syntax.Final Answer:
Missing comma between arguments -> Option CQuick Check:
CEIL needs one number, no extra arguments [OK]
- Adding extra arguments to CEIL
- Forgetting commas between arguments
- Assuming CEIL rounds decimals differently
prices with a column cost containing decimal values. You want to create a new column cost_rounded that rounds cost to the nearest integer but always rounds .5 up. Which SQL expression should you use?Solution
Step 1: Understand rounding .5 up requirement
Standard ROUND rounds .5 up, but behavior can vary by SQL dialect.Step 2: Analyze options
FLOOR(cost + 0.5) uses FLOOR(cost + 0.5), which shifts values so .5 rounds up correctly.Step 3: Verify other options
ROUND(cost) may round .5 up but depends on implementation; B and D do not correctly round .5 up.Final Answer:
FLOOR(cost + 0.5) -> Option AQuick Check:
Use FLOOR(x + 0.5) to round .5 up reliably [OK]
- Assuming ROUND always rounds .5 up
- Using CEIL directly without adjustment
- Using FLOOR without adding 0.5
Ready to prove it?
Pass the test and this pattern moves unknown
