MySQL Statistical Functions
Last Updated :
30 Jul, 2024
MySQL provides a rich set of statistical functions that we can use to perform various statistical analyses directly within the database. These functions help us to derive insights and trends from large datasets and are essential for data analysis. This article will explore some of the key MySQL statistical functions.
What are Statistical Functions?
Statistical functions in MySQL are built-in functions that perform statistical analysis on numerical data within a database. These functions help us to summarize and understand data by calculating various statistical measures. Here are some statistical functions:
- AVG()
- SUM()
- COUNT()
- MIN()
- MAX()
- STDDEV()
- VARIANCE()
Demo Database
To explain the usage of each statistical function in MySQL, let's create a sample table and populate it with sample data:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2),
experience INT
);
INSERT INTO employees (id, name, department, salary, experience) VALUES
(1, 'Amit Sharma', 'Sales', 50000, 5),
(2, 'Anita Patel', 'HR', 60000, 7),
(3, 'Rajesh Kumar', 'IT', 70000, 10),
(4, 'Sita Verma', 'Sales', 55000, 6),
(5, 'Ravi Gupta', 'IT', 65000, 8),
(6, 'Neeta Singh', 'HR', 62000, 7),
(7, 'Vikram Rao', 'Sales', 58000, 5),
(8, 'Pooja Desai', 'IT', 72000, 12),
(9, 'Meena Reddy', 'HR', 61000, 9),
(10, 'Rohan Kapoor', 'Sales', 53000, 4);
Output:
employees table1. AVG() - Average
MySQL AVG function calculates the average value of a numeric column.
Syntax
SELECT AVG(column_name) FROM table_name;
Example: In the below example we will find the average salary of employees.
SELECT AVG(salary) AS average_salary
FROM employees;
Output:
+----------------+
| average_salary |
+----------------+
| 60600.000000 |
+----------------+
2. SUM() - Sum
The sum function in MySQL adds up all values in a numeric column.
Syntax
SELECT SUM(column_name) FROM table_name;
Example: In this example, we have find the total salary paid to all employees.
SELECT SUM(salary) AS total_salary
FROM employees;
Output:
+--------------+
| total_salary |
+--------------+
| 606000.00 |
+--------------+
3. COUNT() - Count
The count function in MySQl is used to count the total number of rows or non-NULL values in a column.
Syntax
SELECT COUNT(column_name) FROM table_name;
Example: In this example, we will count the total number of employees.
SELECT COUNT(id) AS employee_count
FROM employees;
Output:
+----------------+
| employee_count |
+----------------+
| 10 |
+----------------+
4. MIN() - Minimum
The min function finds the smallest value in a numeric column.
Syntax
SELECT MIN(column_name) FROM table_name;
Example: To find the minimum salary among employees.
SELECT MIN(salary) AS min_salary
FROM employees;
Output:
+------------+
| min_salary |
+------------+
| 50000.00 |
+------------+
5. MAX() - Maximum
The min function finds the maximum value in a numeric column.
Syntax
SELECT MAX(column_name) FROM table_name;
Example: To find the maximum salary among employees.
SELECT MAX(salary) AS max_salary
FROM employees;
Output:
+------------+
| max_salary |
+------------+
| 72000.00 |
+------------+
6. STDDEV() - Standard Deviation
STDDEV function measures the amount of variation or dispersion of values.
Syntax
SELECT STDDEV(column_name) FROM table_name;
Example: In this example, we will calculate the standard deviation of salaries.
SELECT STDDEV(salary) AS stddev_salary
FROM employees;
Output:
+--------------------+
| stddev_salary |
+--------------------+
| 6696.2676171132825 |
+--------------------+
7. VARIANCE() - Variance
VARIANCE function in MySQL measures how much values vary from the mean.
Syntax
SELECT VARIANCE(column_name) FROM table_name;
Example: To calculate the variance of salaries.
SELECT VARIANCE(salary) AS variance_salary
FROM employees;
Output:
+-----------------+
| variance_salary |
+-----------------+
| 44840000 |
+-----------------+
Conclusion
MySQL statistical functions like AVG(), SUM(), COUNT(), MIN(), MAX(), STDDEV(), and VARIANCE() help us to perform data analysis directly in the database. Using these functions, we can quickly calculate averages, totals, counts, and other statistics to gain insights from your data.
Similar Reads
SQL - Statistical Functions
SQL statistical functions are essential tools for extracting meaningful insights from databases. These functions, enable users to perform statistical calculations on numeric data. Whether determining averages, sums, counts, or measures of variability, these functions empower efficient data analysis
4 min read
Statistical Functions in PL/SQL
PL/SQL provides powerful statistical functions to perform various statistical calculations directly within the Oracle database. It provides a rich set of statistical functions that allow developers to perform complex calculations without the need for external tools. These functions, such as AVG, STD
5 min read
SQL | String functions
SQL String Functions are powerful tools that allow us to manipulate, format, and extract specific parts of text data in our database. These functions are essential for tasks like cleaning up data, comparing strings, and combining text fields. Whether we're working with names, addresses, or any form
8 min read
MySQL Window Functions
MySQL Window Functions are advanced SQL capabilities that enable expensive calculations across sets of rows related to the current row. Aggregate functions collapse the result set. These functions, in general, permit ranking, running totals, moving averages, and access to data from other rows within
6 min read
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications. In the article, we will cove
4 min read
SUM() Function in MySQL
The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
SQL SUM() Function
The SUM() function in SQL is one of the most commonly used aggregate functions. It allows us to calculate the total sum of a numeric column, making it essential for reporting and data analysis tasks. Whether we're working with sales data, financial figures, or any other numeric information, the SUM(
5 min read
PLSQL | SQRT Function
In PL/SQL, the SQRT function is used to find the square root of a number. This function is really handy for various tasks that involve mathematical calculations, such as analyzing statistics, solving geometry problems, or handling financial data. The SQRT function is easy to use and can simplify com
4 min read
SQLite MAX() Function
MAX function is a type of Aggregate Function available in SQLite, which is primarily used to find out the maximum value from a given set (a column that is passed as its parameter). Other than that, the MAX function can also be used with other Aggregate functions like HAVING, GROUP BY, etc to sort or
5 min read
Statistical Functions in Excel With Examples
To begin with, statistical function in Excel let's first understand what is statistics and why we need it? So, statistics is a branch of sciences that can give a property to a sample. It deals with collecting, organizing, analyzing, and presenting the data. One of the great mathematicians Karl Pears
6 min read