SPARQL

From Wiki

Jump to: navigation, search

Contents

Overview

SPARQL is a query language for getting information from RDF graphs. It provides facilities to:

  • extract information in the form of URIs, blank nodes, plain and typed literals.
  • extract RDF subgraphs.
  • construct new RDF graphs based on information in the queried graphs
  • matching graph patterns
  • variables –global scope; indicated by "?"or "$"
  • query terms –based on Turtle syntax
  • terms delimited by "<>" are relative URI references
  • data description format - Turtle
  • XML Results Format
  • Easy to transform (XSLT, XQuery)
  • Status: SPARQL Query Language for RDF, W3C Recommendation January 15, 2008

SPARQL Data Formats

  • SPARQL queries RDF graphs.
  • An RDF graph is a set of triples
  • Notice that Jena calls RDF graphs models and triples statements because that is what they were called at the time the Jena API was first designed.

Basic Triple Pattern

Data

@prefix person: <http://example/person/>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
person:A foaf:name "Alice".
person:A foaf:mbox <mailto:alice@example.net>.
person:B foaf:name "Bob".

Query

PREFIX person: <http://example/person/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
  WHERE { 
    ?x foaf:name ?name 
  }

Result

name
"Alice"
"Bob"

Basic Graph Pattern

Data

@prefix person: <http://example/person/>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
person:A foaf:name "Alice".
person:A foaf:mbox <mailto:alice@example.net>.
person:B foaf:name "Bob"

Query

PREFIX person: <http://example/person/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name
   WHERE { 
      ?person foaf:mbox <mailto:alice@example.net> .
      ?person foaf:name ?name . 
   }.

Result

name
"Alice"

Blank Nodes as Data Values

Filters

Data

s:b1 dc:title "SPARQL Query Language Tutorial" ; 
     inv:price "10" ; inv:quantity "3"^^xs:int.
s:b2 dc:title "SPARQL Query Language"; 
     inv:price "20" ; inv:quantity "5"^^xs:int.
s:b3 dc:title "Moving from SQL to SPARQL" ; 
     inv:price "5" ; inv:quantity "0"^^xs:int.
stock:book4 dc:title "Applying XQuery" ; 
      inv:price "20" ; inv:quantity "8"^^xs:int.

Query

SELECT ?book ?title
 WHERE {
  ?book dc:title ?title.
  ?book inv:price ?price.FILTER ( ?price < 15 )
  ?book inv:quantity ?q.FILTER ( ?q > 0 ) 
 }

Result

book title
s:b1 "SPARQL Query Language Tutorial"

OPTIONAL

Data

@prefix person: <http://example/person/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
person:a foaf:name "Alice"; foaf:nick "Ali1966".
person:b foaf:name "Bob".

Query

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?nick
  WHERE { 
    ?x foaf:name ?name .
    OPTIONAL {?x foaf:nick ?nick }
  }

Result

name nick
"Alice" "Ali1967"
"Bob"

UNION

Data

book:a dc10:title "SPARQL Query Language Tutorial".
book:b dc11:title "SPARQL Query Language (2nd ed)".
book:c dc10:title "SPARQL"; dc11:title "SPARQL".

Query

PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT ?t
 WHERE {
  {?book dc10:title ?t} UNION {?book dc11:title ?}
 }

Result

t
"SPARQL Query Language Tutorial"
"SPARQL"
"SPARQL Query Language (2nd ed)"
"SPARQL"</code


Duplicate solutions –DISTINCT, REDUCED

  • A solution sequence with no DISTINCT or REDUCED query modifier will preserve duplicate solutions.

Data

book:a dc10:title "SPARQL Query Language Tutorial".
book:b dc11:title "SPARQL Query Language (2nd ed)".
book:c dc10:title "SPARQL"; dc11:title "SPARQL".

Query

PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>
SELECT DISTINCT ?t
 WHERE {
  {?book dc10:title ?t} UNION {?book dc11:title ?}
 }

Result

t
<code>"SPARQL Query Language Tutorial"
"SPARQL"
"SPARQL Query Language (2nd ed)"

Filtering solutions –OFFSET, LIMIT

Ordering solutions –ORDER BY

More about Query Forms: SELECT, CONSTRUCT, ASK and DESCRIBE

RDF Datasets

Built-in Functions

XML Format for Results

Tools

== Bibliography ==
Personal tools