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: