Your first SELECT

Triple patterns with variables.

0/2 done

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:

FormReturnsUse when
SELECTA table of variable bindingsYou want columns/rows
ASKA single booleanYou only need does it exist?
CONSTRUCTA new RDF graphYou're reshaping/inferring triples
DESCRIBEAn RDF graph about a resourceYou want 'everything about ?x'

Two things that surprise SQL people

  1. 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 ?ninja produce two rows.
  2. The dataset is explicit. FROM <graph> / FROM NAMED <graph> choose which graph(s) the patterns run against; with no FROM, the endpoint's default graph is used. Querying the wrong dataset is the most common reason a correct-looking query returns nothing.

Visualization

Query Pattern match Bindings Result

A SPARQL query is a small pipeline: parse → match patterns → collect bindings → return rows.

Reading in progress · 0 of 2 activities done