Theory
A SPARQL SELECT query is a triple pattern with variables prefixed by ?:
SELECT ?ninja WHERE {
?ninja a :Ninja .
}
The engine matches the pattern against the graph and binds ?ninja to every subject that fits.
Think of ?var as a placeholder for any RDF term. The engine tries every node in the graph in that slot, keeps the bindings where the whole pattern matches, and returns one row per surviving binding. Two ?ninja occurrences in the same query must bind to the same term — that's how joins happen (next lesson). Result column names come from the ?var names you write after SELECT.
The four query forms (know all of them)
SELECT is one of four result forms — pick the form by the shape of answer you need, not habit:
| Form | Returns | Use when |
|---|---|---|
SELECT | A table of variable bindings | You want columns/rows |
ASK | A single boolean | You only need does it exist? |
CONSTRUCT | A new RDF graph | You're reshaping/inferring triples |
DESCRIBE | An RDF graph about a resource | You want 'everything about ?x' |
Two things that surprise SQL people
- Results are a bag (multiset), not a set. SPARQL does not de-duplicate rows unless you write
SELECT DISTINCT. Two different ways of matching the same?ninjaproduce two rows. - The dataset is explicit.
FROM <graph>/FROM NAMED <graph>choose which graph(s) the patterns run against; with noFROM, the endpoint's default graph is used. Querying the wrong dataset is the most common reason a correct-looking query returns nothing.
