> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> The `gini` function calculates the Gini coefficient of a finite, non-negative numeric column, a measure of inequality in a distribution.

# gini

<h2 id="gini">
  gini
</h2>

Introduced in: v26.8.0

Calculates the [Gini coefficient](https://en.wikipedia.org/wiki/Gini_coefficient) of a column of finite, non-negative numeric values, a measure of inequality in a distribution.

The result ranges from `0` (perfect equality: all values are the same) to a maximum approaching `1` as `n` grows (perfect inequality: one value holds everything and the rest are zero). For a finite sample of `n` values the maximum is `(n - 1) / n`.

All passed values are collected in their input type and then sorted before a numerically stable final calculation. The coefficient is computed from normalized adjacent pairwise differences and returned as `Float64`, so it is rounded to `Float64` precision. Therefore, the function consumes `O(n)` memory, where `n` is the number of values passed. `NaN` values are skipped, while infinite values are rejected.

**Syntax**

```sql theme={null}
gini(expr)
```

**Arguments**

* `expr` — Expression resulting in finite, non-negative numeric values. [`(U)Int*`](/docs/reference/data-types/int-uint) or [`Float*`](/docs/reference/data-types/float) or [`Decimal`](/docs/reference/data-types/decimal)

**Returned value**

The Gini coefficient, or `NaN` if there are fewer than 2 values or the sum of the values is 0. [`Float64`](/docs/reference/data-types/float)

**Examples**

**Equal values**

```sql title=Query theme={null}
SELECT gini(x) FROM (SELECT 100 AS x FROM numbers(10));
```

```response title=Response theme={null}
┌─gini(x)─┐
│       0 │
└─────────┘
```

**Skewed distribution**

```sql title=Query theme={null}
SELECT gini(x) FROM (SELECT number AS x FROM numbers(10));
```

```response title=Response theme={null}
┌─────────────gini(x)─┐
│ 0.3666666666666665  │
└─────────────────────┘
```
