CARDINALITY
The CARDINALITY function returns the size or the count of all the elements in an array.
It can be a useful
function to understand how many elements are in an array and often it is used a check to make sure that
operations on arrays are valid. Below we look at how to use the CARDINALITY function
SYNTAX
The cardinality function take in an array as the only argument.
CARDINALITY(some_array)
CARDINALITY by Example
The table below contains information about players and the points they got in a championship. The output of the
table looks like this:
SELECT *
FROM BoardGamePoints;
Let's write a SQL query that will return the number of games each player was involved in the tournament.
SELECT
*,
CARDINALITY(points) AS games
from BoardGamePoints;
Notice that the output now contains the count of the size of the arrays.