# gini (/docs/reference/sql/aggregate-functions/gini)



This page describes the RawTree engine binary used to generate this reference. It does not define Query API support. Descriptions retain upstream ClickHouse terminology. See [SQL interface scope](/docs/reference/clickhouse-compatibility#service-interfaces).

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 and negative values are rejected.

**Syntax**

```sql
gini(expr)
```

**Arguments**

* `expr` — Expression resulting in finite, non-negative numeric values. [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal`](/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`](/reference/data-types/float)

**Examples**

**Equal values**

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

```text title=Response
┌─gini(x)─┐
│       0 │
└─────────┘
```

**Skewed distribution**

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

```text title=Response
┌─────────────gini(x)─┐
│ 0.36666666666666664 │
└─────────────────────┘
```

*Introduced in version 26.8.0.*
