Skip to main content

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.

About Query Modifiers

There are many more modifiers than those listed in the table below. More information about them can be found on the W3 Schools SPARQL Documentation, or in the SPARQL WikiBooks.

Many modifiers can take a number of different formats of parameters, such as regex (regular expression). More information about writing regex can be found in the SPARQL Wikibooks, and on the W3 Schools website. Another website to help get used to writing regex is Regex Explained.

Often data types need to be specified when using specific values as parameters for modifiers. There are a number of different datatypes, such as xsd:integer (a whole number), xsd:boolean (data that is either true or false), or xsd:decimal (a number with values after the decimal point). To specify these datatypes, use the format ^^xsd:datatype.

For example, if we wanted to find people with dates of birth between Jan. 1 and Dec. 31, 1884, we would use a regex that specified that those values were dates:

FILTER ((?birthDate >= '1844-01-01'^^xsd:date) && (?birthDate <= '1884-12-31' ^^xsd:date))

More information about the basic datatypes can be found on the W3 Schools XML Schema Pages.

Query Modifiers

KeywordPurposeExample
DISTINCTPrevents duplicate results to be returned.
SELECT DISTINCT ?birthdate ?name WHERE { 
?person cwrc:hasGender cwrc:woman;
cwrc:hasBirthDate ?birthdate;
skos:altLabel ?name.
}

This Query returns the names and birthdays of women with no duplicate entries.

BINDAssigns the result of an expression to a variable, using the format "BIND(expression AS ?variable)."
 SELECT ?name ?birthDate ?deathDate ?age WHERE { 
?person rdfs:lable ?name;
cwrc:hasDeathDate ?deathDate;
cwrc:hasBirthDate ?birthDate.
BIND(?deathDate - ?birthDate as ?ageInDays).
BIND(?ageInDays/365.2425 AS ?ageInYears).
BIND(FLOOR(?ageInYears) AS ?age).
}

This Query returns the names and ages of people in the database. The age is calculated using the BIND keyword to assign values to variables such as date of birth, date of death, and ageInDays, so operations can be performed on them without overwriting the variable each time.

FILTER Filters results by a provided clause. You can filter by using a regex (regular expression) that will return true/false, a range of dates, the language of a label, and more.
SELECT ?name ?birthDate WHERE { 
?person cwrc:hasBirthDate ?birthDate ;
cwrc:hasGender cwrc:woman;
rdfs:label ?name.
FILTER ((?birthDate >= '1900-01-01'^^xsd:date)) .
}

This Query returns names of women with birthdates after January 1, 1900.

FILTER NOT EXISTFilters results, returning results which do not meet the provided clause.
 SELECT ?name WHERE { 
?person cwrc:hasBirthDate ?birthDate ;
cwrc:hasGender cwrc:woman;
rdfs:label ?name.
FILTER NOT EXISTS {
?person cwrc:hasBirthDate "1775-12-16"^^xsd:date
}
}

This Query returns names of women except for those with the birthday December 16, 1776 (Jane Austen).

GRAPHSpecifies which graph data will be retrieved from.
 SELECT ?obj ?snippet WHERE { 
GRAPH {
?context ?pred ?obj;
cwrc:contextFocus ?person;
oa:hasTarget ?target;
rdf:type cwrc:DeathContext.
?person rdfs:label "Donne, John".
}.
?target oa:hasSelector/oa:refinedBy/oa:exact ?snippet.
}

This Query will return snippets relating to John Donne's death. It does so by using GRAPH to get the context from the BiographyV2Beta database where it is stored.

GROUP BYGroups the results together according to specified criteria. After grouping results, you can then perform functions on these groups, such as COUNT(), AVG(), SUM(), MIN(), and MAX(). More information on aggregate functions can be found here.
 SELECT ?occupation (COUNT(?person) AS ?numPerProf) WHERE { 
?person cwrc:hasGender cwrc:woman;
cwrc:hasOccupation ?occupation.
}
GROUP BY ?occupation

This Query returns occupations of women in CRWC. The query first finds all the women with occupations, groups them by their occupations (using GROUP BY), and then counts the number of people in each group. These counts will be displayed in the results table in a column labeled "numPerProf", the number of people in each profession.

LIMITLimits the number of results returned. The results will have a random order unless using the 'ORDER BY' keyword.
 SELECT ?name WHERE { 
?person cwrc:hasGender cwrc:woman .
?person rdfs:label ?name .
}LIMIT 10

This Query returns the first ten names of women.

MINUSSimilar to FILTER NOT EXISTS; can be used to prevent certain objects from being included in the results.
 SELECT ?name WHERE {
?person cwrc:hasGender cwrc:woman .
?person rdfs:label ?name .
MINUS {
?person rdfs:label "Askew, Anne".
}.
}

This Query returns names of women, except those named "Anne Askew".

OFFSETSkips the first number of rows specified by offset.
 SELECT ?name WHERE { 
?person cwrc:hasGender cwrc:woman .
?person rdfs:label ?name .

}OFFSET 10

This Query returns names of women, starting at the eleventh name.

OPTIONALMakes the specified criteria optional. If the object exists, it will be displayed in the results, but if not, the cell in the results will simply be empty.
 SELECT ?name ?deathDate WHERE {
?person cwrc:hasGender cwrc:woman;
rdfs:label ?name .
OPTIONAL {?person cwrc:hasDeathDate ?deathDate}.
}

This Query returns names of women and their date of death. If they do not have a date of death, their name will still be displayed, but the column for date of death will be empty for that row.

ORDER BY Sorts the data by the values specified. Default is ascending order, unless otherwise specified.
 SELECT ?name ?birthDate WHERE {
?person cwrc:hasBirthDate ?birthDate.
?person rdfs:label ?name .

}ORDER BY ?birthDate

This Query returns names of people, ordered by their date of birth.

VALUESAllows for more than one match to the criteria.
 SELECT ?name ?birthDate WHERE {
VALUES ?name {"Anne" "Mary"}
?person cwrc:hasBirthDate ?birthDate.

}

This Query returns names, these names can either be Anne or Mary.

UNIONAllows for grouping of different results, as well as combining multiple graph patterns so that one of several may match
 SELECT ?name ?occupation WHERE {
?person rdfs:label ?name.
{?person cwrc:hasPaidOccupation ?occupation.}
UNION
{?person cwrc:hasVolunteerOccupation ?occupation.}
}

This Query returns the names and occupations of people who have a volunteer occupation, and people who have paid occupations. By using UNION, the two kinds of occupations are displayed in the same column, rather than needing to have separate columns for each type of occupation.