Skip to content

BasicSPARQLGraphRewritePatternsCheatSheet

Michael DeBellis edited this page Jun 19, 2026 · 1 revision

Tip: Basic SPARQL Graph Rewrite Patterns Cheat Sheet

SPARQL is not only a query language. SPARQL Update also provides commands for moving, copying, adding, deleting, and rewriting triples across named graphs. These commands are especially useful in data-ingestion pipelines, validation workflows, and publication workflows where data moves through stages such as raw, cleaned, validated, and published graphs.

This tip summarizes a few common graph rewrite patterns.


➤ Move Everything from One Graph to Another

Use MOVE when you want to transfer all triples from one named graph to another. After the operation, the source graph is emptied and the target graph contains the moved triples.

MOVE GRAPH <https://example.com/graphs/Raw_Graph>
TO   GRAPH <https://example.com/graphs/Cleaned_Graph>

Use this when the source graph represents a temporary staging area and the data should no longer remain there after the move.


➤ Copy Everything from One Graph to Another

Use COPY when you want the target graph to become a copy of the source graph while leaving the source graph unchanged.

COPY GRAPH <https://example.com/graphs/Validated_Graph>
TO   GRAPH <https://example.com/graphs/Published_Graph>

Use this when you want to publish or snapshot validated data without removing it from its original graph.


➤ Add Everything from One Graph to Another

Use ADD when you want to add all triples from one graph into another graph without replacing the target graph.

ADD GRAPH <https://example.com/graphs/Source_A_Graph>
TO  GRAPH <https://example.com/graphs/Integrated_View>

This is useful when the target graph is an integrated view that accumulates triples from multiple source graphs.


➤ Move Only Selected Triples

Use DELETE/INSERT/WHERE when you want to move only the triples that match a specific pattern.

The following example moves valid climate observations from a raw graph to a validated graph. The validation_status triple is used to select the observations to move, but it is not copied into the validated graph.

PREFIX : <https://example.com/ns#>

DELETE {
  GRAPH <https://example.com/graphs/Raw_Graph> {
    ?observation a :Climate_Observation ;
                 :has_measurement_value ?value ;
                 :validation_status "valid" .
  }
}
INSERT {
  GRAPH <https://example.com/graphs/Validated_Graph> {
    ?observation a :Climate_Observation ;
                 :has_measurement_value ?value .
  }
}
WHERE {
  GRAPH <https://example.com/graphs/Raw_Graph> {
    ?observation a :Climate_Observation ;
                 :has_measurement_value ?value ;
                 :validation_status "valid" .
  }
}

This pattern is useful when data should move from one workflow stage to another only after it satisfies a condition, such as validation status, review status, or provenance requirements.


Bottom Line

Use MOVE, COPY, and ADD when the operation applies to an entire graph. Use DELETE/INSERT/WHERE when you need a pattern-based rewrite that moves, transforms, or filters only selected triples.

Clone this wiki locally