SPARQL cheat sheet
Every recipe is executed against the Quickstart dataset while this page is
built. Queries without GRAPH use the union of every named graph, including
the file catalog.
Select values with prefixes
Declare prefixes once, then select named values from a specific set of source graphs.
Source names.rq
SELECT ?graph ?name
WHERE {
GRAPH ?graph {
?subject <https://schema.org/name> ?name .
}
VALUES ?graph {
<sparqld:examples/alpha-centauri.yamlld>
<sparqld:examples/centaurus.md>
<sparqld:examples/proxima-centauri-b.jsonld>
}
}
ORDER BY ?graph
| graph | name |
|---|---|
| sparqld:examples/alpha-centauri.yamlld | Alpha Centauri |
| sparqld:examples/centaurus.md | Centaurus |
| sparqld:examples/proxima-centauri-b.jsonld | Proxima Centauri b |
Join related resources
Shared variables join statements about a star and its constellation.
Source alpha-centauri.rq
PREFIX schema: <https://schema.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?constellation ?description
WHERE {
?star schema:name "Alpha Centauri" ;
dbo:constellation ?constellationResource ;
schema:description ?description .
?constellationResource schema:name ?constellation .
}
| constellation | description |
|---|---|
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
| Centaurus | The closest star system to the Solar System. |
Keep optional values
OPTIONAL retains rows when a property is absent.
Source optional-values.rq
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX schema: <https://schema.org/>
SELECT ?name ?discovered
WHERE {
GRAPH ?graph {
?body schema:name ?name .
OPTIONAL { ?body dbp:discovered ?discovered }
}
FILTER(STRSTARTS(STR(?graph), "sparqld:examples/"))
}
ORDER BY ?name
| name | discovered |
|---|---|
| Alpha Centauri | |
| Alpha Centauri | |
| Alpha Centauri A | |
| Alpha Centauri AB | |
| Alpha Centauri B | |
| Centaurus | |
| Centaurus | |
| Proxima Centauri | |
| Proxima Centauri b | 2016-08-24 |
| Proxima Centauri b | |
| Proxima Centauri d |
Filter values
Functions such as LCASE, STR, and CONTAINS narrow matching bindings.
Source filter-names.rq
PREFIX schema: <https://schema.org/>
SELECT ?name
WHERE {
?subject schema:name ?name .
FILTER(CONTAINS(LCASE(STR(?name)), "centauri"))
}
ORDER BY ?name
| name |
|---|
| Alpha Centauri |
| Alpha Centauri |
| Alpha Centauri A |
| Alpha Centauri AB |
| Alpha Centauri B |
| Proxima Centauri |
| Proxima Centauri b |
| Proxima Centauri b |
| Proxima Centauri d |
Ask whether data exists
ASK returns one boolean instead of a result table.
Source ask-data.rq
ASK {
?subject ?predicate ?object .
}
True
Construct a graph
CONSTRUCT returns RDF assembled from matching bindings.
Source construct-name.rq
CONSTRUCT {
<http://dbpedia.org/resource/Alpha_Centauri>
<https://schema.org/name> ?name .
}
WHERE {
<http://dbpedia.org/resource/Alpha_Centauri>
<https://schema.org/name> ?name .
}
<http://dbpedia.org/resource/Alpha_Centauri> <https://schema.org/name> "Alpha Centauri" .
Count triples by named graph
Aggregate named graphs to inspect data and file-catalog triples separately.
Source graph-counts.rq
SELECT ?graph (COUNT(*) AS ?triples)
WHERE {
GRAPH ?graph { ?subject ?predicate ?object }
FILTER(STRSTARTS(STR(?graph), "sparqld:examples/"))
}
GROUP BY ?graph
ORDER BY DESC(?triples) ?graph
| graph | triples |
|---|---|
| sparqld:examples/markdown-ld/alpha-centauri.md | 21 |
| sparqld:examples/proxima-centauri-b.jsonld | 5 |
| sparqld:examples/alpha-centauri.yamlld | 4 |
| sparqld:examples/centaurus.md | 3 |
Order and paginate
Always pair LIMIT and OFFSET with ORDER BY for stable pages.
Source paginated-names.rq
PREFIX schema: <https://schema.org/>
SELECT ?name
WHERE {
GRAPH ?graph {
?subject schema:name ?name .
}
FILTER(STRSTARTS(STR(?graph), "sparqld:examples/"))
}
ORDER BY ?name
LIMIT 2
OFFSET 1
| name |
|---|
| Alpha Centauri |
| Alpha Centauri A |