ROUND, CEIL, FLOOR in MySQL - Time & Space Complexity
We want to understand how the time it takes to run rounding functions changes as the amount of data grows.
How does using ROUND, CEIL, or FLOOR on many numbers affect performance?
Analyze the time complexity of the following code snippet.
SELECT ROUND(price, 2) AS rounded_price,
CEIL(price) AS ceiling_price,
FLOOR(price) AS floor_price
FROM products;
This query applies rounding functions to each price in the products table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying ROUND, CEIL, and FLOOR to each row's price.
- How many times: Once per row in the products table.
Each row requires the functions to run once, so the total work grows as the number of rows grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 function calls |
| 100 | About 300 function calls |
| 1000 | About 3000 function calls |
Pattern observation: The work increases directly with the number of rows.
Time Complexity: O(n)
This means the time to run grows in a straight line with the number of rows processed.
[X] Wrong: "Rounding functions run instantly no matter how many rows there are."
[OK] Correct: Each row needs its own calculation, so more rows mean more work and more time.
Understanding how simple functions scale with data size helps you explain query performance clearly and confidently.
"What if we added a WHERE clause to filter rows before applying ROUND, CEIL, and FLOOR? How would the time complexity change?"
Practice
Solution
Step 1: Understand the function purpose
ROUND changes a number to the nearest integer or decimal place, unlike CEIL or FLOOR which only round up or down.Step 2: Compare with other functions
CEIL always rounds up, FLOOR always rounds down, TRUNCATE cuts decimals without rounding.Final Answer:
ROUND -> Option BQuick Check:
ROUND = nearest rounding [OK]
- Confusing CEIL with ROUND
- Thinking FLOOR rounds to nearest
- Assuming TRUNCATE rounds
Solution
Step 1: Check function usage
ROUND accepts two arguments: the number and the decimal places to round to.Step 2: Identify correct syntax
ROUND(3.14159, 2) is correct; CEIL and FLOOR do not take decimal places, and ROUND(3.14159. 2) has a syntax error with a dot instead of a comma.Final Answer:
ROUND(3.14159, 2) -> Option AQuick Check:
ROUND(number, decimals) syntax [OK]
- Using CEIL or FLOOR with decimal places
- Using dot instead of comma between arguments
- Missing second argument for ROUND
SELECT CEIL(4.2), FLOOR(4.8), ROUND(4.5);?Solution
Step 1: Evaluate CEIL(4.2)
CEIL rounds up to the next whole number, so CEIL(4.2) = 5.Step 2: Evaluate FLOOR(4.8) and ROUND(4.5)
FLOOR rounds down, so FLOOR(4.8) = 4. ROUND rounds to nearest integer, 4.5 rounds up to 5.Final Answer:
5, 4, 5 -> Option AQuick Check:
CEIL=5, FLOOR=4, ROUND=5 [OK]
- Thinking FLOOR(4.8) is 5
- Rounding 4.5 down instead of up
- Confusing CEIL and FLOOR
SELECT CEIL(5.7, 1), FLOOR(3.2), ROUND(2.718);Solution
Step 1: Check CEIL function arguments
CEIL only accepts one argument (the number). Passing two arguments causes an error.Step 2: Verify FLOOR and ROUND usage
FLOOR accepts one argument, ROUND can accept one or two arguments. Both FLOOR(3.2) and ROUND(2.718) are valid.Final Answer:
CEIL does not accept two arguments -> Option CQuick Check:
CEIL = 1 argument only [OK]
- Passing two arguments to CEIL or FLOOR
- Assuming ROUND always needs two arguments
- Thinking query has no errors
prices with a column cost containing decimal values. You want to create a new column rounded_cost that rounds cost to the nearest integer but always rounds up if the decimal part is exactly 0.5. Which MySQL expression achieves this?Solution
Step 1: Understand rounding rules
In MySQL, ROUND rounds to nearest integer; halfway cases (.5) round away from zero (up for positive numbers).Step 2: Select the correct expression
CEIL(cost) always rounds up, so it rounds up when decimal part is exactly 0.5, fulfilling the requirement.Step 3: Analyze other options
A rounds .5 up normally, but does not always round up if decimal is exactly 0.5 (it rounds to nearest). B rounds down for exactly .5, which is incorrect. D fails on .5 cases as CEIL(cost - 0.5) can round down.Final Answer:
CEIL(cost) -> Option DQuick Check:
CEIL(cost) always rounds up [OK]
- Assuming ROUND always rounds .5 up as needed
- Using conditional that rounds .5 down
- CEIL(cost - 0.5) fails on .5 cases
Ready to prove it?
Pass the test and this pattern moves unknown
