4️⃣ 🔧 MySQL Functions & Expressions
Estimated reading: 4 minutes 263 views

MySQL Numeric Functions – Powerful Arithmetic & Math Operations


Introduction – Why Use Numeric Functions in MySQL?

Databases do more than just store numbers—they help you calculate, analyze, and transform numeric data on the fly. MySQL’s built-in numeric functions enable developers to perform arithmetic, rounding, modular math, trigonometry, and statistical operations directly in SQL.

These functions are essential in financial systems, e-commerce platforms, billing engines, and real-time analytics dashboards.

In this guide, you’ll learn:

  • Most used MySQL numeric functions with syntax
  • How to perform rounding, absolute value, percentages, and power math
  • Practical real-world examples
  • Best practices for numeric precision and performance

Overview of MySQL Numeric Function Categories

CategoryExamples
ArithmeticABS(), ROUND(), CEIL(), FLOOR()
ModulusMOD(), %
ExponentiationPOW(), EXP(), SQRT()
RandomizationRAND()
TrigonometrySIN(), COS(), TAN(), PI()
LogarithmicLOG(), LOG10(), LN()

Common MySQL Numeric Functions with Examples


1️⃣ ABS() – Absolute Value

SELECT ABS(-15); -- Output: 15

2️⃣ ROUND() – Round a Number

SELECT ROUND(12.789, 2); -- Output: 12.79

Useful for rounding prices, percentages, and averages.


3️⃣ CEIL() – Round Up to Nearest Integer

SELECT CEIL(4.3); -- Output: 5

4️⃣ FLOOR() – Round Down to Nearest Integer

SELECT FLOOR(4.7); -- Output: 4

5️⃣ MOD() – Return Remainder (Modulus)

SELECT MOD(17, 4); -- Output: 1

Can also use the % operator:

SELECT 17 % 4; -- Output: 1

6️⃣ POW() / POWER() – Exponentiation

SELECT POW(2, 3); -- Output: 8

7️⃣ SQRT() – Square Root

SELECT SQRT(49); -- Output: 7

8️⃣ RAND() – Generate Random Number

SELECT RAND(); -- Output: e.g., 0.483921

Generate random number between two bounds:

SELECT FLOOR(1 + (RAND() * 10)); -- 1 to 10

9️⃣ SIGN() – Returns the Sign of a Number

SELECT SIGN(-10); -- Output: -1
SELECT SIGN(0);   -- Output: 0
SELECT SIGN(20);  -- Output: 1

🔟 TRUNCATE() – Cut off decimal digits

SELECT TRUNCATE(123.4567, 2); -- Output: 123.45

Advanced: Trigonometric & Logarithmic Functions

FunctionDescriptionExampleOutput
PI()Returns π (pi)SELECT PI();3.141593
SIN()Sine of angle (radian)SELECT SIN(PI()/2);1.0
COS()CosineSELECT COS(0);1.0
TAN()TangentSELECT TAN(1);~1.557
LOG()Natural log (base e)SELECT LOG(2.71828);~1
LOG10()Log base 10SELECT LOG10(1000);3
EXP()Exponential (e^x)SELECT EXP(1);2.718281828

Real-World Use Cases


💵 1. Round prices to 2 decimal places

SELECT ROUND(price, 2) AS rounded_price FROM products;

2. Calculate percentage of sales per region

SELECT region, 
  ROUND((SUM(sales) / (SELECT SUM(sales) FROM sales)) * 100, 2) AS percentage
FROM sales
GROUP BY region;

🎲 3. Randomly pick a promotion winner

SELECT * FROM users ORDER BY RAND() LIMIT 1;

4. Calculate square roots or powers

SELECT name, SQRT(score) AS sqrt_score FROM assessments;

Best Practices for Numeric Functions

Best Practice Tip
Use ROUND() instead of FORMAT()ROUND() is numeric; FORMAT() returns a string
Avoid RAND() on large datasetsUse cautiously—can cause performance issues in ORDER BY
Use integer math for percentagesMultiply first, then divide to avoid floating point errors
Avoid truncation for monetary valuesPrefer ROUND() to maintain accuracy

Summary – Recap & Next Steps

MySQL numeric functions allow you to calculate, format, randomize, and evaluate numbers efficiently within SQL queries. Whether you’re handling prices, analytics, physics, or predictions—these functions help you deliver reliable and precise results.

Key Takeaways

  • Use ROUND(), CEIL(), FLOOR() to handle numeric rounding and thresholds
  • Use MOD(), POW(), SQRT() for mathematical operations
  • RAND() is great for randomness but costly on big tables
  • Always ensure decimal precision for currency and financial calculations

Real-World Relevance

Numeric functions are vital in pricing systems, billing logic, discounts, statistics, data analytics, and machine learning pipelines stored in SQL-based warehouses.


FAQ – MySQL Numeric Functions


What’s the difference between ROUND() and TRUNCATE()?

  • ROUND() rounds up or down.
  • TRUNCATE() simply cuts off extra digits without rounding.

How do I generate a random number between 1 and 100?

SELECT FLOOR(1 + (RAND() * 100));

Is MOD() the same as %?

Yes. Both return the remainder from a division operation.

SELECT 9 % 4; -- 1
SELECT MOD(9, 4); -- 1

Can I use numeric functions inside SELECT, WHERE, or ORDER BY?

Absolutely. You can use them anywhere in SQL expressions.


What’s the best function for rounding currency values?

Use ROUND(value, 2) for accurate financial rounding to 2 decimal places.


Share Now :
Share

🔢 MySQL Numeric Functions

Or Copy Link

CONTENTS
Scroll to Top