SPARQL
Nikola Tulechki & Vladimir Alexiev & Svetla Boytcheva
What is SPARQL?
● SPARQL = SPARQL Protocol And Query Language
● SQL-like query language for RDF data
● SPARQL 1.0 only allowed accessing the data (query)
● SPARQL 1.1 introduced:
○ Query extensions:
○ Aggregates, Subqueries, Negation, …
○ Data management updates:
○ Insert, Delete, Delete/Insert
○ Graph management updates:
○ Create, Load, Clear, Drop, Copy, Move, Add
[Link]
What is a SPARQL Query?
Main Idea: Pattern matching
● Queries describe sub-graphs of the queried graph
● Graph patterns are RDF graphs specified in Turtle syntax, which contain
variables (prefixed by either “?” or “$”)
● Sub-graphs that match the graph patterns yield a result
[Link]
A Turtle document is a textual representations of an RDF graph. The following Turtle
document describes the relationship between Green Goblin and Spiderman.
Comments • #
Object list • ,
Literals • “”
Prefixes • @prefix pr: <resource>
Predicate list • ;
Special token • A
Language tag • @en
Simple Triples
SPARQL Queries
ASK • test whether a query patterns has a solution (yes/no)
• returns an RDF graph containing (all) triples about one or more
DESCRIBE resources
SELECT • returns variables & their bindings (RETURNS A TABLE)
• returns an RDF graph specified by a graph template (RETURNS A
CONSTRUCT GRAPH)
• Writes/Deletes an RDF graph specified by a graph template to the
INSERT/DELETE repository
Components of a SPARQL Query
List of namespace
definitions : • PREFIX
Query form + • SELECT, CONSTRUCT, ASK,
variables : DESCRIBE ...
List of data
sources (optional) • FROM, FROM NAMED
:
Query patterns
and filters : • WHERE:
• subqueries, expressions,
BIND, VALUES, FILTER
Solution modifiers: • ORDER BY, LIMIT, OFFSET
Rail diagrams of query syntax
Types of Graph Patterns
Basic graph patterns • A conjunction of triple patterns
OPTIONAL graph • Specifies optional parts of a pattern (similar to an
pattern “outer join” in SQL)
UNION graph • Specifies disjunctions (alternatives)
patterns
SPARQL 1.1 Query Extensions
● Aggregates
● Sub-queries
● Negation (NOT, NOT EXISTS)
● Expressions in the SELECT clause
● Property Paths
● Assignment (BIND)
● Enumeration (VALUES)
● A short form for CONSTRUCT
● An expanded set of functions and operators
Useful Resources
[BOOK] Learning SPARQL: Querying and Updating with SPARQL 1.1. Bob
DuCharme, July 2013 (ed 2), 386 pages
SPARQL by Example tutorial
Wikidata SPARQL tutorial
Rail Diagrams of Query Syntax