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
Keyword | Purpose | Example |
DISTINCT | Prevents duplicate results to be returned. |
This Query returns the names and birthdays of women with no duplicate entries. |
BIND | Assigns the result of an expression to a variable, using the format "BIND(expression AS ?variable)." |
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. |
This Query returns names of women with birthdates after January 1, 1900. |
FILTER NOT EXIST | Filters results, returning results which do not meet the provided clause. |
This Query returns names of women except for those with the birthday December 16, 1776 (Jane Austen). |
GRAPH | Specifies which graph data will be retrieved from. |
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 BY | Groups 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. |
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. |
LIMIT | Limits the number of results returned. The results will have a random order unless using the 'ORDER BY' keyword. |
This Query returns the first ten names of women. |
MINUS | Similar to FILTER NOT EXISTS; can be used to prevent certain objects from being included in the results. |
This Query returns names of women, except those named "Anne Askew". |
OFFSET | Skips the first number of rows specified by offset. |
This Query returns names of women, starting at the eleventh name. |
OPTIONAL | Makes 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. |
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. |
This Query returns names of people, ordered by their date of birth. |
VALUES | Allows for more than one match to the criteria. |
This Query returns names, these names can either be Anne or Mary. |
UNION | Allows for grouping of different results, as well as combining multiple graph patterns so that one of several may match |
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. |