SlideShare a Scribd company logo
SPARQL




1
Query Languages: SQL
(SQL Query Language)
       A language for querying collections of tuples:
               SELECT SALARY, HIRE_DATE
               FROM EMPS
               WHERE EMP_ID = 13954




                  EMP_ID   NAME     HIRE_DATE    SALARY
                  13954    Joe      2000-04-14   48000
                  10335    Mary     1998-11-23   52000
                  …        …        …            …
                  04182    Bob      2005-02-10   21750

    2
Query Languages: XQuery
(XML Query)
       A language for querying trees of XDM nodes:
            for $e in document(my_employees.xml)
            where $emp/emp/@emp-id = 13954
            return $emp/emp/salary



                                                          < e m p lo ye e s>



                                < e m p>                                   < e m p>   <e m p>   <e m p>


               e m p-id = 1 39 54
                                                                               ...      ...       ...


         < n a m e>          < h ire -d a te>   < s a la ry>



           Joe               2 0 00 -04 -14      4 80 00



    3
Why an RDF Query Language?
Different XML Representations
       XML at a lower level of abstraction than RDF
       There are various ways of syntactically representing an
        RDF statement in XML
       Thus we would require several XQuery queries, e.g.
           //uni:lecturer/uni:title if uni:title element
           //uni:lecturer/@uni:title if uni:title attribute
           Both XML representations equivalent!




    4
Two basic families of SQL-like langages for
RDF(S)
       RDQL
           Implementations: Jena, Sesame, RDFStore, ...
       RQL
           Implementations: RQL, SPARQL, ...




    5
Introduction to RDQL
       RDF Data Query Language
       JDBC/ODBC friendly

       Simple:

          SELECT
                    some information
          FROM
                    somewhere
          WHERE
                    this match
          AND
                    these constraints
          USING
                    these vocabularies


    6
Example




7
Example
       q1 contains a query:
SELECT ?x
WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, "John Smith")


       For executing q1with a model m1.rdf:
java jena.rdfquery --data m1.rdf --query q1


       The outcome is:
x
=============================
<http://somewhere/JohnSmith/>




    8
Example
       Return all the resources that have property FN and the
        associated values:
SELECT ?x, ?fname
WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, ?fname)


       The outcome is:
x                                | fname
================================================
<http://somewhere/JohnSmith/>    | "John Smith"
<http://somewhere/SarahJones/>   | "Sarah Jones"
<http://somewhere/MattJones/>    | "Matt Jones"




    9
Example
    Return the first name of Jones:
SELECT ?givenName
WHERE (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Family>, "Jones"),
         (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Given>, ?givenName)


    The outcome is:
givenName
=========
"Matthew"
"Sarah"




    10
URI Prefixes : USING
    RDQL has a syntactic convenience that allows prefix strings
     to be defined in the USING clause :

SELECT ?x
WHERE (?x, vCard:FN, "John Smith")
USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>

SELECT ?givenName
WHERE (?y, vCard:Family, "Smith"),
      (?y, vCard:Given, ?givenName)
USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#>




    11
Filters
    RDQL has a syntactic convenience that allows prefix strings
     to be defined in the USING clause :

SELECT ?resource
WHERE (?resource, info:age, ?age)
AND ?age >= 24
USING info FOR <http://somewhere/peopleInfo#>




    12
Limitations
    Does not take into account semantics of RDF(S)
    For example:
        ex:human rdfs:subClassOf ex:animal
        ex:student rdfs:subClassOf ex:human
        ex:john rdf:type ex:student

         Query: “ To which class does the resource John belong?”
         Expected answer: ex:student, ex:human, ex:animal
         However, the query:

         SELECT ?x
         WHERE (<http://example.org/#john>, rdf:type, ?x)
         USING rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

         Yields only:
         <http://example.org/#student>

    Solution: Inference Engines

    13
Why an RDF Query Language?
Different XML Representations
    XML at a lower level of abstraction than RDF
    There are various ways of syntactically representing an
     RDF statement in XML
    Thus we would require several XPath queries, e.g.
        //uni:lecturer/uni:title if uni:title element
        //uni:lecturer/@uni:title if uni:title attribute
        Both XML representations equivalent!




    14
SPARQL
   SPARQL is RDF Query Language
   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
SPARQL Basic Queries
    SPARQL is based on matching graph patterns
    The simplest graph pattern is the triple pattern :
-    like an RDF triple, but with the possibility of a variable instead of an RDF
     term in the subject, predicate, or object positions
    Combining triple patterns gives a basic graph pattern, where an exact match
     to a graph is needed to fulfill a pattern




    16
Basic graph pattern
   Set of Triple Patterns
     Triple Pattern – similar to an RDF Triple (subject, predicate, object), but any
      component can be a query variable; literal subjects are allowed

                                 ?book dc:title ?title

       Matching a triple pattern to a graph: bindings between variables and RDF
        Terms


   Matching of Basic Graph Patterns
     A Pattern Solution of Graph Pattern GP on graph G is any substitution S
      such that S(GP) is a subgraph of G.
          SELECT ?x ?v
          WHERE { ?x ?x ?v }
                                      rdf:type rdf:type rdf:Property
              x                   v
              rdf:type            rdf:Property
Examples
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?c
WHERE
{
    ?c rdf:type rdfs:Class .
}
 Retrieves all triple patterns, where:
- the property is rdf:type
- the object is rdfs:Class
 Which means that it retrieves all classes




 18
Examples (2)
    Get all instances of a particular class (e.g. course) :

PREFIX uni: <http://www.mydomain.org/uni-ns#>
SELECT ?i
WHERE
{
  ?i rdf:type uni:course .
}




    19
Multiple Matches
                              Data    @prefix foaf: <http://xmlns.com/foaf/0.1/> .
                                      _:a foaf:name "Johnny Lee Outlaw" .
                                      _:a foaf:mbox <mailto:jlow@example.com> .
                                      _:b foaf:name "Peter Goodguy" .
                                      _:b foaf:mbox <mailto:peter@example.org> .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>   Query
SELECT ?name ?mbox                                     Group Graph Pattern
WHERE                                                  (set of graph patterns)
{ ?x foaf:name ?name .                                 also!
 ?x foaf:mbox ?mbox }

                 Query Result     name              mbox
                                  "Johnny Lee       <mailto:jlow@example.com>
                                  Outlaw"
                                  "Peter Goodguy" <mailto:peter@example.org>
Using select-from-where
    As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure:
      SELECT specifies the projection: the number and order of retrieved data
      FROM is used to specify the source being queried (optional)
      WHERE imposes constraints on possible solutions in the form of graph
       pattern templates and boolean constraints

    Retrieve all phone numbers of staff members:

            SELECT ?x ?y
            WHERE
            { ?x uni:phone ?y .}

    Here ?x and ?y are variables, and ?x uni:phone ?y represents a resource-
     property-value triple pattern




    21
Implicit Join
    Retrieve all lecturers and their phone numbers:
             SELECT ?x ?y
             WHERE
             { ?x rdf:type uni:Lecturer ;
                       uni:phone ?y . }
    Implicit join: We restrict the second pattern only to those triples, the resource
     of which is in the variable ?x
      Notice the syntax shortcut : a semicolon indicates that the following
        triple shares its subject with the previous one




    22
Implicit join (2)
    The previous query is equivalent to writing:
         SELECT ?x ?y
         WHERE
         {
             ?x rdf:type uni:Lecturer .
             ?x uni:phone ?y .
         }




    23
Explicit Join
    Retrieve the name of all courses taught by the lecturer with ID 949352
     SELECT ?n
     WHERE
     {
         ?x rdf:type uni:Course ;
            uni:isTaughtBy 949352 .
         ?c uni:name ?n .
         FILTER (?c = ?x) .
     }




    24
Basic Graph Pattern - Blank Nodes

             @prefix foaf: <http://xmlns.com/foaf/0.1/> .
    Data
             _:a foaf:name "Alice" .
             _:b foaf:name "Bob" .


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


                           x      name
            Query Result   _:c    “Alice“
                           _:d    “Bob”
Group Pattern
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
{ ?x foaf:name ?name .
 ?x foaf:mbox ?mbox }



PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox
WHERE
{ {?x foaf:name ?name;
     foaf:mbox ?mbox }
 }
Value Constraints
                         @prefix dc: <http://purl.org/dc/elements/1.1/> .
                Data     @prefix : <http://example.org/book/> .
                         @prefix ns: <http://example.org/ns#> .
                         :book1 dc:title "SPARQL Tutorial" .
                         :book1 ns:price 42 .
                         :book2 dc:title "The Semantic Web" .
                         :book2 ns:price 23 .

PREFIX dc: <http://purl.org/dc/elements/1.1/>        Query
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price
WHERE { ?x ns:price ?price .
         FILTER ?price < 30 .
         ?x dc:title ?title . }
                                            title                      price
                            Query Result
                                            "The Semantic Web"         23
Regular expressions can be used
•   “Find the name and email addresses of authors of a paper
    about SPARQL”



PREFIX dc:     <http://purl.org/dc/elements/1.1/>
PREFIX ldap:   <http://ldap.hp.com/people#>
PREFIX foaf:

SELECT ?name ?name2
{
 WHERE ?doc          dc:title     ?title .
    FILTER regex(?title, “SPARQL”) .
    ?doc          dc:creator   ?reseacher .
    ?researcher   ldap:email      ?email .
    ?researcher   ldap:name       ?name
}




                                                           28
Optional graph patterns
<uni:lecturer rdf:about=“949352”>
     <uni:name>Grigoris Antoniou</uni:name>
</uni:lecturer>
<uni:professor rdf:about=“94318”>
     <uni:name>David Billington</uni:name>
     <uni:email>david@work.example.org</uni:email>
</uni:professor>


    For one lecturer it only lists the name
    For the other it also lists the email address




    29
Optional graph patterns (2)
    All lecturers and their email addresses:
SELECT ?name ?email
WHERE
{ ?x rdf:type uni:Lecturer ;
           uni:name ?name ;
           uni:email ?email .
}
    The result of the previous query would be:

         ?name                          ?email
         David Billington               david@work.example.org


    Grigoris Antoniou is listed as a lecturer, but he has no e-mail address


    30
Optional graph patterns(3)
    As a solution we can adapt the query to use an optional pattern:

     SELECT ?name ?email
     WHERE
     { ?x rdf:type uni:Lecturer ;
           uni:name ?name .
           OPTIONAL { x? uni:email ?email }
     }
    The meaning is roughly “give us the names of lecturers, and if known also their e-
     mail address”
    The result looks like this:

               ?name                           ?email
               Grigoris Antoniou
               David Billington                david@work.example.org

    31
Optional graph patterns (4)
                   @prefix dc: <http://purl.org/dc/elements/1.1/> .
                   @prefix : <http://example.org/book/> .
          Data
                   @prefix ns: <http://example.org/ns#> .
                   :book1 dc:title "SPARQL Tutorial" .
                   :book1 ns:price 42 .
                   :book2 dc:title "The Semantic Web" .
                   :book2 ns:price 23 .

PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX ns: <http://example.org/ns#>
SELECT ?title ?price                              Query
WHERE { ?x dc:title ?title .
         OPTIONAL { ?x ns:price ?price .
                         FILTER ?price < 30 }}    title               price
                                                  “SPARQL Tutorial“
                                Query Result
                                                  "The Semantic Web" 23
Multiple Optional Blocks
                  @prefix foaf: <http://xmlns.com/foaf/0.1/> .
                  @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
           Data
                  @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
                  _:a foaf:name "Alice" .
                  _:a foaf:homepage <http://work.example.org/alice/> .
                  _:b foaf:name "Bob" .
                  _:b foaf:mbox <mailto:bob@work.example> .

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?mbox ?hpage                           Query
WHERE { ?x foaf:name ?name .
         OPTIONAL { ?x foaf:mbox ?mbox }.
OPTIONAL { ?x foaf:homepage ?hpage } }
                                                               Query Result
name      Mbox                            hpage
“Alice“                                   <http://work.example.org/alice/>
“Bob“     <mailto:bob@example.com>
Alternative Graph Patterns
                 @prefix dc10: <http://purl.org/dc/elements/1.0/> .
                 @prefix dc11: <http://purl.org/dc/elements/1.1/> .
         Data
                 _:a dc10:title "SPARQL Query Language Tutorial" .
                 _:b dc11:title "SPARQL Protocol Tutorial" .
                 _:c dc10:title "SPARQL" .
                 _:c dc11:title "SPARQL (updated)" .

PREFIX dc10: <http://purl.org/dc/elements/1.0/>
PREFIX dc11: <http://purl.org/dc/elements/1.1/>                   Query
SELECT ?x ?y
WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } }   Query Result
x                                       y
                                        "SPARQL (updated)"
                                        "SPARQL Protocol Tutorial"
"SPARQL"
"SPARQL Query Language Tutorial"

More Related Content

PPTX
4 sw architectures and sparql
PPTX
SPARQL
PPTX
SWT Lecture Session 3 - SPARQL
PPTX
SWT Lecture Session 11 - R2RML part 2
PPTX
SWT Lecture Session 10 R2RML Part 1
PPTX
Protocol-Oriented Programming in Swift
PPTX
SWT Lecture Session 9 - RDB2RDF direct mapping
KEY
XQuery - a technical overview
4 sw architectures and sparql
SPARQL
SWT Lecture Session 3 - SPARQL
SWT Lecture Session 11 - R2RML part 2
SWT Lecture Session 10 R2RML Part 1
Protocol-Oriented Programming in Swift
SWT Lecture Session 9 - RDB2RDF direct mapping
XQuery - a technical overview

What's hot (20)

PPTX
SWT Lecture Session 2 - RDF
PPT
inheritance
PPTX
Introduction to SPARQL
ODP
Graph Data -- RDF and Property Graphs
PPT
Querying the Semantic Web with SPARQL
PDF
Data translation with SPARQL 1.1
PPT
From SQL to SPARQL
PPTX
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
PDF
Querying XML: XPath and XQuery
PDF
The Gremlin in the Graph
PDF
Types and perl language
PPTX
Adventures in TclOO
PDF
Introduction to XPath
PDF
XSPARQL CrEDIBLE workshop
PPT
XML and XPath details
PDF
What can scala puzzlers teach us
PPT
SPARQL in a nutshell
PPT
PDF
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
SWT Lecture Session 2 - RDF
inheritance
Introduction to SPARQL
Graph Data -- RDF and Property Graphs
Querying the Semantic Web with SPARQL
Data translation with SPARQL 1.1
From SQL to SPARQL
A Preliminary survey of RDF/Neo4j as backends for KnetMiner
Querying XML: XPath and XQuery
The Gremlin in the Graph
Types and perl language
Adventures in TclOO
Introduction to XPath
XSPARQL CrEDIBLE workshop
XML and XPath details
What can scala puzzlers teach us
SPARQL in a nutshell
A Generic Mapping-based Query Translation from SPARQL to Various Target Datab...
Ad

Viewers also liked (8)

PDF
NARYBEK CITY MoU with Uijeonbu city
PDF
PDF
Metaksan full
PDF
Chapter 1 introduction
PPT
Congratulations andrew!
PPT
Examples of my proffesional skills (in Danish)
PPTX
Brug Mindre Tid På Mails
NARYBEK CITY MoU with Uijeonbu city
Metaksan full
Chapter 1 introduction
Congratulations andrew!
Examples of my proffesional skills (in Danish)
Brug Mindre Tid På Mails
Ad

Similar to Sparql (20)

PPTX
Semantic web meetup – sparql tutorial
PPTX
SPARQL introduction and training (130+ slides with exercices)
PDF
Semantic Web(Web 3.0) SPARQL
PDF
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...
PPTX
Introduction to SPARQL
PPT
Semantic Web
PPT
Semantic Web
PDF
A Hands On Overview Of The Semantic Web
PDF
Two graph data models : RDF and Property Graphs
PPT
Aidan's PhD Viva
PPT
PPTX
Towards an RDF Validation Language based on Regular Expression Derivatives
PPTX
The Semantic Web #10 - SPARQL
PPTX
ShEx by Example
PDF
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs
PDF
Rdf data-model-and-storage
PPTX
Jena Programming
PPTX
Triplestore and SPARQL
PPT
A Semantic Multimedia Web (Part 2)
PPTX
SWT Lecture Session 5 - RDFS
Semantic web meetup – sparql tutorial
SPARQL introduction and training (130+ slides with exercices)
Semantic Web(Web 3.0) SPARQL
TermPicker: Enabling the Reuse of Vocabulary Terms by Exploiting Data from th...
Introduction to SPARQL
Semantic Web
Semantic Web
A Hands On Overview Of The Semantic Web
Two graph data models : RDF and Property Graphs
Aidan's PhD Viva
Towards an RDF Validation Language based on Regular Expression Derivatives
The Semantic Web #10 - SPARQL
ShEx by Example
2011 4IZ440 Semantic Web – RDF, SPARQL, and software APIs
Rdf data-model-and-storage
Jena Programming
Triplestore and SPARQL
A Semantic Multimedia Web (Part 2)
SWT Lecture Session 5 - RDFS

Sparql

  • 2. Query Languages: SQL (SQL Query Language)  A language for querying collections of tuples: SELECT SALARY, HIRE_DATE FROM EMPS WHERE EMP_ID = 13954 EMP_ID NAME HIRE_DATE SALARY 13954 Joe 2000-04-14 48000 10335 Mary 1998-11-23 52000 … … … … 04182 Bob 2005-02-10 21750 2
  • 3. Query Languages: XQuery (XML Query)  A language for querying trees of XDM nodes: for $e in document(my_employees.xml) where $emp/emp/@emp-id = 13954 return $emp/emp/salary < e m p lo ye e s> < e m p> < e m p> <e m p> <e m p> e m p-id = 1 39 54 ... ... ... < n a m e> < h ire -d a te> < s a la ry> Joe 2 0 00 -04 -14 4 80 00 3
  • 4. Why an RDF Query Language? Different XML Representations  XML at a lower level of abstraction than RDF  There are various ways of syntactically representing an RDF statement in XML  Thus we would require several XQuery queries, e.g.  //uni:lecturer/uni:title if uni:title element  //uni:lecturer/@uni:title if uni:title attribute  Both XML representations equivalent! 4
  • 5. Two basic families of SQL-like langages for RDF(S)  RDQL  Implementations: Jena, Sesame, RDFStore, ...  RQL  Implementations: RQL, SPARQL, ... 5
  • 6. Introduction to RDQL  RDF Data Query Language  JDBC/ODBC friendly  Simple: SELECT some information FROM somewhere WHERE this match AND these constraints USING these vocabularies 6
  • 8. Example  q1 contains a query: SELECT ?x WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, "John Smith")  For executing q1with a model m1.rdf: java jena.rdfquery --data m1.rdf --query q1  The outcome is: x ============================= <http://somewhere/JohnSmith/> 8
  • 9. Example  Return all the resources that have property FN and the associated values: SELECT ?x, ?fname WHERE (?x, <http://www.w3.org/2001/vcard-rdf/3.0#FN>, ?fname)  The outcome is: x | fname ================================================ <http://somewhere/JohnSmith/> | "John Smith" <http://somewhere/SarahJones/> | "Sarah Jones" <http://somewhere/MattJones/> | "Matt Jones" 9
  • 10. Example  Return the first name of Jones: SELECT ?givenName WHERE (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Family>, "Jones"), (?y, <http://www.w3.org/2001/vcard-rdf/3.0#Given>, ?givenName)  The outcome is: givenName ========= "Matthew" "Sarah" 10
  • 11. URI Prefixes : USING  RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?x WHERE (?x, vCard:FN, "John Smith") USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#> SELECT ?givenName WHERE (?y, vCard:Family, "Smith"), (?y, vCard:Given, ?givenName) USING vCard FOR <http://www.w3.org/2001/vcard-rdf/3.0#> 11
  • 12. Filters  RDQL has a syntactic convenience that allows prefix strings to be defined in the USING clause : SELECT ?resource WHERE (?resource, info:age, ?age) AND ?age >= 24 USING info FOR <http://somewhere/peopleInfo#> 12
  • 13. Limitations  Does not take into account semantics of RDF(S)  For example: ex:human rdfs:subClassOf ex:animal ex:student rdfs:subClassOf ex:human ex:john rdf:type ex:student Query: “ To which class does the resource John belong?” Expected answer: ex:student, ex:human, ex:animal However, the query: SELECT ?x WHERE (<http://example.org/#john>, rdf:type, ?x) USING rdf FOR <http://www.w3.org/1999/02/22-rdf-syntax-ns#> Yields only: <http://example.org/#student>  Solution: Inference Engines 13
  • 14. Why an RDF Query Language? Different XML Representations  XML at a lower level of abstraction than RDF  There are various ways of syntactically representing an RDF statement in XML  Thus we would require several XPath queries, e.g.  //uni:lecturer/uni:title if uni:title element  //uni:lecturer/@uni:title if uni:title attribute  Both XML representations equivalent! 14
  • 15. SPARQL  SPARQL is RDF Query Language  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
  • 16. SPARQL Basic Queries  SPARQL is based on matching graph patterns  The simplest graph pattern is the triple pattern : - like an RDF triple, but with the possibility of a variable instead of an RDF term in the subject, predicate, or object positions  Combining triple patterns gives a basic graph pattern, where an exact match to a graph is needed to fulfill a pattern 16
  • 17. Basic graph pattern  Set of Triple Patterns  Triple Pattern – similar to an RDF Triple (subject, predicate, object), but any component can be a query variable; literal subjects are allowed ?book dc:title ?title  Matching a triple pattern to a graph: bindings between variables and RDF Terms  Matching of Basic Graph Patterns  A Pattern Solution of Graph Pattern GP on graph G is any substitution S such that S(GP) is a subgraph of G. SELECT ?x ?v WHERE { ?x ?x ?v } rdf:type rdf:type rdf:Property x v rdf:type rdf:Property
  • 18. Examples PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT ?c WHERE { ?c rdf:type rdfs:Class . }  Retrieves all triple patterns, where: - the property is rdf:type - the object is rdfs:Class  Which means that it retrieves all classes 18
  • 19. Examples (2)  Get all instances of a particular class (e.g. course) : PREFIX uni: <http://www.mydomain.org/uni-ns#> SELECT ?i WHERE { ?i rdf:type uni:course . } 19
  • 20. Multiple Matches Data @prefix foaf: <http://xmlns.com/foaf/0.1/> . _:a foaf:name "Johnny Lee Outlaw" . _:a foaf:mbox <mailto:jlow@example.com> . _:b foaf:name "Peter Goodguy" . _:b foaf:mbox <mailto:peter@example.org> . PREFIX foaf: <http://xmlns.com/foaf/0.1/> Query SELECT ?name ?mbox Group Graph Pattern WHERE (set of graph patterns) { ?x foaf:name ?name . also! ?x foaf:mbox ?mbox } Query Result name mbox "Johnny Lee <mailto:jlow@example.com> Outlaw" "Peter Goodguy" <mailto:peter@example.org>
  • 21. Using select-from-where  As in SQL, SPARQL queries have a SELECT-FROM-WHERE structure:  SELECT specifies the projection: the number and order of retrieved data  FROM is used to specify the source being queried (optional)  WHERE imposes constraints on possible solutions in the form of graph pattern templates and boolean constraints  Retrieve all phone numbers of staff members: SELECT ?x ?y WHERE { ?x uni:phone ?y .}  Here ?x and ?y are variables, and ?x uni:phone ?y represents a resource- property-value triple pattern 21
  • 22. Implicit Join  Retrieve all lecturers and their phone numbers: SELECT ?x ?y WHERE { ?x rdf:type uni:Lecturer ; uni:phone ?y . }  Implicit join: We restrict the second pattern only to those triples, the resource of which is in the variable ?x  Notice the syntax shortcut : a semicolon indicates that the following triple shares its subject with the previous one 22
  • 23. Implicit join (2)  The previous query is equivalent to writing: SELECT ?x ?y WHERE { ?x rdf:type uni:Lecturer . ?x uni:phone ?y . } 23
  • 24. Explicit Join  Retrieve the name of all courses taught by the lecturer with ID 949352 SELECT ?n WHERE { ?x rdf:type uni:Course ; uni:isTaughtBy 949352 . ?c uni:name ?n . FILTER (?c = ?x) . } 24
  • 25. Basic Graph Pattern - Blank Nodes @prefix foaf: <http://xmlns.com/foaf/0.1/> . Data _:a foaf:name "Alice" . _:b foaf:name "Bob" . PREFIX foaf: <http://xmlns.com/foaf/0.1/> Query SELECT ?x ?name WHERE { ?x foaf:name ?name } x name Query Result _:c “Alice“ _:d “Bob”
  • 26. Group Pattern PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { ?x foaf:name ?name . ?x foaf:mbox ?mbox } PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox WHERE { {?x foaf:name ?name; foaf:mbox ?mbox } }
  • 27. Value Constraints @prefix dc: <http://purl.org/dc/elements/1.1/> . Data @prefix : <http://example.org/book/> . @prefix ns: <http://example.org/ns#> . :book1 dc:title "SPARQL Tutorial" . :book1 ns:price 42 . :book2 dc:title "The Semantic Web" . :book2 ns:price 23 . PREFIX dc: <http://purl.org/dc/elements/1.1/> Query PREFIX ns: <http://example.org/ns#> SELECT ?title ?price WHERE { ?x ns:price ?price . FILTER ?price < 30 . ?x dc:title ?title . } title price Query Result "The Semantic Web" 23
  • 28. Regular expressions can be used • “Find the name and email addresses of authors of a paper about SPARQL” PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ldap: <http://ldap.hp.com/people#> PREFIX foaf: SELECT ?name ?name2 { WHERE ?doc dc:title ?title . FILTER regex(?title, “SPARQL”) . ?doc dc:creator ?reseacher . ?researcher ldap:email ?email . ?researcher ldap:name ?name } 28
  • 29. Optional graph patterns <uni:lecturer rdf:about=“949352”> <uni:name>Grigoris Antoniou</uni:name> </uni:lecturer> <uni:professor rdf:about=“94318”> <uni:name>David Billington</uni:name> <uni:email>david@work.example.org</uni:email> </uni:professor>  For one lecturer it only lists the name  For the other it also lists the email address 29
  • 30. Optional graph patterns (2)  All lecturers and their email addresses: SELECT ?name ?email WHERE { ?x rdf:type uni:Lecturer ; uni:name ?name ; uni:email ?email . }  The result of the previous query would be: ?name ?email David Billington david@work.example.org  Grigoris Antoniou is listed as a lecturer, but he has no e-mail address 30
  • 31. Optional graph patterns(3)  As a solution we can adapt the query to use an optional pattern: SELECT ?name ?email WHERE { ?x rdf:type uni:Lecturer ; uni:name ?name . OPTIONAL { x? uni:email ?email } }  The meaning is roughly “give us the names of lecturers, and if known also their e- mail address”  The result looks like this: ?name ?email Grigoris Antoniou David Billington david@work.example.org 31
  • 32. Optional graph patterns (4) @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix : <http://example.org/book/> . Data @prefix ns: <http://example.org/ns#> . :book1 dc:title "SPARQL Tutorial" . :book1 ns:price 42 . :book2 dc:title "The Semantic Web" . :book2 ns:price 23 . PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX ns: <http://example.org/ns#> SELECT ?title ?price Query WHERE { ?x dc:title ?title . OPTIONAL { ?x ns:price ?price . FILTER ?price < 30 }} title price “SPARQL Tutorial“ Query Result "The Semantic Web" 23
  • 33. Multiple Optional Blocks @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . Data @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . _:a foaf:name "Alice" . _:a foaf:homepage <http://work.example.org/alice/> . _:b foaf:name "Bob" . _:b foaf:mbox <mailto:bob@work.example> . PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?name ?mbox ?hpage Query WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox }. OPTIONAL { ?x foaf:homepage ?hpage } } Query Result name Mbox hpage “Alice“ <http://work.example.org/alice/> “Bob“ <mailto:bob@example.com>
  • 34. Alternative Graph Patterns @prefix dc10: <http://purl.org/dc/elements/1.0/> . @prefix dc11: <http://purl.org/dc/elements/1.1/> . Data _:a dc10:title "SPARQL Query Language Tutorial" . _:b dc11:title "SPARQL Protocol Tutorial" . _:c dc10:title "SPARQL" . _:c dc11:title "SPARQL (updated)" . PREFIX dc10: <http://purl.org/dc/elements/1.0/> PREFIX dc11: <http://purl.org/dc/elements/1.1/> Query SELECT ?x ?y WHERE { { ?book dc10:title ?x } UNION { ?book dc11:title ?y } } Query Result x y "SPARQL (updated)" "SPARQL Protocol Tutorial" "SPARQL" "SPARQL Query Language Tutorial"