Skip to content

COUNT

Return the number of non-NULL values for an expression. The ALL keyword is optional. Use COUNT(*) to return a count of all of the rows in a table (regardless of NULL values).

COUNT( [ ALL ] expression)
COUNT(*)

The COUNT function works with all supported data types.

For example, count the rows in the match table:

premdb=# SELECT COUNT(*) FROM match;
 count 
-------
  8606
(1 row)

The following query groups by the winners column in the season table to count the number of times a given team won the league:

premdb=# SELECT winners, COUNT(winners) 
FROM season WHERE winners IS NOT NULL GROUP BY winners ORDER BY 2 DESC;
     winners      | count 
-------------------+-------
 Manchester United |    13
 Chelsea           |     4
 Arsenal           |     3
 Manchester City   |     2
 Blackburn Rovers  |     1
 Leicester City    |     1
(6 rows)

Note: The COUNT function always returns a BIGINT data type.

Parent topic:Aggregate Functions