Inferensys

Glossary

Basic Graph Pattern

A Basic Graph Pattern (BGP) is the core matching construct in SPARQL, consisting of a set of triple patterns that must all match for the query to produce a solution.
Moody home-office setup in a converted highrise loft, analyst working late with multiple screens showing knowledge graph visualizations, city lights through large windows behind.
SPARQL CORE CONSTRUCT

What is a Basic Graph Pattern?

A Basic Graph Pattern (BGP) is the fundamental matching unit in a SPARQL query, consisting of a set of triple patterns that must all be satisfied to produce a query solution.

A Basic Graph Pattern (BGP) is the core matching construct in the SPARQL query language. It is defined as a set of triple patterns—RDF triples where any component can be a variable—that must all find a match in the target RDF dataset for the query to yield a solution. The SPARQL processor evaluates a BGP by performing a graph pattern match, finding all possible variable bindings (solutions) that make each triple pattern a true statement in the data graph. This conjunctive matching forms the basis for more complex SPARQL query forms.

BGPs are combined using SPARQL operators like UNION or OPTIONAL to build complex query patterns. Crucially, the evaluation of a BGP is defined as a single join across all its triple patterns, not as a sequence of independent lookups. This allows for sophisticated query optimization by the triplestore. Understanding BGPs is essential for writing efficient SPARQL, as they directly correspond to the fundamental subgraph isomorphism problem that graph databases solve.

SPARQL QUERY CONSTRUCT

Key Components of a Basic Graph Pattern

A Basic Graph Pattern (BGP) is the fundamental matching unit in a SPARQL query. It consists of a set of triple patterns that must all be satisfied (matched) against the RDF dataset for the query to produce a solution (a set of bindings for the variables).

01

Triple Pattern

The atomic building block of a BGP. It mirrors the structure of an RDF triple (subject-predicate-object) but allows variables (prefixed with ? or $) in any position to act as placeholders for unknown values.

  • Example: ?person foaf:name "Alice" .
  • This pattern matches any triple where the predicate is foaf:name, the object is the literal "Alice", and binds the subject of that triple to the variable ?person.
  • A BGP is a conjunction of triple patterns; all patterns must match simultaneously within the same graph.
02

Variables

Variables are the query's unknowns, represented by a ? or $ followed by a name (e.g., ?city, ?date). They are bound to RDF terms (URIs, literals, or blank nodes) during query execution.

  • Variables create the join conditions across triple patterns in a BGP. If the same variable appears in multiple patterns, it must be bound to the same value in all matches.
  • Example BGP: ?article dc:creator ?author . ?author foaf:based_near ?city .
  • Here, ?author is the join variable, ensuring the matched creator is also the entity with a location.
03

IRIs and Prefixes

IRIs (Internationalized Resource Identifiers) identify resources and predicates. To keep patterns concise, SPARQL uses prefix declarations (e.g., PREFIX foaf: <http://xmlns.com/foaf/0.1/>) to abbreviate long IRIs.

  • Abbreviated IRI: foaf:name expands to the full IRI.
  • Full IRI: Enclosed in angle brackets: <http://example.org/person>.
  • Using correct IRIs is critical for precise matching against the dataset's ontology.
04

Literals and Datatypes

Literals are constant values (strings, numbers, dates) used in the object position of a triple pattern. They can be:

  • Simple Literals: Plain strings: "London".
  • Typed Literals: Include a datatype IRI: "42"^^xsd:integer.
  • Language-tagged Literals: Include a language tag: "chat"@fr.

Pattern matching is sensitive to literal type and language tag. The pattern ?s ex:value "42" will not match a triple with object "42"^^xsd:integer unless the query explicitly uses the typed literal.

05

Blank Nodes in Patterns

Blank nodes (written as _:a or []) in a BGP act as existential variables—they match any resource but do not bind to a variable name for output. They are useful for matching anonymous resources or specific graph structures without needing to identify the node.

  • Example: ?person foaf:knows [ foaf:name "Bob" ] .
  • This pattern matches a person who knows some resource (the blank node) that has the name "Bob". The identity of that resource is not captured in the query results.
06

Graph Context (FROM / GRAPH)

A BGP is matched against a specific RDF Dataset, which is a collection of graphs. The default graph and any named graphs to be queried are specified using the FROM and FROM NAMED clauses in the SPARQL query.

  • Patterns are matched against the default graph unless enclosed in a GRAPH clause.
  • GRAPH ?g { ...BGP... }: Matches the BGP only within the named graph bound to variable ?g.
  • GRAPH <http://ex.org/graph1> { ... }: Matches the BGP specifically within the named graph identified by that IRI.
COMPARISON

BGP vs. Other SPARQL Graph Patterns

A comparison of the Basic Graph Pattern (BGP) with other core SPARQL graph pattern types, detailing their syntax, semantics, and typical use cases.

Feature / Pattern TypeBasic Graph Pattern (BGP)Optional Graph Pattern (OPTIONAL)Alternative Graph Pattern (UNION)Group Graph Pattern (GROUP)

Core Matching Semantics

Conjunctive (AND). All triple patterns in the set must match.

Left-join semantics. Matches optional patterns if possible; does not fail the query if they do not match.

Disjunctive (OR). Produces solutions that match at least one of the combined graph patterns.

Logical grouping. Groups a set of patterns to control precedence, especially with FILTERs.

SPARQL Syntax Keyword

None (implicit). A set of triple patterns within {} is a BGP.

OPTIONAL

UNION

{} (braces)

Impact on Solution Set

Restrictive. Reduces the solution set by requiring all conditions.

Expansive. Can add variable bindings to existing solutions without filtering them out.

Expansive. Combines solution sets from multiple patterns, increasing total results.

Neutral. Alters evaluation order but does not inherently change final results.

Cardinality of Results

Produces the Cartesian product of matches for its triple patterns.

Can increase cardinality if the optional part matches multiple times for a single solution from the mandatory part.

Produces the union of solutions from each branch, potentially increasing total cardinality.

No direct impact; depends on the patterns inside the group.

Use of Unbound Variables

Common Primary Use Case

Core fact retrieval. Finding entities that satisfy a conjunction of properties and relationships.

Enriching results. Adding supplemental information that may not exist for all primary matches.

Querying heterogeneous data. Searching across different possible graph structures or classifications.

Scoping filters. Applying a FILTER expression to the results of a specific set of patterns.

Interaction with FILTER

FILTERs can be applied to the entire BGP's solution set.

FILTERs inside an OPTIONAL block apply only to the optional matches.

FILTERs must be placed within each UNION branch to apply to those solutions.

Essential for applying a FILTER to a specific sub-pattern, not the entire WHERE clause.

Performance Consideration

Typically the most efficient; triplestores optimize BGPs heavily.

Can be computationally expensive, especially with complex optional patterns on large datasets.

May require evaluating multiple sub-queries, but modern optimizers handle this efficiently.

Minimal overhead; primarily a syntactic construct for the query planner.

BASIC GRAPH PATTERN

Frequently Asked Questions

A Basic Graph Pattern (BGP) is the fundamental matching unit in SPARQL, the query language for RDF. It consists of a set of triple patterns that must all be satisfied for the query to return results. This FAQ addresses common questions about its structure, function, and role in semantic querying.

A Basic Graph Pattern (BGP) is the core matching construct in a SPARQL query, defined as a set of one or more triple patterns that must all be matched against the RDF dataset for the query to produce a solution. It is the atomic unit of graph pattern matching, analogous to a conjunction of conditions in a SQL WHERE clause. A BGP is evaluated by finding all possible mappings of its variables to RDF terms (URIs, literals, or blank nodes) in the dataset such that each triple pattern becomes a valid RDF triple. The result is a sequence of solution mappings (bindings for the variables) that satisfy the entire pattern. BGPs are the building blocks for more complex SPARQL patterns, which can combine them using operators like UNION, OPTIONAL, and FILTER.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.