Skip to content
ClickHouse Docs
ClickHouse DocsClickHouse Docs

gini

gini

Introduced in: v26.8.0

Calculates the 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

gini(expr)

Arguments

  • expr — Expression resulting in finite, non-negative numeric values. (U)Int* or Float* or Decimal

Returned value

The Gini coefficient, or NaN if there are fewer than 2 values or the sum of the values is 0. Float64

Examples

Equal values

SELECT gini(x) FROM (SELECT 100 AS x FROM numbers(10));
┌─gini(x)─┐
│       0 │
└─────────┘

Skewed distribution

SELECT gini(x) FROM (SELECT number AS x FROM numbers(10));
┌─────────────gini(x)─┐
│ 0.36666666666666664 │
└─────────────────────┘
Navigation