Basics · Lesson 18 of 19

Math Functions

SQL has built-in math functions for calculations.

ROUND(number, decimals) - round to N decimal places:

SELECT ROUND(salary / 12.0, 2) AS monthly_pay
FROM employees;

ABS(number) - absolute value (remove negative sign):

SELECT ABS(balance) FROM accounts;

Modulo (%) - remainder after division:

SELECT id, id % 2 AS is_odd FROM employees;
-- 0 = even, 1 = odd

Arithmetic in SELECT:

SELECT salary, salary * 1.10 AS with_raise
FROM employees;

You can combine math with other clauses:

SELECT department_id, ROUND(AVG(salary), 0) AS avg_salary
FROM employees GROUP BY department_id;
Check your understanding

Which function rounds a number to 2 decimal places?

Examples run on the employees sample database. Open any of them in the playground to experiment.