✏️ Explanatory Question
Level: Intermediate — Used for calculations, rounding, and formatting numeric output in reports.
Numeric functions perform mathematical operations on numeric values and return a number. They are essential for financial calculations, statistics, and formatting results.
%).ROUND(3.567, 2) gives 3.57 (rounds), while TRUNCATE(3.567, 2) gives 3.56 (simply chops off). Don't confuse the two in financial calculations.
| Function | Example | Result |
|---|---|---|
| ROUND() | ROUND(3.567, 2) |
3.57 |
| CEIL() | CEIL(4.1) |
5 |
| FLOOR() | FLOOR(4.9) |
4 |
| TRUNCATE() | TRUNCATE(3.567, 2) |
3.56 |
| MOD() | MOD(10, 3) |
1 |
| ABS() | ABS(-25) |
25 |
| POWER() | POWER(2, 3) |
8 |
| SQRT() | SQRT(16) |
4 |
-- Round prices to 2 decimal places
SELECT product, ROUND(price, 2) AS price_rounded FROM products;
-- Find even vs odd IDs using MOD
SELECT id, IF(MOD(id, 2) = 0, 'Even', 'Odd') AS parity FROM users;
-- Ceiling and floor for pagination math
SELECT CEIL(105 / 10) AS total_pages; -- 11 pages for 105 rows
-- Random sample of 5 employees
SELECT * FROM employees ORDER BY RAND() LIMIT 5;
-- Compound calculation
SELECT POWER(1.05, 10) AS growth_factor; -- 5% growth over 10 periods