0
0
SQLquery~5 mins

ROUND, CEIL, FLOOR in SQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: ROUND, CEIL, FLOOR
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
1030 (3 functions x 10 rows)
100300 (3 functions x 100 rows)
10003000 (3 functions x 1000 rows)

Pattern observation: The total operations increase directly with the number of rows.

Final Time Complexity

Time Complexity: O(n)

This means the time to run these functions grows in a straight line as the number of rows increases.

Common Mistake

[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.

Interview Connect

Understanding how simple functions scale with data size helps you explain query performance clearly and confidently.

Self-Check

"What if we added a nested subquery that also uses ROUND on each row? How would the time complexity change?"