In SPARQL, there are four query forms - SELECT, CONSTRUCT, ASK, DESCRIBE with different return values.
- SELECT : Return all or subset of the variables bound in a query pattern match
- CONSTRUCT : Returns an RDF graph constructed by substituting variables in a set of triple templates.
- ASK : Returns a Boolean indicating whether a query pattern matches or not.
- Describe : Returns an RDF graph that describes the resources found.
Since we all quite know well about SELECT form, I'd like to give example on other forms here. Suppose we have ontology as below and four books have their prices separately.
Practice#1 : Get books(Subject)→price(Predicate)→price value(Object) triples.
1: PREFIX : <http://boobks.example#>
2: CONSTRUCT { ?book :price ?price }
3: WHERE {
4: ?book :price ?price .
5: }
6: ORDER BY ?book
Query Result :
Practice#2 : Use ASK form to test Subject→affiliates(Predicate)→Object pattern exist or not.
1: PREFIX : <http://boobks.example#>
2: ASK { ?org :affiliates ?author}
Practice#3 : Get RDF graph describes Subject→writesBook(Predicate)→book3(Object).
1: PREFIX : <http://boobks.example#>
2: DESCRIBE ?x
3: WHERE { ?x :writesBook :book3 }
Query Result : It will return RDF graph describe your query pattern.