w3resource

MySQL STDDEV_SAMP() function

STDDEV_SAMP() function

MySQL STDDEV_SAMP() function returns the sample standard deviation of an expression ( the square root of VAR_SAMP()). Statistically, sample standard deviation quantifies the amount of variation or dispersion in a dataset for which you have a sample of the whole population.

It returns NULL if no matching rows are found.

This function is useful in -

  • It's used when you have a sample of data and want to estimate the variability in the entire population.
  • Data Analysis: The primary purpose of the STDDEV_SAMP() function is to accurately calculate the sample standard deviation.
  • When comparing different samples or groups, STDDEV_SAMP() is used to assess the significance of differences between the groups.
  • Sample standard deviation is used to calculate confidence intervals for population parameters based on sample data.
  • STDDEV_SAMP() is used to explore the variation and distribution of data in sample datasets, helping in initial data analysis.
  • Sample standard deviation is used in hypothesis testing to test the validity of assumptions and draw conclusions about population characteristics.

Syntax:

STDDEV_SAMP(expr);

Where expr is an expression.

MySQL Version: 8.0

Example: MySQL STDDEV_SAMP() function

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

Sample table: purchase

+------------+------------+----------------+------------+------------+---------+---------------------------------+----------+---------+-------------+-------------+------------+
| invoice_no | invoice_dt | ord_no         | ord_date   | receive_dt | book_id | book_name                       | pub_lang | cate_id | receive_qty | purch_price | total_cost |
+------------+------------+----------------+------------+------------+---------+---------------------------------+----------+---------+-------------+-------------+------------+
| INV0001    | 2008-07-15 | ORD/08-09/0001 | 2008-07-06 | 2008-07-19 | BK001   | Introduction to Electrodynamics | English  | CA001   |          15 |       75.00 |    1125.00 |
| INV0002    | 2008-08-25 | ORD/08-09/0002 | 2008-08-09 | 2008-08-28 | BK004   | Transfer  of Heat and Mass      | English  | CA002   |           8 |       55.00 |     440.00 |
| INV0003    | 2008-09-20 | ORD/08-09/0003 | 2008-09-15 | 2008-09-23 | BK005   | Conceptual Physics              | NULL     | CA001   |          20 |       20.00 |     400.00 |
| INV0004    | 2007-08-30 | ORD/07-08/0005 | 2007-08-22 | 2007-08-30 | BK004   | Transfer  of Heat and Mass      | English  | CA002   |          15 |       35.00 |     525.00 |
| INV0005    | 2007-07-28 | ORD/07-08/0004 | 2007-06-25 | 2007-07-30 | BK001   | Introduction to Electrodynamics | English  | CA001   |           8 |       25.00 |     200.00 |
| INV0006    | 2007-09-24 | ORD/07-08/0007 | 2007-09-20 | 2007-09-30 | BK003   | Guide to Networking             | Hindi    | CA003   |          20 |       45.00 |     900.00 |
+------------+------------+----------------+------------+------------+---------+---------------------------------+----------+---------+-------------+-------------+------------+

Code:


-- This query calculates the sample standard deviation of the 'total_cost' column in the 'purchase' table.
SELECT STDDEV_SAMP(total_cost)
-- This statement selects the sample standard deviation of the 'total_cost' column.
FROM purchase;
-- This part of the query specifies the table from which data is being retrieved, which is 'purchase'.

Explanation:

  • The purpose of this SQL query is to compute the sample standard deviation of the 'total_cost' values in the 'purchase' table.

  • SELECT STDDEV_SAMP(total_cost): This part of the query selects the sample standard deviation of the 'total_cost' column. Sample standard deviation is a statistical measure of the amount of variation or dispersion in a sample.

  • FROM purchase: This part specifies the table from which the data is being selected, which is the 'purchase' table.

  • The query will return a single value, which is the sample standard deviation of the 'total_cost' values in the 'purchase' table. This value provides insight into the spread or dispersion of the 'total_cost' values in the sample represented by the 'purchase' table. It's important to note that sample standard deviation is used when the data is a subset (sample) of a larger population, whereas population standard deviation considers the entire population.

Output:

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

PREV : STDDEV_POP()
NEXT : STDDEV()



Follow us on Facebook and Twitter for latest update.