Query modifiers: refining your search

SPARQL has many built-in keywords which can be used to refine your search. The table below provides a list of some of the main keywords along with a brief description.

Keyword Purpose
FILTER An expression that modifies your search is placed in the filter paraenthesis. See Query 1 for an example.
FILTER NOT EXISTS Returns true if a specified graph pattern does not exist.
OPTIONAL Makes certain patterns of preceding triples optional. See Query 2 for an example.
MINUS Similar to FILTER NOT EXISTS; subtracts the graph pattern in the argument.
UNION Allows for the retrieval of different graph patters at once.
OFFSET Skips the first number of rows specified by offset.
LIMIT Limits the number of outputs displayed in a random order.
VALUES Specifies values for variables; allows for more than one match.
ORDER BY Sorts the data using the values in the argument. Data are shown in ascending order unless otherwise specified. See Query 3 for an example.
GROUP BY Groups data together to perform aggregate functions. See Query 4 for an example.
GRAPH Specifies which graph data will be retrieved from.

Examples:

Women named Anne

                    PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?name 
WHERE { 
    ?person cwrc:hasGender cwrc:woman .
    ?person rdfs:label ?name .
FILTER(regex(?name, "Anne", "i")) 
}                   
                

The FILTER command takes a single argument. Many of the arguments make use of a function. Here, the function regex is used to filter the results based on the label of the person. The query returns all the names of the women in the dataset, and the filter command reduces that output to only those containing the name "Anne". The "i" is for "insensitive"; this means the results can be either uppercase or lowercase.

Women's occupations

                    
                    PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>                        
SELECT ?name ?occupation 
WHERE { 
    ?person cwrc:hasGender cwrc:woman ;
            cwrc:hasOccupation ?occupation ; 
            skos:altLabel ?name
    
    OPTIONAL {?occupation a cwrc:teacher}
}                    
                

The OPTIONAL graph pattern does not need to be satisfied. If the OPTIONAL tag was removed in this example, the resulting table would include all of the women who are teachers. The inclusion of the OPTIONAL tag will display all women and their occupations, including women who are teachers. This is similar to an outer join in SQL.

Birthdates

                    PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?name ?birthDate 
WHERE { 
    ?person cwrc:hasBirthDate ?birthDate ;
            skos:altLabel ?name
FILTER ((?birthDate >= '1844-01-01'^^xsd:date) && (?birthDate <= '1884-01-01' ^^xsd:date)) . 
  	
}ORDER BY ?birthDate
                 
                

This query searches for every person who has a birthdate between two specific dates. The triples that satisfy this are then displayed in a table which will be ordered by birth dates from earliest to latest in ascending numerical value.

Literals: The object of a triple can be a URI, a string, or a literal. The data type can be specified if known. In the example above the data, in this case a date, is specified.

Examples of other types:

  • d:item dm:quantity "4"^^xsd:integer
  • d:item dm:invoiced "false"^^xsd:boolean
  • d:item dm:costPerItem "3.50"^^xsd:decimal

Counting professions

                    PREFIX cwrc: <http://sparql.cwrc.ca/ontologies/cwrc#>

SELECT ?occupation (COUNT(?person) AS ?numberPerProfession) 
WHERE { 
    ?person a cwrc:NaturalPerson;
        cwrc:hasOccupation ?occupation.
}
GROUP BY ?occupation 
ORDER BY DESC (?numberPerProfession)
                

The GROUP BY function groups functions together. These functions can then be performed on each group. The query above groups results by occupation. The COUNT function then adds the number of people with that profession. The table will have two columns: one for occupation, the other, called numberPerProfession, with an amount.

Other functions:

  • AVG()
  • MAX()
  • MIN()
  • SUM()
  • COUNT()