The queries so far found matching rows and showed them as they were. This lesson groups the rows and counts them, follows several arrows in one step, and turns results into new triples.
Counting: COUNT and GROUP BY
COUNT counts the rows found. A function that summarises many rows as one value like this is called an aggregate function. A computed value needs a variable name given with AS before it appears in the result.
PREFIX ex: <https://example.org/>
SELECT (COUNT(?pet) AS ?n) WHERE {
?person ex:keeps ?pet .
}
The pattern finds three rows and the answer is one. All the rows found form a single group, summarised as one count.
To count per person, group the rows with GROUP BY. Rows for the same person form one group, and an aggregate function is computed once per group.
SELECT ?person (COUNT(?pet) AS ?n) WHERE {
GROUP BY ?person
Rows with the same ?person were grouped, and each group gives one row. ?person is named in GROUP BY, so it can appear in the SELECT as it is.
A condition on a group uses HAVING, not FILTER. FILTER applies to rows before grouping, HAVING to the results after it.
HAVING (COUNT(?pet) > 1)
A count is known only after grouping, so this condition cannot be a FILTER. HAVING is checked once per group.
| Function | What it does |
|---|---|
COUNT(?x), COUNT(*) | How many have a value, how many rows |
COUNT(DISTINCT ?x) | How many different values |
SUM, AVG | Total, average |
MIN, MAX | Smallest value, largest value |
GROUP_CONCAT | Joins the values in a group into one string |
SAMPLE | Any one value from the group |
Following arrows in a row: property paths
Asking "what kinds of animal does Minji keep" means crossing two arrows. You can write two lines joined by a variable in the middle, or put a path in the predicate position and finish in one line.
| Notation | Meaning | Example |
|---|---|---|
p/q | Follow p, then q | ex:keeps/a |
p|q | p or q | rdfs:label|skos:prefLabel |
^p | Follow p backwards | ^ex:keeps (who keeps it) |
p* | Follow p zero or more times | rdfs:subClassOf* |
p+ | Follow p one or more times | ex:partOf+ |
p? | Follow p zero times or once | rdfs:subClassOf? |
SELECT ?person ?kind WHERE {
?person ex:keeps/a ?kind .
Follow ex:keeps, then a (the type). The animal in the middle was not given a variable, so it does not appear in the result.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?pet WHERE {
?pet a/rdfs:subClassOf* ex:Animal .
Nowhere does the file say "Nabi is an animal", and nothing was inferred. The query starts at the type and follows rdfs:subClassOf zero or more times until it reaches ex:Animal. Notice that no dashed arrow appeared in the graph.
a/rdfs:subClassOf* is the path seen most often in practice. Even on a store with reasoning turned off it finds "everything belonging to any kind of animal". Unlike inference, it does not create new facts and keep them. It only follows arrows inside this one query.
Getting a graph back: CONSTRUCT
The answer to a SELECT is a table. CONSTRUCT fills a template for each row found, producing new triples, and returns them as a graph. It is used to move data written in one vocabulary into another, or to cut the needed part out of a large graph.
CONSTRUCT {
?pet ex:keptBy ?person .
WHERE {
?person ex:keeps ?pet .
}
For each row found, the template above is filled in to make one triple. The answer is three ex:keptBy statements pointing the other way. The original graph is unchanged, and the returned graph can be stored and used separately.
The four query forms
| Form | Answer | Used for |
|---|---|---|
SELECT | A table of variables and values | Most lookups |
ASK | True or false | Checking that a condition holds, tests |
CONSTRUCT | A newly built graph | Conversion, extracting a subgraph |
DESCRIBE | A graph describing a thing | What comes back is up to the store |
ASK {
ex:junho ex:keeps/a ex:Dog .
This asks whether anything Junho keeps is a dog. Nothing matches, so the answer is false. Change ex:junho to ex:minji and it becomes true.
Common misunderstandings
- The
*in a property path is the same as inference. - A path follows arrows only while this query looks for its answer. Inference produces new triples that other queries and rules see as well.
COUNT(?x)counts rows.- It counts the rows where
?xhas a value. Rows left blank by an OPTIONAL are not counted.COUNT(*)counts rows. - With GROUP BY, any variable can still go in the SELECT.
- Only the variables named in GROUP BY and the results of aggregate functions can. A variable with several values in one group has no single value to show.
Aggregates group rows and summarise them, paths follow several arrows in one line, and CONSTRUCT turns the answer back into a graph.