LearnContact
Lesson 4318 min read

Numeric Functions

Learn how to round, floor, ceil, and perform other common calculations on numbers directly inside MySQL queries.

Introduction

Alongside text, most real applications also need to work with numbers — prices, quantities, averages, and scores. MySQL includes a full set of numeric functions so you can perform these calculations directly in your queries, instead of pulling raw numbers into an application first.

In this lesson we will use an "orders" table to explore ROUND(), CEIL(), FLOOR(), ABS(), MOD(), POWER(), and SQRT().

setup.sql
CREATE TABLE orders (
    id INT PRIMARY KEY,
    product_name VARCHAR(50),
    price DECIMAL(10, 2),
    quantity INT
);

INSERT INTO orders (id, product_name, price, quantity) VALUES
(1, 'Notebook', 4.756, 3),
(2, 'Backpack', 29.995, 1),
(3, 'Pen Set', 2.333, 10);
What You Will Learn
  • How to round numbers with ROUND().
  • How to always round up or down with CEIL() and FLOOR().
  • How to get an absolute (non-negative) value with ABS().
  • How to find a remainder with MOD() or the % operator.
  • How to raise numbers to a power and take a square root with POWER() and SQRT().

Why Numeric Functions Matter

Numeric functions are essential for tasks like displaying prices to two decimal places, calculating how many full boxes are needed to pack a given quantity, or working out statistics like an average score. Performing this math inside SQL keeps the calculation close to the data and avoids repeating logic in every application that reads it.

ROUND()

ROUND() rounds a number to a given number of decimal places. If you omit the second argument, it rounds to the nearest whole number.

SELECT price, ROUND(price, 2) AS rounded_price, ROUND(price) AS whole_price
FROM orders;
Result
+--------+---------------+-------------+
| price  | rounded_price | whole_price |
+--------+---------------+-------------+
| 4.756  | 4.76          | 5           |
| 29.995 | 30.00         | 30          |
| 2.333  | 2.33          | 2           |
+--------+---------------+-------------+
Tip

ROUND() also accepts a negative second argument to round to the left of the decimal point, e.g. ROUND(1234, -2) returns 1200.

CEIL() and FLOOR()

CEIL() (also written CEILING()) always rounds a number up to the nearest whole number, and FLOOR() always rounds it down, regardless of the decimal value.

SELECT price, CEIL(price) AS ceiling_value, FLOOR(price) AS floor_value
FROM orders;
Result
+--------+---------------+-------------+
| price  | ceiling_value | floor_value |
+--------+---------------+-------------+
| 4.756  | 5             | 4           |
| 29.995 | 30            | 29          |
| 2.333  | 3             | 2           |
+--------+---------------+-------------+

A common real use is calculating how many boxes are needed to ship a quantity of items when each box holds a fixed amount — CEIL() ensures you always get a whole number of boxes, even for a partially filled last box.

ABS()

ABS() returns the absolute value of a number — its value with the sign removed, so negative numbers become positive.

SELECT ABS(-15) AS abs_negative, ABS(15) AS abs_positive;
Result
+--------------+--------------+
| abs_negative | abs_positive |
+--------------+--------------+
| 15           | 15           |
+--------------+--------------+

This is useful for calculating a difference between two values where you only care about the size of the gap, not which value was larger — for example, comparing an expected quantity to an actual quantity delivered.

MOD() and the % Operator

MOD() returns the remainder after dividing one number by another. The % operator does the exact same thing and can be used interchangeably.

SELECT quantity, MOD(quantity, 3) AS mod_result, quantity % 3 AS percent_result
FROM orders;
Result
+----------+------------+----------------+
| quantity | mod_result | percent_result |
+----------+------------+----------------+
| 3        | 0          | 0              |
| 1        | 1          | 1              |
| 10       | 1          | 1              |
+----------+------------+----------------+
Common Use

MOD() is frequently used to check whether a number is even or odd (MOD(n, 2) = 0 means even), or to detect leftover items when dividing a quantity into equal groups.

POWER() and SQRT()

POWER() raises a number to a given exponent, and SQRT() returns the square root of a number.

SELECT POWER(2, 3) AS two_cubed, SQRT(81) AS square_root_of_81;
Result
+------------+--------------------+
| two_cubed  | square_root_of_81  |
+------------+--------------------+
| 8          | 9                  |
+------------+--------------------+

Reference Table

Here is a quick reference for the numeric functions covered in this lesson.

FunctionPurposeExampleResult
ROUND()Rounds to N decimal placesROUND(4.756, 2)4.76
CEIL()Rounds up to nearest wholeCEIL(4.1)5
FLOOR()Rounds down to nearest wholeFLOOR(4.9)4
ABS()Removes the signABS(-15)15
MOD() / %Returns the remainderMOD(10, 3)1
POWER()Raises to an exponentPOWER(2, 3)8
SQRT()Returns the square rootSQRT(81)9

Worked Example: Discounted Price

Let's calculate a 15% discounted price for every order, a rounded total cost, and check whether the quantity ordered is an even number.

worked_example.sql
SELECT
    product_name,
    price,
    ROUND(price * 0.85, 2) AS discounted_price,
    ROUND(price * quantity, 2) AS total_cost,
    CASE WHEN MOD(quantity, 2) = 0 THEN 'Even' ELSE 'Odd' END AS quantity_type
FROM orders;
Result
+--------------+--------+-------------------+------------+---------------+
| product_name | price  | discounted_price  | total_cost | quantity_type |
+--------------+--------+-------------------+------------+---------------+
| Notebook     | 4.756  | 4.04              | 14.27      | Odd           |
| Backpack     | 29.995 | 25.50             | 30.00      | Odd           |
| Pen Set      | 2.333  | 1.98              | 23.33      | Even          |
+--------------+--------+-------------------+------------+---------------+

This query rounds the raw prices to a clean two-decimal discounted price, calculates a rounded total cost for the full quantity, and uses MOD() to label each order's quantity as even or odd (the CASE expression used here is covered in full in the next lesson).

Common Mistakes

Avoid These Mistakes
  • Confusing ROUND() with FLOOR() or CEIL() — ROUND() can go either up or down, the others always go one direction.
  • Forgetting that CEIL() and FLOOR() return whole numbers, not decimals.
  • Assuming MOD() only works with whole numbers — it also works with decimal values.
  • Not rounding calculated prices before displaying them to users, leading to ugly long decimals.

Best Practices

  • Round monetary values to 2 decimal places before displaying them.
  • Use CEIL() when you need to guarantee "at least this many," such as boxes or pages needed.
  • Use MOD() for even/odd checks and grouping logic rather than writing custom application code.
  • Store prices as DECIMAL rather than FLOAT to avoid tiny rounding errors in financial data.

Frequently Asked Questions

What is the difference between ROUND() and CEIL()/FLOOR()?

ROUND() rounds to the nearest value (up or down, whichever is closer), while CEIL() always rounds up and FLOOR() always rounds down, no matter how close the decimal is.

Can MOD() be used with negative numbers?

Yes, MOD() works with negative numbers, and the sign of the result follows the sign of the first argument (the dividend).

Is there a function for a cube root or other roots besides square root?

MySQL does not have a dedicated cube root function, but you can use POWER(x, 1/3) to calculate it.

Do numeric functions work on columns as well as literal numbers?

Yes — every example in this lesson applies equally to a column reference, a literal number, or an entire expression.

Key Takeaways

  • ROUND() rounds to the nearest value at a chosen decimal precision.
  • CEIL() always rounds up and FLOOR() always rounds down.
  • ABS() strips the sign from a number.
  • MOD() (or %) returns the remainder of a division.
  • POWER() raises a number to an exponent, and SQRT() finds its square root.

Summary

Numeric functions let you perform calculations like rounding, flooring, and remainder checks directly inside your SQL queries, which is especially useful for pricing, quantities, and statistics.

In this lesson, you learned ROUND(), CEIL(), FLOOR(), ABS(), MOD(), POWER(), and SQRT(), and used several of them together to calculate a discounted price. Next, you will explore MySQL's date and time functions.

Lesson 43 Completed
  • You can round, floor, and ceil numeric values.
  • You understand how to find remainders and absolute values.
  • You are ready to move on to date and time functions.
Next Lesson →

Date & Time Functions