Introduction to RDF Query with SPARQL

Part 1 - Introduction

Dave Beckett

ILRT, University of Bristol, UK

Learning Goals

After this part of the tutorial you should




  • SPARQL is an RDF query language over a protocol

Background of RDF Query Languages

Why do you need a query language (for RDF)?

RDF Query Language Design

The main RDF query language styles:

Most popular by far are the SQL-like languages.

RDQL/Squish is the most popular SQL-like language with multiple independent implementations.

Querying XML compared to Querying RDF

Concept XML RDF
Model Document or
Tree or
Infoset (PSVI)
Set of Triples
= RDF Graph
Atomic Units Elements, Attributes, Text Triples, URIs, Blank Nodes, Text
Identifiers Element/Attribute names
XPointers / XPaths
Described by DTDs
W3C XML Schema
Relax NG
RDF Schema

Comparing XML and RDF query

  • Different models
  • XML query cannot handle all RDF terms

XSLT / XQuery and RDF

Why a standard RDF query language now?

given that RDQL (and others) are widely implemented

So in March 2004 the W3C formed the RDF Data Access Working Group

Use Cases for RDF Query

Some of the use cases DAWG recorded were:

Requirements for RDF query

These led to requirements

from existing languages:
conjunction (AND) of triple patterns with variable bindings and constraints
from use cases:
graphs, datatypes, extension functions, aggregation, alternates, descriptions
from the WG charter:
a protocol.
no rules language, no cursors, no proofs and no updates

SPARQL - Query Language

SPARQL - Protocol

Turtle RDF syntax - URIs and Blank Nodes

Enclosed in <>
Relative URI references turned in URIs (RFC 3986)
@prefix prefix <http://....>
in the style of XML QNames as a shorthand for the full URI
Blank Nodes
[] for a Blank Node used once

Turtle RDF syntax - RDF Literals

"""Long literal with
Datatyped Literals
"lexical form"^^datatype URI
e.g. "10"^^xsd:integer
10 Decimal integer (xsd:integer)
true Boolean (xsd:boolean)
2.5 Double (xsd:double)

Turtle RDF Syntax - Triples and Abbreviations

Triples separated by .
:a :b :c . :d :e :f .
Common triple predicate and subject:
:a :b :c, :d .
which is the same as :a :b :c . :a :b :d .
Common triple subject:
:a :b :c; :d :e . which is the same as: :a :b :c . :a :d :e .
Blank node as a subject
:a :b [ :c :d ] which is the same as: :a :b _:x . _:x :c :d .
for blank node _:x
RDF Collections
:a :b ( :c :d :e :f )
which is short for many triples

Example Dataset of people and who they know

foaf:knows picture

Example Dataset - detail

Example document from Dataset

@prefix  foaf: <> .
@prefix  rdf:  <> .
@prefix  rdfs: <> .
@prefix  dc:   <> .
@prefix  bio:  <> .
@prefix  xsd:  <> .

<> rdf:type   foaf:PersonalProfileDocument ;
   dc:creator <> .

_:a  a             foaf:Person ;
     foaf:name     "Alice" ;
     foaf:mbox     <> ;
     foaf:mbox     <> ;
     foaf:knows    _:b ;
     bio:event     [ a bio:Birth; bio:date "1974-02-28"^^xsd:date ] .

_:b  a             foaf:Person ;
     foaf:name     "Bob" ;
     foaf:mbox     <> ;
     bio:event     [ a bio:Birth; bio:date "1973-01-02"^^xsd:date ] ;
     rdfs:seeAlso  <bobFoaf.rdf> ;
     rdf:type      foaf:Person .

<bobFoaf.rdf> rdf:type foaf:PersonalProfileDocument .

_:a  foaf:knows    _:c .
_:c  foaf:mbox     <> .



  • Using multiple vocabularies declared with @prefix
  • This is semi structured data
  • blank nodes are connecting the people nodes

SPARQL querying online


SPARQL querying on the console with ARQ

Download and install:

Windows Setup:
Set environment variable ARQROOT to the absolute location of the ARQ-0.95 folder. Add the ARQ-0.95/bin folder to the console search PATH.
Unix, Linux, OSX, Cygwin Setup:
Set environment variable ARQROOT to the absolute path of the ARQ-0.9.5/lib directory. Add the ARQ-0.9.5/bin directory to the shell search PATH. Make all the scripts are executable with chmod u+x bin/*.

SPARQL querying on the console with ARQ

$ sparql --query
| name                           | website                              |
| "Norm Walsh"                   | <>          |
| "Dave Beckett"                 | <> |
| "Ikki Ohmukai"                 | <>            |
| "W3C Semantic Web News"        | <>         |
| "Danny Ayers"                  | <>             |

or with XML results:

$ sparql --results rs/xml --query
<?xml version="1.0"?>
<sparql xmlns:rdf=""
    xmlns="" >
    <variable name="name"/>
    <variable name="website"/>

End of Part 1


Run the SPARQL query in by whichever web based or console method you prefer


Copyright 2005 Dave Beckett, Steve Harris, Eric Prud'hommeaux and Andy Seaborne. Terms of use are given on the main Introduction to RDF Query with SPARQL Tutorial page.