Cheat sheet
The build runs every recipe against the Quickstart dataset. Queries without
GRAPH use the union of every named graph, including the file catalog.
Select values with prefixes
Declare a prefix once, then select names for three related subjects from the shared default graph.
Source names.rq
PREFIX schema: <https://schema.org/>
SELECT DISTINCT ?subject ?name
WHERE {
VALUES ?subject {
<http://dbpedia.org/resource/Alpha_Centauri>
<http://dbpedia.org/resource/Centaurus>
<http://dbpedia.org/resource/Proxima_Centauri_b>
}
?subject schema:name ?name .
FILTER(?name IN ("Alpha Centauri", "Centaurus", "Proxima Centauri b"))
}
ORDER BY ?subject
| subject | name |
|---|---|
| http://dbpedia.org/resource/Alpha_Centauri | Alpha Centauri |
| http://dbpedia.org/resource/Centaurus | Centaurus |
| http://dbpedia.org/resource/Proxima_Centauri_b | 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 DISTINCT ?constellation ?description
WHERE {
<http://dbpedia.org/resource/Alpha_Centauri>
dbo:constellation ?constellationResource ;
schema:description ?description .
?constellationResource schema:name ?constellation .
}
| constellation | description |
|---|---|
| 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 DISTINCT ?name ?discovered
WHERE {
VALUES ?body {
<http://dbpedia.org/resource/Alpha_Centauri>
<http://dbpedia.org/resource/Proxima_Centauri_b>
}
?body schema:name ?name .
OPTIONAL { ?body dbp:discovered ?discovered }
}
ORDER BY ?name
| name | discovered |
|---|---|
| Alpha Centauri | |
| Proxima Centauri b | 2016-08-24 |
Filter values
Functions such as LCASE, STR, and CONTAINS narrow matching bindings.
Source filter-names.rq
PREFIX schema: <https://schema.org/>
SELECT DISTINCT ?name
WHERE {
VALUES ?subject {
<http://dbpedia.org/resource/Alpha_Centauri>
<http://dbpedia.org/resource/Proxima_Centauri_b>
}
?subject schema:name ?name .
FILTER(CONTAINS(LCASE(STR(?name)), "centauri"))
}
ORDER BY ?name
| name |
|---|
| Alpha Centauri |
| Proxima Centauri b |
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 a graph in the Resource Description Framework (RDF)
format, 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
Compare triple counts for three related astronomy example files.
Source graph-counts.rq
SELECT ?graph (COUNT(*) AS ?triples)
WHERE {
VALUES ?graph {
<sparqld:examples/alpha-centauri.yamlld>
<sparqld:examples/centaurus.md>
<sparqld:examples/proxima-centauri-b.jsonld>
}
GRAPH ?graph { ?subject ?predicate ?object }
}
GROUP BY ?graph
ORDER BY DESC(?triples) ?graph
| graph | triples |
|---|---|
| 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 DISTINCT ?name
WHERE {
?subject schema:name ?name .
}
ORDER BY ?name
LIMIT 2
OFFSET 1
| name |
|---|
| Alpha Centauri |
| Alpha Centauri A |