COUNT

COUNT

The aggregate function COUNT can be used to write queries to answer how many items there are that match a certain pattern. In such cases, we are not interested in the items that match our query pattern themselves, but just want to know the number of the items that match.

Suppose we wanted to know how many Wikidata items there are about women chemists. In principle, we could run a query as follows:

Above the table with the results, WDQS shows how many items were found in how many milliseconds of running the query.

This strategy won’t always work, however. Suppose we wanted to know how many Wikidata items there are about actresses. In principle, we could edit the query as follows:

#Actresses on Wikidata
SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q5.            # Item is instance of human
  ?item wdt:P21 wd:Q6581072.      # Item is a woman
  ?item wdt:P106 wd:Q33999.       # Occupation is actor.     
  
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }  
  }

In principle, WDQS would return all matching items and show us how many items were found that match the pattern. However, because the number of matching items is very large, the query will time out. This is due to a query deadline which is set to 60 seconds. Every query that takes more time to execute than this configured deadline will time out:

To answer how many actresses there are on Wikidata we declare a variable in the SELECT clause, in this example we call it ?actresscount. The variable is defined as the COUNT of items with patterns matching those in the WHERE clause:

This query has only one result: the variable actresscount with the value of the count of all items matching the search pattern (human and female and actress)

Skip to content