w3resource

MySQL VAR_SAMP() function

VAR_SAMP() function

MySQL VAR_SAMP() function returns the sample variance of a given expression. Variance is a measure of how spread out or dispersed a set of data points is.

This function is useful in -

  • Variance is a key parameter in statistics that provides insights into the dispersion of data points around the mean.
  • Variance can indicate how much your data points vary from the average. High variance might suggest inconsistencies or outliers in the data.
  • In fields like finance, understanding the variance of returns on investments helps in assessing risk. A higher variance might indicate higher risk.
  • In manufacturing or process control, variance can be used to monitor the consistency and quality of products.
  • When dealing with scientific experiments or surveys, analyzing the variance of measurements can provide insights into the reliability of the results.

Syntax

VAR_SAMP(expr);

Where expr is an expression.

MySQL Version: 8.0

Example:

Sample table: purchase


Code:

SELECT VAR_SAMP(total_cost)             
FROM purchase;

Explanation

The following statement returns the sample variance of 'total_cost' from purchase table.

Output:

mysql> SELECT VAR_SAMP(total_cost)             
    -> FROM purchase;
+----------------------+
| VAR_SAMP(total_cost) |
+----------------------+
|        119366.666667 | 
+----------------------+
1 row in set (0.00 sec)

Example: MySQL VAR_SAMP() with where clause

The following statement returns the sample variance of 'expr' 'total_cost' from purchase table for the category 'cate_id' as given with WHERE clause.

Sample table: purchase


Code:

SELECT VAR_SAMP(total_cost)              
FROM purchase             
WHERE cate_id='CA001';

Output:

mysql> SELECT VAR_SAMP(total_cost)              
    -> FROM purchase             
    -> WHERE cate_id='CA001';
+----------------------+
| VAR_SAMP(total_cost) |
+----------------------+
|        236875.000000 | 
+----------------------+
1 row in set (0.00 sec)

Previous: VAR_POP()
Next: VARIANCE()



Follow us on Facebook and Twitter for latest update.