WOLFRAM

Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how
Wolfram Language & System Documentation Center

SPARQLQuery[query]

is a query operator that can be applied to an RDFStore .

SPARQLQuery["query"]

uses a SPARQL query string.

Details and Options
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
See Also
Related Guides
Cite this Page

SPARQLQuery[query]

is a query operator that can be applied to an RDFStore .

SPARQLQuery["query"]

uses a SPARQL query string.

Details and Options

  • SPARQLQuery can be used in SPARQLExecute to query a SPARQL endpoint.
  • query can be a SPARQLSelect , SPARQLAsk or SPARQLConstruct operator.
  • An aggregate can be specified as SPARQLQuery[SPARQLSelect []/*SPARQLAggregate []].
  • The following options can be given:
  • "Base" Automatic base IRI used to resolve relative IRIs
    "From" Automatic default graph to query
    "FromNamed" Automatic list of named graphs to query
  • With the setting "From"{iri1,iri2,} the RDF merge of the graphs identified by the irii is used as default graph.
  • SPARQLQuery[File [...]] and SPARQLQuery[URL [...]] are also supported.

Examples

open all close all

Basic Examples  (1)

Wolfram Language code: Needs["GraphStore`"]

Specify an example RDF graph:

Wolfram Language code: ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: g = RDFStore[{ RDFTriple[ex["e1"], ex["a"], 1], RDFTriple[ex["e2"], ex["a"], 2], RDFTriple[ex["e3"], ex["a"], 3], RDFTriple[ex["e4"], ex["a"], 4], RDFTriple[ex["e1"], ex["b"], "x"], RDFTriple[ex["e2"], ex["b"], "y"], RDFTriple[ex["e3"], ex["b"], "z"], RDFTriple[ex["e4"], ex["b"], "x"] }];

Query all the data associated with a given subject:

Wolfram Language code: g//SPARQLQuery[ SPARQLSelect[RDFTriple[ex["e2"], SPARQLVariable["p"], SPARQLVariable["o"]]] ]

Query all the data associated with a given predicate:

Wolfram Language code: g//SPARQLQuery[ SPARQLSelect[RDFTriple[SPARQLVariable["s"], ex["b"], SPARQLVariable["o"]]] ]

Find all the subjects that have a certain value for a given predicate:

Wolfram Language code: g//SPARQLQuery[ SPARQLSelect[RDFTriple[SPARQLVariable["s"], ex["b"], "x"]] ]

Find the values of predicate "a" for subjects that have a certain value for predicate "b":

Wolfram Language code: g//SPARQLQuery[ SPARQLSelect[{ RDFTriple[SPARQLVariable["s"], ex["a"], SPARQLVariable["a"]], RDFTriple[SPARQLVariable["s"], ex["b"], "x"] } -> "a"] ]

Aggregate solutions:

Wolfram Language code: g//SPARQLQuery[ SPARQLSelect[RDFTriple[SPARQLVariable["s"], ex["a"], SPARQLVariable["a"]]] /* SPARQLAggregate["total" -> SPARQLEvaluation["sum"][SPARQLVariable["a"]]] ]

Group and aggregate solutions:

Wolfram Language code: g//SPARQLQuery[ SPARQLSelect[{ RDFTriple[SPARQLVariable["s"], ex["a"], SPARQLVariable["a"]], RDFTriple[SPARQLVariable["s"], ex["b"], SPARQLVariable["b"]] }] /* SPARQLAggregate[{"b", "total" -> SPARQLEvaluation["sum"][SPARQLVariable["a"]]}, SPARQLVariable["b"]] ]

Scope  (2)

Wolfram Language code: Needs["GraphStore`"]
Wolfram Language code: ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: g = RDFStore[{ RDFTriple[ex["strawberry"], ex["color"], "red"] }];

Specify a query using a SPARQL query string:

Wolfram Language code: g//SPARQLQuery[" prefix ex: <http://example.org/> select * where {ex:strawberry ex:color ?color .} "]
Wolfram Language code: Needs["GraphStore`"]

Export a SPARQL query:

Wolfram Language code: rdf[s_] := URL["http://www.w3.org/1999/02/22-rdf-syntax-ns#" <> s]; schema[s_] := URL["http://schema.org/" <> s]; ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: ExportString[ SPARQLQuery[ SPARQLSelect[{ RDFTriple[SPARQLVariable["movie"], rdf["type"], schema["Movie"]], RDFTriple[SPARQLVariable["movie"], ex["durationHours"], SPARQLVariable["duration"]] } /; SPARQLVariable["duration"] > 5] ], "SPARQLQuery", "Prefixes" -> <| "schema" -> "http://schema.org/", "ex" -> "http://example.org/" |> ]

Import the same query:

Wolfram Language code: ImportString[%, "SPARQLQuery"]
Wolfram Research (2019), SPARQLQuery, Wolfram Language function, https://reference.wolfram.com/language/GraphStore/ref/SPARQLQuery.html.

Text

Wolfram Research (2019), SPARQLQuery, Wolfram Language function, https://reference.wolfram.com/language/GraphStore/ref/SPARQLQuery.html.

CMS

Wolfram Language. 2019. "SPARQLQuery." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphStore/ref/SPARQLQuery.html.

APA

Wolfram Language. (2019). SPARQLQuery. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphStore/ref/SPARQLQuery.html

BibTeX

@misc{reference.wolfram_2026_sparqlquery, author="Wolfram Research", title="{SPARQLQuery}", year="2019", howpublished="\url{https://reference.wolfram.com/language/GraphStore/ref/SPARQLQuery.html}", note=[Accessed: 18-August-2026]}

BibLaTeX

@online{reference.wolfram_2026_sparqlquery, organization={Wolfram Research}, title={SPARQLQuery}, year={2019}, url={https://reference.wolfram.com/language/GraphStore/ref/SPARQLQuery.html}, note=[Accessed: 18-August-2026]}

Top [フレーム]

AltStyle によって変換されたページ (->オリジナル) /