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?"