Submit Feedback/Bug Report

myExperiment Logo How To SPARQL

                    Back to Contents Page

4. WHERE

The WHERE clause of a SPARQL query defines where you want to find values for the variables you have defined in the SELECT clause. Inside the curly parenthesis {} the basic unit is a triple, this is made up of three components, a subject, a predicate and an object. This is much like the grammatical structure of a basic natural language sentence.

The    boycatchesa     ball
SubjectPredicateObject

In SPARQL the subject, predicate or object can take one of two forms a variable which is defined by putting a ? prior to the variable name, (e.g. ?a, ?text, etc.) or a Universal Resource Identifier (URI). As discussed in PREFIX (and BASE) this can take one of two forms depending on whether a prefix for the namespace of the URI has been defined within the query. These triples can then be concatenated together using a full-stop (.). By doing this you can build an interconnected graph of nodes joined by relationships. It is generally good to ensure that each triple connects at some point in the graph. The first of the following query is a completely connected graph where as the second isn't and therefore will return the superset of all possible homepage/mbox combinations. This is such a high number of results that there is intentionally not a link to run this query as the webserver with run out of memory trying to process all the results.

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?homepage ?mbox
WHERE {
  ?x rdf:type mebase:User .
  ?x foaf:homepage ?homepage .
  ?x foaf:mbox ?mbox
}
[Run]
[Hide Example Results]
homepagembox
http://www.linkedin.com/in/drneilravenhillmailto:neilr@alto-marketing.com
http://www.msu.edu/~lampeclimailto:lampecli@msu.edu
http://pbeltrao.blogspot.commailto:pedrobeltrao@gmail.com
http://bibliogum.wordpress.com/mailto:bibliogum@yahoo.fr
http://www.leaphish.com/102/mailto:jhnkelly010@gmail.com
http://www.informatics.sussex.ac.uk/escience-usabilitymailto:hilarys@sussex.ac.uk
http://www.science.uva.nl/~adammailto:adam@science.uva.nl
http://www.rug.nl/staff/m.a.swertzmailto:m.a.swertz@rug.nl
http://www.ucps.k12.nc.usmailto:tom.moncrief@ucps.k12.nc.us
http://libresoft.esmailto:israel.herraiz@urjc.es
http://ianfoster.typepad.commailto:foster@mcs.anl.gov

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?homepage ?mbox
WHERE {
  ?x rdf:type mebase:User .
  ?x foaf:homepage ?homepage .
  ?y foaf:mbox ?mbox
}

As like the query above it is not unusual to have the same subject multiple times over. A semi-colon (;) rather than a full-stop (.) can be used after each triple to replace the subject for the next triple if it is the same, leaving you only needing to define the predicate and object. This is useful because it helps reduce the chances typos like the one previously described. The query below is the same as the previous query but uses this shorthand syntax:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?homepage ?mbox
WHERE {
  ?x rdf:type mebase:User ;
    foaf:homepage ?homepage ;
    foaf:mbox ?mbox
}
[Run]
[Hide Example Results]
homepagembox
http://www.linkedin.com/in/drneilravenhillmailto:neilr@alto-marketing.com
http://www.msu.edu/~lampeclimailto:lampecli@msu.edu
http://pbeltrao.blogspot.commailto:pedrobeltrao@gmail.com
http://bibliogum.wordpress.com/mailto:bibliogum@yahoo.fr
http://www.leaphish.com/102/mailto:jhnkelly010@gmail.com
http://www.informatics.sussex.ac.uk/escience-usabilitymailto:hilarys@sussex.ac.uk
http://www.science.uva.nl/~adammailto:adam@science.uva.nl
http://www.rug.nl/staff/m.a.swertzmailto:m.a.swertz@rug.nl
http://www.ucps.k12.nc.usmailto:tom.moncrief@ucps.k12.nc.us
http://libresoft.esmailto:israel.herraiz@urjc.es
http://ianfoster.typepad.commailto:foster@mcs.anl.gov

Sometimes as well as having the same subject multiple times over you may also have the same predicate. in this case you can use a comma (,) to separate each object. Another way to save time writing your query is to use 'a' rather than rdf:type to specify the type of a particular entity.

BASE <http://www.myexperiment.org/>
PREFIX ore: <http://www.openarchives.org/ore/terms/>
PREFIX mepack: <http://rdf.myexperiment.org/ontologies/packs/>

SELECT ?pack
WHERE {
  ?pack a mepack:Pack ;
        ore:aggregates <workflows/181>, <workflows/246>
}
[Run]
[Hide Example Results]
pack
http://www.myexperiment.org/packs/1
http://www.myexperiment.org/packs/47

4.1. UNION

The UNION clause allows you to return results where you want to match multiple patterns. An example of this may be returning all comments and ratings for a particular workflow:

BASE <http://www.myexperiment.org/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX meannot: <http://rdf.myexperiment.org/ontologies/annotations/>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
SELECT ?annotation ?annotator
WHERE{
  ?annotation mebase:annotates <workflows/72> .
  { ?annotation rdf:type meannot:Comment } 
    UNION { ?annotation rdf:type meannot:Rating } .
  ?annotation mebase:has-annotator ?annotator
}
[Run]
[Hide Example Results]
annotationannotator
http://www.myexperiment.org/workflows/72/comments/33http://www.myexperiment.org/users/18
http://www.myexperiment.org/workflows/72/ratings/63http://www.myexperiment.org/users/283
http://www.myexperiment.org/workflows/72/comments/163http://www.myexperiment.org/users/18
http://www.myexperiment.org/workflows/72/ratings/132http://www.myexperiment.org/users/690

4.2. OPTIONAL

There may be ocassions where you want to include additional variables in your search but you still want to return results if there are not values for these variables. An example might be that you want to return the name, homepage and email address for users. Some users may have not set a homepage and/or email address but you still want to know their name:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX sioc: <http://rdfs.org/sioc/ns#>
PREFIX mebase: <http://rdf.myexperiment.org/ontologies/base/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?name ?homepage ?email
WHERE{
  ?user rdf:type mebase:User ;
    sioc:name ?name .
    OPTIONAL { ?user foaf:homepage ?homepage } .
    OPTIONAL { ?user foaf:mbox ?email }
}
[Run]
[Hide Example Results]
namehomepageemail
Workflowxdong
Piculin
Daniel Kornevhttp://blogs.msdn.com/semantics/mailto:daniel.kornev@microsoft.com
Ramesh kuc
John locke
Nikolas
Nlynch
kondas
Jos?? Manuel Rodr??guezhttp://www.inab.orgmailto:jmrodriguez@cnio.es
Babo1ug
Funkyd101mailto:d.woodhead@gmail.com
Hong harpermailto:ihatewinter03@yahoo.com
                    Back to Contents Page