Skip main navigation
We use cookies to give you a better experience, if that’s ok you can close this message and carry on browsing. For more info read our cookies policy.
We use cookies to give you a better experience. Carry on browsing if you're happy with this, or read our cookies policy for more information.

Disjunction

Suppose that for some reason you want to construct triples for albums made either by the Beatles or by the Smashing Pumpkins (or both).

Including both of these constraints in the WHERE list will not work, because implicitly the list represents a conjunction of statements, each of which must be satisfied.

To allow disjunctions, SPARQL contains a UNION pattern; this is formed by placing the keyword UNION between two subsets of statements, each subset delimited by curly brackets. The meaning is that variable bindings should be retrieved if they satisfy either the statements on the left or the statements on the right (or both). Thus our target query is formed as follows:

PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX mo: <http://purl.org/ontology/mo/>

CONSTRUCT { ?album dc:creator ?band .
            ?track dc:creator ?band .
          }
WHERE { ?band foaf:made ?album .
        ?album mo:record ?record .
        ?record mo:track ?track .
        { ?band foaf:name "The Beatles" }
          UNION
        { ?band foaf:name "The Smashing Pumpkins" }
      }

Note that the first three statements of the WHERE list lie outside the scope of the UNION operator.


This work is a derivative of ‘Using Linked Data Effectively’ by The Open University (2014) and licensed under CC by 4.0 International Licence adapted and used by the University of Southampton. http://www.euclid-project.eu/

Share this article:

This article is from the free online course:

Introduction to Linked Data and the Semantic Web

University of Southampton

Get a taste of this course

Find out what this course is like by previewing some of the course steps before you join:

  • Welcome to the course
    Welcome to the course
    video

    Watch Dr Elena Simperl & Dr Barry Norton explain how this short course on linked data & the semantic web can help you use this technology in your work

  • Developing real world applications
    Developing real world applications
    video

    Watch Dr Barry Norton describing some real world applications that have Linked Data as their underlying technology.

  • Welcome to Week 2
    Welcome to Week 2
    video

    Watch Dr Barry Norton explain what you will learn about SPARQL (the query language) on this course and what you will be practicing.

Contact FutureLearn for Support