Statistical and mathematical functions in SQL
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.
Employee_id | Employee_name | Salary |
1 | Alice | 50000 |
2 | Bob | 52000 |
3 | Carol | 48000 |
4 | David | 60000 |
5 | Emma | 65000 |
6 | Frank | 55000 |
7 | Grace | 58000 |
SUM()
Calculates the sum of a set of values.
SELECT SUM(salary) AS total_salary FROM employees;
total_salary |
388000 |
This query calculates the total salary of all employees.
AVG()
Calculates the average (mean) of a set of values.
SELECT AVG(salary) AS avg_salary FROM employees;
avg_salary |
54000 |
This query calculates the average salary of employees.
MIN()
:Returns the minimum value from a set of values.
Example:
SELECT MIN(salary) AS min_salary FROM employees;
48000
min_salary |
48000 |
MAX()
:Returns the maximum value from a set of values.
Example:
sql SELECT MAX(salary) AS max_salary FROM employees;
65000
max_salary |
65000 |
This query finds the maximum salary among employees.
COUNT()
:Counts the number of rows in a result set or the number of non-null values in a column.
Example:
SELECT COUNT(*) AS total_employees FROM employees;
7
total_employees |
7 |
This query counts the total number of employees.
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:
SELECT STDDEV(salary) AS salary_stddev, VARIANCE(salary) AS salary_variance FROM employees;
salary_stddev | salary_variance |
6383.192778 | 40750000 |
The above calculations determine the variance and standard deviation of salaries.
CORR()
:Calculates the correlation coefficient between two numeric columns. It measures the linear relationship between the two variables.
Example:
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.
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:
SELECT COVAR_POP(x, y) AS population_covariance, COVAR_SAMP(x, y) AS sample_covariance FROM data;
population_covariance | sample_covariance |
6250 | 7500 |
These queries calculate the population and sample covariances between columns x and y.
POWER()
and SQRT()
POWER(x, y)
raises x to the power of y.
SQRT(x)
calculates the square root of x.
Example:
SELECT POWER(2, 3) AS two_cubed, SQRT(25) AS square_root_of_25;
two_cubed | square_root_of_25 |
8 | 5 |
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.
ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.
What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!
Start using NotebookLM today and embark on a smarter, more efficient learning journey!
This can be a super guide for you to start and excel in your data science career.
In this following article we will familiarize with dictionary and it’s numerous functionalities that makes it so versatile.
Learn to store bytes and byte-arrays. Learn to convert normal datatypes to byte sequence. The bytes() function in Python returns an immutable bytes sequence.
At its heart, the `print()` function sends data to the standard output, typically the console.
List is one of the four data types in Python. Python allows us to create a heterogeneous collection of items inside a list.
NLP is a branch of AI that concerns with computers(AI) understanding natural languages.
Booleans are most important aspects of programming languages.
Booleans are most important aspects of programming languages.
Sentiment analysis can determine the polarity of sentiments from given sentences. We can classify them into certain ranges positive, neutral, negative
Strings is one of the important fundamental datatypes in python. Interactions of input and output console’s are conveyed using strings.
Read this article further to know where to use stemmers and lemmatization. Lemmatization maybe better than stemmer but is it worth your time.
One response to “SQL stats and maths functions”
[…] 7. Statistical and Mathematical Functions: […]
Points You Earned