MySQL CEIL() Function
MySQL CEIL() Function: Rounding Up to the Nearest Whole Number
The CEIL() function returns the smallest integer value greater than or equal to a given number. In simple terms, it always rounds fractional numbers upward along the number line.
For positive numbers like 4.12, CEIL() returns 5. For negative numbers like -4.12, rounding upward along the number line moves closer to zero, producing -4.
Syntax
CEIL(x)
x: Any numeric literal, column, or expression.
Practical Example
Suppose demo_orders tracks item weights in kilograms, and your shipping carrier bills each parcel by whole-kilogram tiers rounded up. You can calculate the billable weight tier with CEIL().
SELECT
order_id,
total_weight_kg,
CEIL(total_weight_kg) AS billed_kg,
CEIL(total_weight_kg) * 3.50 AS shipping_fee
FROM demo_orders
WHERE order_id BETWEEN 301 AND 304;
| order_id | total_weight_kg | billed_kg | shipping_fee |
|---|---|---|---|
| 301 | 2.05 | 3 | 10.50 |
| 302 | 4.00 | 4 | 14.00 |
| 303 | 0.45 | 1 | 3.50 |
| 304 | NULL | NULL | NULL |
Order 301 weighs 2.05 kg and jumps to 3 kg. Order 302 is already an exact whole number (4.00), so CEIL() leaves it at 4.
Common Mistakes & NULL Behavior
- Expecting negative numbers to grow in magnitude:
CEIL(-3.8)returns-3, not-4. Moving from-3.8to-3goes upward toward positive infinity. - Integer division before CEIL(): Writing
CEIL(total_items / 10)works in MySQL because/performs decimal division (5 / 10 = 0.5000,CEIL = 1), whereastotal_items DIV 10truncates beforeCEIL()runs. - NULL inputs:
CEIL(NULL)returnsNULL. Wrap optional quantities withCOALESCE()when billing defaults to0.
Frequently Asked Questions
What is the difference between CEIL() and CEILING() in MySQL?
None. CEIL() is an exact alias for CEILING() in MySQL, and both produce identical values and return types.
What return type does CEIL() produce?
For exact-value inputs like DECIMAL or INT, CEIL() returns an exact BIGINT or DECIMAL. For FLOAT or DOUBLE inputs, it returns a DOUBLE.
How do I round up to the nearest 10 instead of 1?
Divide by 10, apply CEIL(), and multiply back: CEIL(price / 10) * 10 turns 42 into 50.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.