MySQL CEILING() Function
MySQL CEILING() Function: Standard SQL Upward Rounding
The CEILING() function returns the smallest integer greater than or equal to the numeric argument you pass in. It follows the standard SQL function name and works identically to CEIL() in MySQL.
Whenever you need guaranteed upward rounding—such as computing pagination page counts, pallet counts, or hourly billing blocks—CEILING() ensures partial units never get dropped. Exact whole numbers stay unchanged.
Syntax
CEILING(x)
x: Any valid numeric expression, column, or literal.
Practical Example
Suppose demo_products stores stock quantities and box capacities (units_per_box). You want to know how many shipping boxes each product requires when even a single leftover unit needs its own box.
SELECT
product_id,
product_name,
stock_qty,
units_per_box,
CEILING(stock_qty / units_per_box) AS boxes_needed
FROM demo_products
WHERE units_per_box > 0;
| product_id | product_name | stock_qty | units_per_box | boxes_needed |
|---|---|---|---|---|
| 10 | Desk Lamp | 25 | 10 | 3 |
| 11 | Monitor Stand | 40 | 8 | 5 |
| 12 | HDMI Cable | 1 | 20 | 1 |
Even though 25 / 10 is 2.5 and 1 / 20 is 0.05, CEILING() rounds both upward to 3 and 1 full boxes.
Common Mistakes & NULL Behavior
- Confusing CEILING() with ROUND():
ROUND(2.1)returns2, whereasCEILING(2.1)returns3. Only useCEILING()when any positive fraction must bump to the next integer. - Negative input direction:
CEILING(-2.9)evaluates to-2, not-3, because-2is greater than-2.9. UseFLOOR()if you need to round toward negative infinity. - Division by zero inside CEILING(): Passing
CEILING(stock_qty / 0)returnsNULLin non-strict mode (or errors underERROR_FOR_DIVISION_BY_ZERO). PassingNULLdirectly also returnsNULL.
Frequently Asked Questions
Should I use CEIL() or CEILING() in MySQL?
Both work identically in MySQL. CEILING() matches ANSI SQL and PostgreSQL/SQL Server naming, which makes queries slightly easier to port across database engines.
Does CEILING() take a decimal-places parameter like ROUND()?
No. CEILING() accepts only one argument and always rounds to a whole integer. To round up to two decimals, compute CEILING(val * 100) / 100.
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.