AVG

Returns the average of numeric values.

Syntax

-- As an aggregate
AVG( expr )

-- As a window function
AVG ( expr ) OVER (
         [ PARTITION BY value_expression [, ...] ]
         [ ORDER BY value_expression [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] ]
         [ frame_clause ]
      )

Arguments

Parameter

Description

expr

Numeric expression

Returns

Return type is dependant on the argument.

  • For TINYINT, SMALLINT and INT, the return type is INT.

  • For BIGINT, the return type is BIGINT.

  • For REAL, the return type is REAL

  • For DOUBLE, rhe return type is DOUBLE

Notes

  • NULL values are ignored

  • AVG relies on calculating the sum, which can very quickly overflow on large data sets. If the sum is over 231 for example, up-cast to a larger type like BIGINT: AVG(expr :: BIGINT)

Examples

For these examples, assume a table named nba, with the following structure:

CREATE TABLE nba
(
   "Name" text,
   "Team" text,
   "Number" tinyint,
   "Position" text,
   "Age" tinyint,
   "Height" text,
   "Weight" real,
   "College" text,
   "Salary" float
 );

Here’s a peek at the table contents (Download nba.csv):

nba.csv

Avery Bradley

Boston Celtics

0

PG

25

2-Jun

180

Texas

7730337

Jae Crowder

Boston Celtics

99

SF

25

6-Jun

235

Marquette

6796117

John Holland

Boston Celtics

30

SG

27

5-Jun

205

Boston University

R.J. Hunter

Boston Celtics

28

SG

22

5-Jun

185

Georgia State

1148640

Jonas Jerebko

Boston Celtics

8

PF

29

10-Jun

231

5000000

Amir Johnson

Boston Celtics

90

PF

29

9-Jun

240

12000000

Jordan Mickey

Boston Celtics

55

PF

21

8-Jun

235

LSU

1170960

Kelly Olynyk

Boston Celtics

41

C

25

Jul-00

238

Gonzaga

2165160

Terry Rozier

Boston Celtics

12

PG

22

2-Jun

190

Louisville

1824360

Simple average

t=> SELECT AVG("Age") FROM nba;
avg
---
26

Note

The return type is the same as the input type. To get a fractional result, cast the argument:

t=> SELECT AVG("Age" :: REAL) FROM nba;
avg
-------
26.9387

Combine AVG with other aggregates

t=> SELECT "Age", AVG("Salary") as "Average salary", COUNT(*) as "Number of players" FROM nba GROUP BY 1;
Age | Average salary | Number of players
----+----------------+------------------
 19 |        1930440 |                 2
 20 |        2725790 |                19
 21 |        2067379 |                19
 22 |        2357963 |                26
 23 |        2034746 |                41
 24 |        3785300 |                47
 25 |        3930867 |                45
 26 |        6866566 |                36
 27 |        6676741 |                41
 28 |        5110188 |                31
 29 |        6224177 |                28
 30 |        7061858 |                31
 31 |        8511396 |                22
 32 |        7716958 |                13
 33 |        3930739 |                14
 34 |        7606030 |                10
 35 |        3461739 |                 9
 36 |        2238119 |                10
 37 |       12777778 |                 4
 38 |        1840041 |                 4
 39 |        2517872 |                 2
 40 |        4666916 |                 3