SQL Tutorial › Functions & Grouping › Numeric Functions

SQL Numeric Functions

IntermediateLesson 27 of 699 min read7 queries run on MySQL 8.4

Reports are full of arithmetic: rounding averages, working out percentages, splitting items into boxes, finding even ids. MySQL’s numeric functions handle all of it in the query. This lesson covers the ones you will use weekly and the integer-division trap that catches many people.

Advertisement
In this lesson
  • Round with ROUND, TRUNCATE, CEIL and FLOOR
  • Use MOD, DIV, ABS, POWER and SQRT
  • Calculate percentages without rounding errors
  • Know how / behaves in MySQL

Quick reference

MySQL
SELECT ROUND(1234.567, 2)    AS round_2,       ROUND(1234.567, -2)   AS round_hundreds,       TRUNCATE(1234.567, 1) AS truncate_1,       CEIL(4.1)             AS ceil_,       FLOOR(4.9)            AS floor_,       ABS(-15)              AS abs_,       MOD(17, 5)            AS mod_,       17 DIV 5              AS int_div,       17 / 5                AS division,       POWER(2, 10)          AS power_,       SQRT(144)             AS sqrt_;
ResultMySQL 8.4
round_2round_hundredstruncate_1ceil_floor_abs_mod_int_divdivisionpower_sqrt_
1234.5712001234.55415233.4000102412
1 row in set
How this query works, step by step
  1. ROUND(x, 2) rounds to 2 decimals; a negative precision rounds to tens, hundreds…: 1,200.
  2. TRUNCATE(x, 1) cuts after one decimal without rounding: 1234.5.
  3. CEIL rounds up to the next whole number, FLOOR down.
  4. MOD(17, 5) is the remainder (2) and 17 DIV 5 the whole-number quotient (3).
  5. / always returns a decimal in MySQL: 3.4000.

ROUND an average

MySQL
SELECT dept_id,       AVG(salary)           AS raw_avg,       ROUND(AVG(salary), 1) AS avg_1dp,       ROUND(AVG(salary))    AS avg_wholeFROM employeesWHERE dept_id IS NOT NULLGROUP BY dept_idORDER BY dept_id;
ResultMySQL 8.4
dept_idraw_avgavg_1dpavg_whole
178400.000078400.078400
269000.000069000.069000
366500.000066500.066500
478000.000078000.078000
4 rows in set
How this query works, step by step
  1. AVG returns an exact decimal with 4 places.
  2. ROUND(..., 1) keeps one decimal; ROUND(x) with no precision rounds to a whole number.
  3. Exact values ending in .5 round away from zero, so 63,500.5 would become 63,501.

CEIL: how many boxes?

Each box holds 24 units. How many boxes does each product’s stock need?

MySQL
SELECT product_name, stock,       CEIL(stock / 24) AS boxes_needed,       MOD(stock, 24)   AS units_in_last_boxFROM productsWHERE stock > 0;
ResultMySQL 8.4
product_namestockboxes_neededunits_in_last_box
Wireless Mouse12050
Mechanical Keyboard45221
Office Chair12112
Standing Desk515
Notebook Pack3001312
Gel Pens2501110
Desk Lamp3026
Monitor Arm18118
8 rows in set
How this query works, step by step
  1. stock / 24 gives a fraction, such as 120 / 24 = 5 or 45 / 24 = 1.875.
  2. CEIL rounds up, because a partly filled box is still a box.
  3. MOD(stock, 24) shows how many units go in the last box (0 means it is full).

Percentages

Each department’s share of the total payroll:

MySQL
SELECT dept_id,       SUM(salary) AS payroll,       ROUND(100 * SUM(salary) / (SELECT SUM(salary) FROM employees), 1) AS pct_of_totalFROM employeesGROUP BY dept_idORDER BY payroll DESC;
ResultMySQL 8.4
dept_idpayrollpct_of_total
139200037.0
NULL25000023.6
220700019.5
313300012.5
4780007.4
5 rows in set
How this query works, step by step
  1. The subquery (SELECT SUM(salary) FROM employees) is the company total: 1,060,000.
  2. Each department’s payroll is multiplied by 100 before dividing, which keeps precision.
  3. ROUND(..., 1) formats the percentage.
Note

In MySQL / never truncates: 7 / 2 is 3.5000. In SQL Server, PostgreSQL and SQLite, dividing two integers drops the fraction (7 / 2 = 3), so write 7 * 1.0 / 2 there.

MOD: even and odd

MySQL
SELECT emp_id, first_name,       CASE WHEN MOD(emp_id, 2) = 0 THEN 'even' ELSE 'odd' END AS id_typeFROM employeesWHERE dept_id = 2;
ResultMySQL 8.4
emp_idfirst_nameid_type
105Vikramodd
106Nehaeven
107Arjunodd
3 rows in set

Every other row, a common interview question:

MySQL
SELECT emp_id, first_nameFROM employeesWHERE MOD(emp_id, 2) = 0;
ResultMySQL 8.4
emp_idfirst_name
100Meera
102Rahul
104Karan
106Neha
108Kavya
110Sneha
6 rows in set

ABS, SIGN and differences

How far is each salary from 75,000, regardless of direction?

SQL
SELECT first_name, salary,       salary - 75000       AS difference,       ABS(salary - 75000)  AS distance,       SIGN(salary - 75000) AS above_or_belowFROM employeesWHERE dept_id IN (1, 4)ORDER BY distance;
ResultMySQL 8.4
first_namesalarydifferencedistanceabove_or_below
Priya72000-30003000-1
Karan72000-30003000-1
Sneha78000300030001
Rahul9800023000230001
Asha12500050000500001
Amit25000-5000050000-1
6 rows in set

Random numbers

RAND() returns a random number from 0 to 1. Pick one random product (fine for small tables; slow on large ones because every row is sorted):

MySQL
SELECT product_name FROM products ORDER BY RAND() LIMIT 1;

A random whole number from 1 to 6, like a dice roll: FLOOR(1 + RAND() * 6).

Exercise: Price with tax

Show each product’s price, the 18% GST amount rounded to 2 decimals, and the total price rounded to a whole number.

Expected resultMySQL 8.4
product_namepricegstprice_with_gst
Wireless Mouse799143.82943
Mechanical Keyboard3499629.824129
USB-C Hub1999359.822359
Office Chair89991619.8210619
Standing Desk189993419.8222419
Notebook Pack24944.82294
Gel Pens19935.82235
Desk Lamp1499269.821769
Monitor Arm2799503.823303
9 rows in set
Hint 1

ROUND(price * 0.18, 2) and ROUND(price * 1.18).

Quick quiz

1. What does ROUND(1234.567, -2) return?

2. What is CEIL(2.01)?

3. What does 7 / 2 return in MySQL?

4. How do you get the remainder of 17 divided by 5?

Numeric functions FAQ

How do I round to 2 decimal places in SQL?

Use ROUND(value, 2). To cut without rounding, use TRUNCATE(value, 2) in MySQL.

What is the difference between CEIL and FLOOR?

CEIL (or CEILING) rounds up to the nearest whole number; FLOOR rounds down.

Why does integer division give a decimal in MySQL?

MySQL’s / operator always returns a decimal result. Use DIV for whole-number division, or FLOOR(a / b).

How do I calculate a percentage in SQL?

Multiply by 100 before dividing and round the result: ROUND(100 * part / total, 1).

Leave a Comment