SQL stats and maths functions

Statistical and mathematical functions in SQL

Statistical and mathematical functions in SQL allow you to perform calculations and analysis on numeric data within your database. These functions are invaluable for tasks ranging from basic statistical measures to more complex mathematical operations.

Sample Dataset (employees)

Employee_idEmployee_nameSalary
1Alice50000
2Bob52000
3Carol48000
4David60000
5Emma65000
6Frank55000
7Grace58000

1. SUM()

Calculates the sum of a set of values.

SQL
SQL
SQL
SELECT SUM(salary) AS total_salary FROM employees;
total_salary
388000

This query calculates the total salary of all employees.

2. AVG()

Calculates the average (mean) of a set of values.

SQL
SQL
SQL
SELECT AVG(salary) AS avg_salary FROM employees;
avg_salary
54000

This query calculates the average salary of employees.

3. MIN():

Returns the minimum value from a set of values.

Example:

SQL
SQL
SQL
SELECT MIN(salary) AS min_salary FROM employees;
48000
min_salary
48000

4. MAX():

Returns the maximum value from a set of values.

Example:

SQL
SQL
SQL
sql SELECT MAX(salary) AS max_salary FROM employees;
65000
max_salary
65000

This query finds the maximum salary among employees.

5. COUNT():

Counts the number of rows in a result set or the number of non-null values in a column.

Example:

SQL
SQL
SQL
SELECT COUNT(*) AS total_employees FROM employees;
7
total_employees
7

This query counts the total number of employees.

6. STDDEV() and VARIANCE():

STDDEV() computes the standard deviation of a set of values, which is a measure of variation or dispersion.

VARIANCE() computes a set of values variance, which is the average of the squared differences from the mean.

Example:

SQL
SQL
SQL
SELECT STDDEV(salary) AS salary_stddev, VARIANCE(salary) AS salary_variance FROM employees;
salary_stddevsalary_variance
6383.19277840750000

The above calculations determine the variance and standard deviation of salaries.

7. CORR():

Calculates the correlation coefficient between two numeric columns. It measures the linear relationship between the two variables.

Example:

SQL
SQL
SQL
SELECT CORR(age, income) AS age_income_correlation FROM customers;
employee_id_salary_correlation
0.1069888054

This query calculates the correlation between customer ages and their incomes.

8. COVAR_POP() and COVAR_SAMP()

COVAR_POP() calculates the population covariance between two numeric columns.

COVAR_SAMP() calculates the sample covariance between two numeric columns.

Example:

SQL
SQL
SQL
SELECT COVAR_POP(x, y) AS population_covariance, COVAR_SAMP(x, y) AS sample_covariance FROM data;
population_covariancesample_covariance
62507500

These queries calculate the population and sample covariances between columns x and y.

9. POWER() and SQRT()

POWER(x, y) raises x to the power of y.

SQRT(x) calculates the square root of x.

Example:

SQL
SQL
SQL
SELECT POWER(2, 3) AS two_cubed, SQRT(25) AS square_root_of_25;
two_cubedsquare_root_of_25
85

These queries perform mathematical operations on numeric values.

These functions are fundamental for performing statistical analysis and mathematical calculations within SQL. They are essential for summarising data, detecting trends, and deriving meaningful insights from numerical data in your database.

How useful was this post?

Click on a star to rate it!

  • ANCOVA: Analysis of Covariance with python

    ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.

  • Learn Python The Fun Way

    What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!

  • Meet the most efficient and intelligent AI assistant : NotebookLM

    Start using NotebookLM today and embark on a smarter, more efficient learning journey!

  • Break the ice

    This can be a super guide for you to start and excel in your data science career.

  • ANOVA (Analysis of Variance ) part 1

    A method to find a statistical relationship between two variables in a dataset where one variable is used to group data.

  • Basic plots with Seaborn

    Seaborn library has matplotlib at its core for data point visualizations. This library gives highly statistical informative graphics functionality to Seaborn.

  • Matplotlib in python

    The Matplotlib library helps you create static and dynamic visualisations. Dynamic visualizations that are animated and interactive. This library makes it easy to plot data and create graphs.

  • Plotly with Python and R

    This library is named Plotly after the company of the same name. Plotly provides visualization libraries for Python, R, MATLAB, Perl, Julia, Arduino, and REST.

  • Numpy Array

    Numpy array have functions for matrices ,linear algebra ,Fourier Transform. Numpy arrays provide 50x more speed than a python list.

  • NumPy: Python’s Mathematical Backbone

    Numpy has created a vast ecosystem spanning numerous fields of science.

  • Introduction to Pandas: A Guide

    Pandas is a easy to use data analysis and manipulation tool. Pandas provides functionality for categorical,ordinal, and time series data . Panda provides fast and powerful calculations for data analysis.

  • Pandas Dataframe in brief

    In this tutorial, you will learn How to Access The Data in Various Ways From the dataframe.

  • Exploring the World of Sets in Python

    Understand one of the important data types in Python. Each item in a set is distinct. Sets can store multiple items of various types of data.

  • A Beginner’s Guide to Immutable Tuples

    Tuples are a sequence of Python objects. A tuple is created by separating items with a comma. They are put inside the parenthesis “”(“” , “”)””.

One response to “SQL stats and maths functions”

  1. […] 7. Statistical and Mathematical Functions: […]

Points You Earned

Untitled design 6
0 distinction_points
Untitled design 5
python_points 0
0 Solver points
Instagram
WhatsApp
error: Content is protected !!