Lesson

MySQL CEIL() Function

10 min reading
Free Course

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.8 to -3 goes 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), whereas total_items DIV 10 truncates before CEIL() runs.
  • NULL inputs: CEIL(NULL) returns NULL. Wrap optional quantities with COALESCE() when billing defaults to 0.

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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum