Lesson 43 of 60 – Numeric Functions
72%

Numeric Functions in SQL

Numeric functions are SQL functions used to perform calculations and operations on numeric values. They are useful for working with prices, fees, marks, salaries, quantities, percentages, and other numbers.

Note: Numeric function names and available functions can vary between database systems. The examples in this lesson mainly use commonly supported SQL functions and MySQL syntax where noted.

1. What are Numeric Functions?

Numeric functions perform mathematical operations on numeric values.

They can be used to:

  • Round numbers
  • Find absolute values
  • Calculate powers
  • Find remainders
  • Generate random numbers
  • Calculate square roots
  • Perform mathematical calculations

2. Common Numeric Functions

Some commonly used numeric functions are:

Function Purpose
ROUND() Rounds a number
CEIL() Rounds a number upward
FLOOR() Rounds a number downward
ABS() Returns the absolute value
MOD() Returns the remainder
POWER() Calculates a power
SQRT() Calculates the square root
RAND() Generates a random value

3. ROUND() Function

The ROUND() function rounds a number to the specified number of decimal places.

SELECT ROUND(125.678, 2);

Result:

125.68

4. ROUND() Without Decimal Places

You can use ROUND() without specifying the number of decimal places.

SELECT ROUND(125.678);

Result:

126

5. ROUND() with a Column

ROUND() can be used with numeric columns.

SELECT
    name,
    ROUND(fee, 2) AS rounded_fee
FROM students;

This displays the fee rounded to two decimal places.

6. CEIL() Function

In MySQL, CEIL() returns the smallest integer greater than or equal to the given number.

SELECT CEIL(12.3);

Result:

13

7. FLOOR() Function

The FLOOR() function returns the largest integer less than or equal to the given number.

SELECT FLOOR(12.9);

Result:

12

8. ABS() Function

The ABS() function returns the absolute value of a number.

SELECT ABS(-25);

Result:

25

ABS() removes the negative sign from a negative number.

9. ABS() with a Column

ABS() can be useful when calculating differences or values that should be treated as positive.

SELECT
    student_id,
    ABS(paid_fee - total_fee) AS fee_difference
FROM student_fees;

This returns the absolute difference between paid and total fees.

10. MOD() Function

The MOD() function returns the remainder after division.

SELECT MOD(10, 3);

Result:

1

10 divided by 3 leaves a remainder of 1.

11. MOD() for Even and Odd Numbers

MOD() can be used to determine whether a number is even or odd.

SELECT MOD(10, 2);

A result of 0 means the number is even.

SELECT MOD(11, 2);

A result of 1 means the number is odd.

12. POWER() Function

The POWER() function calculates one number raised to the power of another.

SELECT POWER(2, 3);

Result:

8

Because 2 × 2 × 2 = 8.

13. POW() Function

In MySQL, POW() can also be used to calculate powers.

SELECT POW(5, 2);

Result:

25

14. SQRT() Function

The SQRT() function returns the square root of a number.

SELECT SQRT(64);

Result:

8

15. SIGN() Function

In MySQL, SIGN() returns the sign of a number.

SELECT SIGN(-10);

Result:

-1
SELECT SIGN(10);

Result:

1
SELECT SIGN(0);

Result:

0

16. RAND() Function

In MySQL, RAND() generates a pseudo-random floating-point value between 0 and 1.

SELECT RAND();

Each execution can produce a different value.

17. RAND() with a Seed

RAND() can receive a seed value.

SELECT RAND(10);

Using the same seed can produce a repeatable sequence in MySQL.

18. TRUNCATE() Function

In MySQL, the TRUNCATE() function cuts a number to a specified number of decimal places without rounding.

SELECT TRUNCATE(125.678, 2);

Result:

125.67

This is different from ROUND(), which would round the value.

19. GREATEST() Function

In MySQL, GREATEST() returns the largest value from the supplied arguments.

SELECT GREATEST(10, 25, 15);

Result:

25

20. LEAST() Function

In MySQL, LEAST() returns the smallest value from the supplied arguments.

SELECT LEAST(10, 25, 15);

Result:

10

21. Numeric Functions with WHERE

Numeric functions can be used in filtering conditions.

SELECT *
FROM students
WHERE ROUND(fee, 0) > 5000;

This filters records based on the rounded fee value.

22. Numeric Functions with ORDER BY

Numeric functions can also be used when sorting results.

SELECT name, fee
FROM students
ORDER BY ROUND(fee, 0) DESC;

This sorts students according to the rounded fee.

23. Numeric Functions with Aggregate Functions

Numeric functions can be combined with aggregate functions.

SELECT ROUND(AVG(marks), 2) AS average_marks
FROM students;

This calculates the average marks and rounds the result to two decimal places.

24. Calculating Percentage

Numeric expressions can be used to calculate percentages.

SELECT
    name,
    marks,
    ROUND((marks / 100) * 100, 2) AS percentage
FROM students;

If the maximum marks are 100, the marks themselves represent the percentage.

25. Calculating Discount

Numeric calculations are useful for calculating discounts.

SELECT
    product_name,
    price,
    discount,
    ROUND(price - (price * discount / 100), 2) AS final_price
FROM products;

This calculates the final price after applying the discount percentage.

26. Calculating Remaining Fee

Numeric expressions can be used to calculate remaining fees.

SELECT
    student_id,
    total_fee,
    paid_fee,
    ROUND(total_fee - paid_fee, 2) AS remaining_fee
FROM student_fees;

This calculates the amount that is still due.

27. Common Numeric Function Mistake

A common mistake is confusing rounding with truncation.

SELECT ROUND(12.678, 2);

Result: 12.68

SELECT TRUNCATE(12.678, 2);

Result: 12.67 in MySQL.

ROUND() changes the value according to rounding rules, while TRUNCATE() cuts off extra decimal places.

28. Numeric Functions with NULL

When a numeric function receives NULL, many SQL functions return NULL for that expression.

SELECT ROUND(NULL, 2);

The result is NULL.

When NULL values need a replacement value, functions such as COALESCE() can be useful.

29. Practical Student Example

Numeric functions are useful for creating student fee and marks reports.

SELECT
    name,
    marks,
    ROUND(marks, 2) AS rounded_marks,
    fee,
    ROUND(fee, 2) AS rounded_fee
FROM students;

This displays rounded marks and fee values.

30. Complete Numeric Functions Example

Here is a complete example using several numeric functions.

CREATE TABLE students (
    student_id INT PRIMARY KEY,
    name VARCHAR(100),
    marks DECIMAL(5,2),
    total_fee DECIMAL(10,2),
    paid_fee DECIMAL(10,2)
);

SELECT
    name,
    ROUND(marks, 1) AS rounded_marks,
    ABS(total_fee - paid_fee) AS fee_difference,
    ROUND(total_fee - paid_fee, 2) AS remaining_fee
FROM students;

This query rounds marks, calculates the absolute fee difference, and calculates the remaining fee for each student.

📌 Key Points

  • Numeric functions are used to perform operations on numbers.
  • ROUND() rounds a number.
  • CEIL() rounds a number upward.
  • FLOOR() rounds a number downward.
  • ABS() returns the absolute value.
  • MOD() returns the remainder after division.
  • POWER() and POW() calculate powers.
  • SQRT() calculates the square root.
  • RAND() generates a pseudo-random value in MySQL.
  • TRUNCATE() cuts decimal places without rounding in MySQL.
  • GREATEST() returns the largest supplied value in MySQL.
  • LEAST() returns the smallest supplied value in MySQL.
  • Numeric functions can be combined with SELECT, WHERE, ORDER BY, and aggregate functions.

🧠 Quick Quiz

Question: Which SQL function is used to round a number to a specified number of decimal places?