Graph Query Patterns
Graph queries bind variables against structure. In SPARQL, a basic graph pattern joins triple patterns; a property path matches routes over named predicates. Property-graph languages use different syntax and data semantics, but share recurring questions: select a neighborhood, join through an intermediate node, aggregate matches, and bound work.
Prerequisites
Complete knowledge graph fundamentals. Retain the difference between a typed predicate and an unlabeled adjacency.
Learning goals
- Read a triple-pattern join as a declarative match.
- Distinguish existence, path endpoints, path enumeration, and shortest-path questions.
- Recognize duplicate, optional-match, aggregation, and inference semantics.
- Bound traversal and inspect plans before running a broad query.
Query contract
- Active graph: the six asserted local triples from the previous lesson; there is no implicit inference or remote federation.
- Result unit: sorted variable bindings, not paths or causal chains unless explicitly requested.
- Duplicate rule: the Python example returns a set-like result; SPARQL solution sequences may contain duplicate bindings unless
DISTINCTis requested. - Interpretation limit: a matched pattern says the recorded graph contains that structure, not why the relationship exists.
Reusable patterns
A SPARQL-shaped join for people, labels, and teams is:
SELECT ?person ?label ?team WHERE {
?person :type :Person .
?person :label ?label .
?person :memberOf ?team .
}
The variables must bind consistently across all three patterns. OPTIONAL preserves a row when an optional pattern does not match; FILTER NOT EXISTS expresses scoped negation under the query's graph semantics. Aggregation counts solution rows, which may not equal unique entities. Use COUNT(DISTINCT ?person) when that is the intended denominator.
SPARQL property paths such as :knows+ return endpoint bindings for one-or-more steps. They do not, by themselves, enumerate every route or provide a path length. Cycles and zero-length operators also affect matches. In any graph system, unrestricted variable-length traversal can touch a large fraction of the graph; anchor starting terms, restrict relationship types, cap depth where the language allows it, and inspect the physical plan.
Worked example
triples = {
("ada", "type", "Person"), ("ben", "type", "Person"),
("ada", "label", "Ada"), ("ben", "label", "Ben"),
("ada", "memberOf", "team-graph"), ("ada", "knows", "ben"),
}
people = {subject for subject, predicate, obj in triples if (predicate, obj) == ("type", "Person")}
labels = {subject: obj for subject, predicate, obj in triples if predicate == "label"}
teams = {subject: obj for subject, predicate, obj in triples if predicate == "memberOf"}
bindings = sorted(
(person, labels[person], teams[person])
for person in people if person in labels and person in teams
)
knows = {(subject, obj) for subject, predicate, obj in triples if predicate == "knows"}
reachable = sorted(obj for subject, obj in knows if subject == "ada")
print(f"bindings={bindings}")
print(f"ada_knows_one_hop={reachable} inference=none")
print("query_boundary=pattern-matches-not-path-explanations")
Expected output:
bindings=[('ada', 'Ada', 'team-graph')]
ada_knows_one_hop=['ben'] inference=none
query_boundary=pattern-matches-not-path-explanations
Limitations
- The example is a transparent pattern matcher, not a conforming SPARQL engine.
- Query languages differ on null-like values, duplicate rows, paths, schema, and updates; translate concepts, not syntax alone.
- An entailment-enabled store may return statements absent from the asserted graph.
- Broad traversals can expose sensitive neighborhoods even when individual attributes are hidden.
Practice
Extend the triples with Ben's team membership. Predict the difference between counting matching rows, distinct people, and distinct teams before changing the code.
Next steps
Continue to node and graph embeddings to replace explicit matches with learned vectors for a declared objective.
Primary sources
Checked 2026-10-18: W3C, SPARQL 1.1 Query Language; W3C, SPARQL 1.1 Overview; NetworkX 3.7, descendants.