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:
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 table 1. AVG() - AverageMySQL AVG function calculates the average value of a numeric column.
SyntaxSELECT 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:
+----------------+2. SUM() - Sum
| average_salary |
+----------------+
| 60600.000000 |
+----------------+
The sum function in MySQL adds up all values in a numeric column.
SyntaxSELECT 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:
+--------------+3. COUNT() - Count
| total_salary |
+--------------+
| 606000.00 |
+--------------+
The count function in MySQl is used to count the total number of rows or non-NULL values in a column.
SyntaxSELECT 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:
+----------------+4. MIN() - Minimum
| employee_count |
+----------------+
| 10 |
+----------------+
The min function finds the smallest value in a numeric column.
SyntaxSELECT MIN(column_name) FROM table_name;
Example: To find the minimum salary among employees.
SELECT MIN(salary) AS min_salary
FROM employees;
Output:
+------------+5. MAX() - Maximum
| min_salary |
+------------+
| 50000.00 |
+------------+
The min function finds the maximum value in a numeric column.
SyntaxSELECT MAX(column_name) FROM table_name;
Example: To find the maximum salary among employees.
SELECT MAX(salary) AS max_salary
FROM employees;
Output:
+------------+6. STDDEV() - Standard Deviation
| max_salary |
+------------+
| 72000.00 |
+------------+
STDDEV function measures the amount of variation or dispersion of values.
SyntaxSELECT 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:
+--------------------+7. VARIANCE() - Variance
| stddev_salary |
+--------------------+
| 6696.2676171132825 |
+--------------------+
VARIANCE function in MySQL measures how much values vary from the mean.
SyntaxSELECT VARIANCE(column_name) FROM table_name;
Example: To calculate the variance of salaries.
SELECT VARIANCE(salary) AS variance_salary
FROM employees;
Output:
+-----------------+Conclusion
| variance_salary |
+-----------------+
| 44840000 |
+-----------------+
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4