Basics

Reading data from the graph with SELECT, WHERE, and triple patterns.

SPARQL reads data from the graph by matching patterns. You describe the shape of the data you're looking for, and the engine finds everything that matches.

The examples on this page assume the following prefix declarations. In SPARQL, prefixes are declared with PREFIX (no @, no .):

PREFIX rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd:    <http://www.w3.org/2001/XMLSchema#>
PREFIX tasks:  <https://example.org/spec/tasks#>
PREFIX people: <https://example.org/people/>

SELECT and WHERE

The simplest script: find all tasks and their titles.

PREFIX tasks: <https://example.org/spec/tasks#>

SELECT ?task ?title WHERE {
    ?task a tasks:Task .
    ?task tasks:title ?title .
}

?task and ?title are variables. The WHERE clause contains triple patterns with variables in place of the parts you want to find. The engine matches these patterns against the graph and returns every combination of ?task and ?title that satisfies all the patterns.

Variables always start with ?. Pattern triples end with . just like Turtle.

Triple Patterns

A triple pattern is a triple with one or more variables. The engine tries to match it against real triples in the graph.

PREFIX tasks:  <https://example.org/spec/tasks#>
PREFIX people: <https://example.org/people/>

# Find all things that are Tasks
?task a tasks:Task .

# Find the title of a specific task
<https://example.org/tasks/123> tasks:title ?title .

# Find all tasks assigned to alice
?task tasks:assignee people:alice .

# Find everything about task 123
<https://example.org/tasks/123> ?predicate ?object .

Multiple patterns in a WHERE clause must all match simultaneously:

PREFIX tasks:  <https://example.org/spec/tasks#>

SELECT ?task ?title ?assignee WHERE {
    ?task a tasks:Task .
    ?task tasks:title ?title .
    ?task tasks:assignee ?assignee .
}

This only returns tasks that have both a title and an assignee. If a task has no assignee, it won't appear in results. Use OPTIONAL if you want tasks regardless of whether they have an assignee.

Shorthand

Like Turtle, SPARQL supports semicolons for the same subject:

PREFIX tasks: <https://example.org/spec/tasks#>

SELECT ?task ?title ?status WHERE {
    ?task a tasks:Task ;
          tasks:title ?title ;
          tasks:status ?status .
}

Same script as writing three separate triple patterns, but more readable.

Prefixes in SPARQL

SPARQL uses PREFIX (no @, no .) instead of Turtle's @prefix:

PREFIX tasks: <https://example.org/spec/tasks#>
PREFIX rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?task WHERE {
    ?task rdf:type tasks:Task .
}

The a shorthand for rdf:type works in SPARQL too.

Counting and Aggregation

PREFIX tasks: <https://example.org/spec/tasks#>

SELECT (COUNT(?task) AS ?total) WHERE {
    ?task a tasks:Task .
}

Other aggregates: SUM(), AVG(), MIN(), MAX(), GROUP_CONCAT().

Group by a variable:

PREFIX tasks: <https://example.org/spec/tasks#>

SELECT ?status (COUNT(?task) AS ?count) WHERE {
    ?task a tasks:Task ;
          tasks:status ?status .
}
GROUP BY ?status

DISTINCT

Remove duplicate results:

PREFIX tasks: <https://example.org/spec/tasks#>

SELECT DISTINCT ?assignee WHERE {
    ?task tasks:assignee ?assignee .
}

ORDER BY and LIMIT

Sort and limit results:

PREFIX tasks: <https://example.org/spec/tasks#>

SELECT ?task ?title WHERE {
    ?task a tasks:Task ;
          tasks:title ?title .
}
ORDER BY ?title
LIMIT 10

See Also

On this page