MATH functions in SQL

MATH functions in SQL

Absolute Value or ABS()

Use ABS() to return non-negative values.

ABS(number) #returns the number in positive form.

SELECT Duration, ABS(Duration) AS AbsSeconds
FROM Incidents
#this returns Duration in non-negative values

Square and square root

SELECT 
SQRT(9) #this returns the square root of 9 which is 3
SQUARE(9) #this returns the square of 9 which is 81
FROM numbers

Logs

  • logs() returns the natural logarithm

  • By default, the base is set to 2.718281828.

LOG(number [,Base])

To calculate log base 10

SELECT
Duration , LOG(Duration, 10) AS LogSeconds
FROM numbers

COUNT()

  • Returns the number of items found in a group.
COUNT([ALL] expression) #counts all rows
COUNT(DISTINCT expression) #returns the number of unique values
COUNT(*) #count of all rows

SUM()

  • Returns the sum of all values of a group
SUM([ALL] expression) #returns the sum of the expression
SUM(DISTINCT) #returns the sum of distinct values
SELECT 
    gender, 
    -- Count the number of voters for each group
    COUNT(*) AS voters,
    -- Calculate the total number of votes per group
    SUM(total_votes) AS total_votes
FROM voters
GROUP BY gender;

MAX()

  • Returns the maximum value of an expression
MAX([ALL] expression)
MAX(DISTINCT expression)  #returns the MAX of distinct values in a group

MIN()

-Returns the minimum value of an expression

MIN([ALL] expression)
MIN(DISTINCT expression)  #returns the MIN of distinct values in a group
SELECT 
    company,
    -- Calculate the average cocoa percent
    AVG(cocoa_percent) AS avg_cocoa,
    -- Calculate the minimum rating received by each company
    MIN(rating) AS min_rating,
    -- Calculate the maximum rating received by each company
    MAX(rating) AS max_rating
FROM ratings
GROUP BY company;

AVG()

  • Returns the average of the values in the group
AVG([ALL] expression)
AVG(DISTINCT expression)  #returns the AVG of distinct values in a group
SELECT 
    company,
    -- Calculate the average cocoa percent
    AVG(cocoa_percent) AS avg_cocoa
FROM ratings
GROUP BY company;

Analytic functions

Screenshot 2022-08-08 at 21.32.03.png

Screenshot 2022-08-08 at 21.33.39.png

Screenshot 2022-08-08 at 21.34.38.png

SIGN(numeric_expression)

  • Returns the sign of an expression as an integer

  • -1 (negative numbers)

  • 0 (if the expression is zero)

  • +1 (positive numbers)

Screenshot 2022-08-08 at 21.47.14.png

SELECT
    rating,
    -- Round-up the rating to an integer value
    CEILING(rating) AS round_up,
    -- Round-down the rating to an integer value
    FLOOR(rating) AS round_down,
    -- Round the rating value to one decimal
    ROUND(rating, 1) AS round_onedec,
    -- Round the rating value to two decimals
    ROUND(rating, 2) AS round_twodec
FROM ratings;

RECAP

SELECT 
    first_name + ' ' + last_name AS name,
    country,
    birthdate,
    -- Retrieve the birthdate of the oldest voter per country
    FIRST_VALUE(birthdate) 
    OVER (PARTITION BY country ORDER BY birthdate) AS oldest_voter,
    -- Retrieve the birthdate of the youngest voter per country
    LAST_VALUE(birthdate) 
        OVER (PARTITION BY country ORDER BY birthdate ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
                ) AS youngest_voter
FROM voters
WHERE country IN ('Spain', 'USA');
DECLARE @number DECIMAL(4, 2) = 4.5;
DECLARE @power INT = 4;

SELECT
    @number AS number,
    @power AS power,
    -- Raise the @number to the @power
    POWER(@number, @power) AS number_to_power,
    -- Calculate the square of the @number
    SQUARE(@number) num_squared,
    -- Calculate the square root of the @number
    SQRT(@number) num_square_root;
SELECT 
    company, 
    -- Select the number of cocoa flavors for each company
    COUNT(*) AS flavors,
    -- Select the minimum, maximum and average rating
    MIN(rating) AS lowest_score,   
    MAX(rating) AS highest_score,   
    AVG(rating) AS avg_score,
    -- Round the average rating to 1 decimal
    ROUND(AVG(rating), 1) AS round_avg_score,
    -- Round up and then down the aveg. rating to the next integer 
    CEILING(AVG(rating)) AS round_up_avg_score,   
    FLOOR(AVG(rating)) AS round_down_avg_score
FROM ratings
GROUP BY company
ORDER BY flavors DESC;