PostgreSQL

Chapter 7 - DQL (Data Query Language)

NUMERIC functions

In PostgreSQL, numeric functions are crucial for performing mathematical operations and calculations on numeric data types. These functions help in manipulating and analyzing numerical data stored in your database. They can be used for a variety of tasks, from simple arithmetic operations to more complex mathematical computations. Understanding these functions is essential for querying and processing numerical data effectively.

Here are some key numeric functions in PostgreSQL:

  1. ABS()Function
    • Returns the absolute value of a number.
    • Syntax: ABS(number)
    • Example:
    SELECT ABS(-123.45) AS absolute_value;
    

    This query returns 123.45.

  2. ROUND()Function
    • Rounds a number to a specified number of decimal places.
    • Syntax: ROUND(number, [decimal_places])
    • Example:
    SELECT ROUND(123.4567, 2) AS rounded_value;
    

    This query returns 123.46.

  3. CEIL()orCEILING()Function
    • Returns the smallest integer greater than or equal to a number.
    • Syntax: CEIL(number) or CEILING(number)
    • Example:
    SELECT CEIL(123.45) AS ceiling_value;
    

    This query returns 124.

  4. FLOOR()Function
    • Returns the largest integer less than or equal to a number.
    • Syntax: FLOOR(number)
    • Example:
    SELECT FLOOR(123.45) AS floor_value;
    

    This query returns 123.

  5. POWER()Function
    • Raises a number to the power of another number.
    • Syntax: POWER(base, exponent)
    • Example:
    SELECT POWER(2, 3) AS power_value;
    

    This query returns 8.

  6. SQRT()Function
    • Returns the square root of a number.
    • Syntax: SQRT(number)
    • Example:
    SELECT SQRT(16) AS square_root;
    

    This query returns 4.

  7. RANDOM()Function
    • Returns a random number between 0 and 1.
    • Syntax: RANDOM()
    • Example:
    SELECT RANDOM() AS random_number;
    

    This query returns a random number such as 0.5832.

These numeric functions can be particularly useful when dealing with financial data in banking applications. For instance, if you need to calculate interest rates or round transaction amounts, these functions provide the necessary tools for accurate computations.

Tansy SQL Course - NUMERIC functions - Video Thumbnail
Comments(0 comments)

Comments Not Found