SPARQLQuery[query]
is a query operator that can be applied to an RDFStore.
SPARQLQuery["query"]
uses a SPARQL query string.
SPARQLQuery
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 allBasic Examples (1)
Needs["GraphStore`"]ex[s_] := URL["http://example.org/" <> s];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:
g//SPARQLQuery[
SPARQLSelect[RDFTriple[ex["e2"], SPARQLVariable["p"], SPARQLVariable["o"]]]
]Query all the data associated with a given predicate:
g//SPARQLQuery[
SPARQLSelect[RDFTriple[SPARQLVariable["s"], ex["b"], SPARQLVariable["o"]]]
]Find all the subjects that have a certain value for a given predicate:
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":
g//SPARQLQuery[
SPARQLSelect[{
RDFTriple[SPARQLVariable["s"], ex["a"], SPARQLVariable["a"]],
RDFTriple[SPARQLVariable["s"], ex["b"], "x"]
} -> "a"]
]g//SPARQLQuery[
SPARQLSelect[RDFTriple[SPARQLVariable["s"], ex["a"], SPARQLVariable["a"]]] /*
SPARQLAggregate["total" -> SPARQLEvaluation["sum"][SPARQLVariable["a"]]]
]Group and aggregate solutions:
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)
Needs["GraphStore`"]ex[s_] := URL["http://example.org/" <> s];g = RDFStore[{
RDFTriple[ex["strawberry"], ex["color"], "red"]
}];Specify a query using a SPARQL query string:
g//SPARQLQuery["
prefix ex: <http://example.org/>
select * where {ex:strawberry ex:color ?color .}
"]Needs["GraphStore`"]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];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/"
|>
]ImportString[%, "SPARQLQuery"]Related Guides
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: 13-June-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: 13-June-2026]}