![]() |
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:
Demo DatabaseTo explain the usage of each statistical function in MySQL, let’s create a sample table and populate it with sample data: CREATE TABLE employees ( 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 Output: +----------------+ 2. SUM() – SumThe 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 Output: +--------------+ 3. COUNT() – CountThe 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 Output: +----------------+ 4. MIN() – MinimumThe 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 Output: +------------+ 5. MAX() – MaximumThe 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 Output: +------------+ 6. STDDEV() – Standard DeviationSTDDEV 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 Output: +--------------------+ 7. VARIANCE() – VarianceVARIANCE 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 Output: +-----------------+ ConclusionMySQL 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. FAQs on MySQL Statistical FunctionsWhat is the difference between
|
Reffered: https://www.geeksforgeeks.org
Databases |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 19 |