In this lesson you will get an introduction to aggregate and compute queries.

You have learned to query and manipulate data with Graql and that will probably constitute the bulk of the language you need for a while. Before we go on to other topics, you might want to know that there are also other kind of queries than get, insert and delete.

In this lesson we will very briefly review them, so that you have a more complete view of the language.


Sometime you do not want to just look at the result set of a get query. Sometimes you actually want do things with it and manipulate it in some way.

Meet aggregate queries.

If you look into it, you will see that the bonds in our knowledge graph have a “risk” score attached. They are just randomly generated numbers, so do not try and look for any pattern, but let us see what we can do with them.

Imagine that you want to look up the minimum risk of a bond issued by the company “ENI”. If you just wanted to find all risk scores for all bonds you would run the following query:

$x isa company has name "ENI";
($x, $y) isa issues;
$y isa bond has risk $r;

If you want to find the minimum risk value you would just minimally adapt the last query and substitute get with aggregate min $r;. This is how it looks like (notice that you cannot run aggregate queries in the graph visualiser as the returned response is not a graph).

Aggregate example

With aggregate queries you can calculate means, standard deviations, counts and many other useful things. You can find the complete list in the documentation.

How would you find the standard deviation of the risk values of all bonds?

Asking things

Answer me quickly: do we have a company named “AwesomePuppies” in our knowledge graph?

Let’s ask the dashboard. Literally. Run this query in the Dashboard Console:

match $x isa company has name "AwesomePuppies";
aggregate ask;

To find out whether something is in the knowledge graph, you just need to use the ask aggregate. If the get query returns no result, then the corresponding ask aggregate will return False. Otherwise it will return True. Nothing fancy, but very useful. Especially if you are building an app on top of Grakn.

Compute queries

The aggregates effectively do their computations on the results of a match query. This means that before doing your calculations, Grakn first has to execute the query and return the results. If you want to do your statistics on large result graphs you would use a lot of resources. To stay efficient you need something more powerful, namely Grakn analytics.

To use Analytics you run compute queries, that have a slightly different syntax. A compute query has the form compute SOMETHING of ATTRIBUTE in SUBTYPES; where SOMETHING stands for the metric you want to compute (like min), ATTRIBUTE is the numeric variable on which you want to perform your computation and SUBTYPES is just a list of entities and relationships that have that attribute. For example

compute min of risk in bond;

Apart from the basic statistics which can be performed with aggregate queries as well, you will be able to use compute queries to run powerful graph analytics on your whole knowledge graph. The topic definitely deserves more attention than this brief example, so we will dedicate a whole module to it later in the series.

What have you learned?

You now know all the query types that Graql supports. You are ready to step up your Grakn level.

What next?

First of all, try and do the Graql review before proceeding to module 3. If you want to know more about the queries introduced in this lesson you can, as always, have a look at the docs.