What Is SPARQL?
SPARQL (SPARQL Protocol and RDF Query Language) is the standard query language for RDF knowledge graphs. It's to knowledge graphs what SQL is to relational databases.
SPARQL queries work by defining graph patterns — templates that are matched against the triples in the graph. Variables (prefixed with ?) are bound to matching values.
The Four Query Forms
| Form | Returns | Use case |
|---|---|---|
| SELECT | Table of variable bindings | Retrieve facts |
| ASK | Boolean (yes/no) | Check if something exists |
| CONSTRUCT | New RDF graph | Transform/extract subgraph |
| DESCRIBE | RDF description of a resource | Explore an unknown resource |
Interactive query
Predicates: knows · worksAt · locatedIn
SELECT ?person WHERE {
?person rdf:type ex:Person .
}Writing SPARQL Queries
Basic SELECT Query
Every SPARQL query has two key sections:
- SELECT — which variables to return
- WHERE — the graph pattern to match
PREFIX ex: <http://example.org/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
# 1. Find all people
SELECT ?person WHERE {
?person rdf:type foaf:Person .
}
# ──────────────────────────────────────────
# 2. Find names of all people
SELECT ?person ?name WHERE {
?person rdf:type foaf:Person ;
foaf:name ?name .
}
# ──────────────────────────────────────────
# 3. Find people over 30 (using FILTER)
SELECT ?name ?age WHERE {
?person foaf:name ?name ;
foaf:age ?age .
FILTER (?age > 30)
}
ORDER BY DESC(?age)
# ──────────────────────────────────────────
# 4. Find people and their employer city
# (multi-hop: person → company → city)
SELECT ?personName ?companyName ?cityName WHERE {
?person foaf:name ?personName ;
ex:worksAt ?company .
?company foaf:name ?companyName ;
ex:locatedIn ?city .
?city foaf:name ?cityName .
}
# ──────────────────────────────────────────
# 5. OPTIONAL — include people even if no company known
SELECT ?name ?companyName WHERE {
?person foaf:name ?name .
OPTIONAL {
?person ex:worksAt ?company .
?company foaf:name ?companyName .
}
}CONSTRUCT and ASK
CONSTRUCT generates new RDF from the query result — useful for data transformation:
# CONSTRUCT — generate new triples from a query
CONSTRUCT {
?person <http://schema.org/worksFor> ?company .
}
WHERE {
?person ex:worksAt ?company .
}
# ──────────────────────────────────────────
# ASK — returns true/false
ASK {
ex:alice ex:worksAt ex:acme .
}
# Returns: true
# ──────────────────────────────────────────
# UNION — match either pattern
SELECT ?entity WHERE {
{ ?entity rdf:type ex:Person . }
UNION
{ ?entity rdf:type ex:Organization . }
}# Run this at: https://query.wikidata.org
SELECT ?person ?personLabel ?year WHERE {
?person wdt:P166 wd:Q38104 . # awarded Nobel Prize in Physics
?person wdt:P569 ?birthDate .
BIND(YEAR(?birthDate) AS ?birthYear)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ?year
LIMIT 20Knowledge check
In SPARQL, what does the OPTIONAL keyword do?
Summary
In this chapter you learned:
- SPARQL uses graph patterns with variables (
?x) to query RDF data - The four query forms: SELECT (rows), ASK (boolean), CONSTRUCT (new graph), DESCRIBE (resource info)
- FILTER, OPTIONAL, and UNION add flexibility to patterns
- Public SPARQL endpoints like Wikidata let you explore real knowledge graphs
Next, we'll compare the RDF graph model to the Property Graph model used by Neo4j and TinkerPop.