📖 Scenario: You work at an online store that records product prices with many decimal places. You want to prepare a simple report showing prices rounded in different ways for easy reading.
🎯 Goal: Create a MySQL table with product prices, then write queries using ROUND, CEIL, and FLOOR functions to show prices rounded normally, rounded up, and rounded down.
📋 What You'll Learn
Create a table named products with columns id (integer) and price (decimal).
Insert exactly these three rows: (1, 12.345), (2, 7.891), (3, 15.678).
Write a SELECT query that shows id and price_rounded using ROUND(price, 2).
Write a SELECT query that shows id and price_ceil using CEIL(price).
Write a SELECT query that shows id and price_floor using FLOOR(price).
💡 Why This Matters
🌍 Real World
Rounding prices is common in billing systems, reports, and user displays to make numbers easier to read and understand.
💼 Career
Database developers and analysts often use rounding functions to prepare data for reports, dashboards, and financial calculations.
Progress0 / 4 steps
1
Create the products table and insert data
Create a table called products with columns id as INTEGER and price as DECIMAL(5,3). Then insert these rows exactly: (1, 12.345), (2, 7.891), and (3, 15.678).
MySQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Write a query to round prices to 2 decimal places
Write a SELECT query that shows id and a new column price_rounded which uses ROUND(price, 2) to round the price to 2 decimal places from the products table.
MySQL
Hint
Use ROUND(price, 2) in the SELECT clause and alias it as price_rounded.
3
Write a query to get the ceiling of prices
Write a SELECT query that shows id and a new column price_ceil which uses CEIL(price) to round the price up to the nearest integer from the products table.
MySQL
Hint
Use CEIL(price) in the SELECT clause and alias it as price_ceil.
4
Write a query to get the floor of prices
Write a SELECT query that shows id and a new column price_floor which uses FLOOR(price) to round the price down to the nearest integer from the products table.
MySQL
Hint
Use FLOOR(price) in the SELECT clause and alias it as price_floor.
Practice
(1/5)
1. Which MySQL function rounds a number to the nearest whole number or specified decimal place?
easy
A. CEIL
B. ROUND
C. FLOOR
D. TRUNCATE
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.
4. Identify the error in this query: SELECT CEIL(5.7, 1), FLOOR(3.2), ROUND(2.718);
medium
A. ROUND requires two arguments
B. FLOOR requires two arguments
C. CEIL does not accept two arguments
D. No error, query is correct
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 C
Quick Check:
CEIL = 1 argument only [OK]
Hint: CEIL/FLOOR take 1 argument; ROUND can take 1 or 2 [OK]
Common Mistakes:
Passing two arguments to CEIL or FLOOR
Assuming ROUND always needs two arguments
Thinking query has no errors
5. You have a table 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?
hard
A. ROUND(cost)
B. IF(cost - FLOOR(cost) = 0.5, FLOOR(cost), ROUND(cost))
C. CEIL(cost - 0.5)
D. CEIL(cost)
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 D
Quick Check:
CEIL(cost) always rounds up [OK]
Hint: CEIL(cost) always rounds up, including .5 cases [OK]
Common Mistakes:
Assuming ROUND always rounds .5 up as needed
Using conditional that rounds .5 down
CEIL(cost - 0.5) fails on .5 cases
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.