An introduction to RDF querying in SPARQL
Zwifi
20.8K views
Grouping results
Aggregation
Aggregation and projection
1
2
3
4
5
6
7
8
9
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?name (COUNT(?role) AS ?nb_roles)
WHERE {
?actor rdf:type yaco:Actor;
rdfs:label ?name;
yaco:playsRole ?role.
} GROUP BY ?actor
1
@prefix yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#> .
Who is he oldest actor?
1
2
3
4
5
6
7
8
9
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?name (MIN(?year) as ?minYear)
WHERE {
?actor rdf:type yaco:Actor;
rdfs:label ?name;
yaco:birthYear ?year.
}
1
@prefix yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#> .
Group filtering
Who are the actors who played in at least two movies?
1
2
3
4
5
6
7
8
9
10
PREFIX yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?name
WHERE {
?actor rdf:type yaco:Actor;
rdfs:label ?name;
yaco:playsRole ?role.
} GROUP BY ?actor
HAVING (COUNT(?role) > 2)
1
@prefix yaco: <https://www.irit.fr/recherches/MELODI/ontologies/cinema#> .
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.