2020-06-18 08:24:31 +00:00
---
2024-03-11 20:48:12 +00:00
title: "varSamp"
2024-06-24 11:52:30 +00:00
slug: /en/sql-reference/aggregate-functions/reference/varSamp
2024-06-24 12:08:48 +00:00
sidebar_position: 212
2020-06-18 08:24:31 +00:00
---
2024-03-11 20:48:12 +00:00
## varSamp
2020-06-18 08:24:31 +00:00
2024-03-14 14:04:19 +00:00
Calculate the sample variance of a data set.
2020-06-18 08:24:31 +00:00
2024-03-11 20:48:12 +00:00
**Syntax**
2020-06-18 08:24:31 +00:00
2024-03-11 20:48:12 +00:00
```sql
2024-06-14 13:33:19 +00:00
varSamp(x)
2024-03-11 20:48:12 +00:00
```
2024-06-14 13:33:19 +00:00
Alias: `VAR_SAMP` .
2024-03-11 20:48:12 +00:00
**Parameters**
2024-06-14 13:33:19 +00:00
- `x` : The population for which you want to calculate the sample variance. [(U)Int* ](../../data-types/int-uint.md ), [Float* ](../../data-types/float.md ), [Decimal* ](../../data-types/decimal.md ).
2024-03-11 20:48:12 +00:00
**Returned value**
2024-06-14 13:39:52 +00:00
2024-06-14 13:33:19 +00:00
- Returns the sample variance of the input data set `x` . [Float64 ](../../data-types/float.md ).
2024-03-11 20:48:12 +00:00
**Implementation details**
2024-06-14 13:33:19 +00:00
The `varSamp` function calculates the sample variance using the following formula:
2024-03-11 20:48:12 +00:00
2024-06-14 13:33:19 +00:00
$$
\sum\frac{(x - \text{mean}(x))^2}{(n - 1)}
$$
2024-03-11 20:48:12 +00:00
Where:
2024-06-14 13:33:19 +00:00
2024-03-11 20:48:12 +00:00
- `x` is each individual data point in the data set.
- `mean(x)` is the arithmetic mean of the data set.
- `n` is the number of data points in the data set.
2024-06-14 13:33:19 +00:00
The function assumes that the input data set represents a sample from a larger population. If you want to calculate the variance of the entire population (when you have the complete data set), you should use [`varPop` ](../reference/varpop.md ) instead.
2024-03-11 20:48:12 +00:00
**Example**
Query:
```sql
2024-06-14 13:33:19 +00:00
DROP TABLE IF EXISTS test_data;
CREATE TABLE test_data
2024-03-11 20:48:12 +00:00
(
2024-06-14 13:33:19 +00:00
x Float64
2024-03-11 20:48:12 +00:00
)
2024-06-14 13:33:19 +00:00
ENGINE = Memory;
2024-03-11 20:48:12 +00:00
2024-06-14 13:33:19 +00:00
INSERT INTO test_data VALUES (10.5), (12.3), (9.8), (11.2), (10.7);
2024-03-11 20:48:12 +00:00
2024-06-14 13:33:19 +00:00
SELECT round(varSamp(x),3) AS var_samp FROM test_data;
2024-03-11 20:48:12 +00:00
```
Response:
```response
2024-06-14 13:33:19 +00:00
┌─var_samp─┐
│ 0.865 │
└──────────┘
2024-03-11 20:48:12 +00:00
```