# count() function¶

The count() function counts the number of the specified values or rows.

• (Native nGQL) You can use count() and GROUP BY together to group and count the number of specific values. Use YIELD to return.
• (OpenCypher style) You can use count() and RETURN. GROUP BY is not necessary.

## Syntax¶

count({expr | *})

• count(*) returns the number of rows (including NULL).
• count(expr) returns the number of non-NULL values that meet the expression.
• count() and size() are different.

### Examples¶

nebula> WITH [NULL, 1, 1, 2, 2] As a UNWIND a AS b \
RETURN count(b), count(*), count(DISTINCT b);
+----------+----------+-------------------+
| count(b) | count(*) | count(distinct b) |
+----------+----------+-------------------+
| 4        | 5        | 2                 |
+----------+----------+-------------------+

# The statement in the following example searches for the people whom player101 follows and people who follow player101, i.e. a bidirectional query.
nebula> GO FROM "player101" OVER follow BIDIRECT \
YIELD properties().name AS Name \
| GROUP BY $-.Name YIELD$-.Name, count(*);
+---------------------+----------+
| $-.Name | count(*) | +---------------------+----------+ | "LaMarcus Aldridge" | 2 | | "Tim Duncan" | 2 | | "Marco Belinelli" | 1 | | "Manu Ginobili" | 1 | | "Boris Diaw" | 1 | | "Dejounte Murray" | 1 | +---------------------+----------+  The preceding example retrieves two columns: • $-.Name: the names of the people.
• count(*): how many times the names show up.

Because there are no duplicate names in the basketballplayer dataset, the number 2 in the column count(*) shows that the person in that row and player101 have followed each other.

# a: The statement in the following example retrieves the age distribution of the players in the dataset.
nebula> LOOKUP ON player \
YIELD player.age As playerage \
| GROUP BY $-.playerage \ YIELD$-.playerage as age, count(*) AS number \
| ORDER BY $-.number DESC,$-.age DESC;
+-----+--------+
| age | number |
+-----+--------+
| 34  | 4      |
| 33  | 4      |
| 30  | 4      |
| 29  | 4      |
| 38  | 3      |
+-----+--------+
...

# b: The statement in the following example retrieves the age distribution of the players in the dataset.
nebula> MATCH (n:player) \
RETURN n.player.age as age, count(*) as number \
ORDER BY number DESC, age DESC;
+-----+--------+
| age | number |
+-----+--------+
| 34  | 4      |
| 33  | 4      |
| 30  | 4      |
| 29  | 4      |
| 38  | 3      |
+-----+--------+
...

# The statement in the following example counts the number of edges that Tim Duncan relates.
nebula> MATCH (v:player{name:"Tim Duncan"}) -- (v2) \
RETURN count(DISTINCT v2);
+--------------------+
| count(distinct v2) |
+--------------------+
| 11                 |
+--------------------+

# The statement in the following example counts the number of edges that Tim Duncan relates and returns two columns (no DISTINCT and DISTINCT) in multi-hop queries.
nebula> MATCH (n:player {name : "Tim Duncan"})-[]->(friend:player)-[]->(fof:player) \
RETURN count(fof), count(DISTINCT fof);
+------------+---------------------+
| count(fof) | count(distinct fof) |
+------------+---------------------+
| 4          | 3                   |
+------------+---------------------+


Last update: January 14, 2022