avg
Calculates the arithmetic mean of numeric values. NULL values are skipped.
Syntax
text
avg(x)Arguments
x— A numeric column or expression. Integers, floating-point numbers, and decimals are supported. Legacy text and blob inputs use SQL numeric conversion. Convert native dates, times, intervals, and other nonnumeric values explicitly before averaging them.
Returned value
Returns a REAL mean for ordinary numeric inputs, or an exact decimal mean for
native decimal inputs. Empty or all-NULL input returns NULL.
Do not mix native decimals with approximate inputs without an explicit cast.
Exact overflow raises NUMERIC_OVERFLOW.
Examples
Basic usage
sql
CREATE TABLE measurements (x Int8);
INSERT INTO measurements VALUES (0), (1), (2), (3), (4), (5);sql
SELECT avg(x) FROM measurements FORMAT csv;text
2.5Empty table returns NULL
sql
CREATE TABLE test (t UInt8);sql
SELECT avg(t) FROM test FORMAT csv;text
\NAverage request duration
Use a numeric duration in a known unit:
sql
CREATE TABLE requests (req_id Int64, duration_ms UInt32);
INSERT INTO requests VALUES (1, 100), (2, 200), (3, 300);sql
SELECT avg(duration_ms) AS avg_duration_ms FROM requests FORMAT csv;text
200.0