Simplified SPARQL Aggregates

September 17th 2011 by Arto

We have observed that SPARQL 1.1’s syntax for aggregates tends to surprise many, if not most, people. According to the standard SPARQL 1.1 grammar, to write a COUNT() query you have to be somewhat verbose:

SELECT (COUNT(*) AS ?count) WHERE {?s ?p ?o}

Note the required explicit variable name, ?count, as well as the required parentheses surrounding the projected expression. With a strictly standards-compliant SPARQL query processor, omitting either the parentheses or the variable binding will result in a syntax error. This was also the case with Dydra until recently.

As of today, however, we also support a simplified and abbreviated convenience syntax for aggregates, one that is more in line with the previous experience and initial expectations of users coming from, say, an SQL background:

SELECT COUNT(*) WHERE {?s ?p ?o}

The same convenience syntax is available for all the familiar COUNT, SUM, MIN, MAX, and AVG aggregates.

To write maximally portable SPARQL queries, you should probably continue to use the standard syntax in any non-trivial software you release. However, when initially exploring your dataset and formulating your queries using the Dydra query editor, we’re sure you’ll quickly find the abbreviated syntax to be a very welcome convenience indeed.

If you have similar pet peeves and pain points with other aspects of the SPARQL standard, let us know. Making your use of SPARQL as easy and pleasant as possible is what we’re here for.

blog comments powered by Disqus