SPARQLSelect[pattern]
is a query operator that can be applied to an RDFStore, which returns a list of associations of variables to corresponding values in subgraphs that match pattern.
SPARQLSelect[patternvars]
returns values only for the variables vars.
SPARQLSelect
SPARQLSelect[pattern]
is a query operator that can be applied to an RDFStore, which returns a list of associations of variables to corresponding values in subgraphs that match pattern.
SPARQLSelect[patternvars]
returns values only for the variables vars.
Details and Options
- SPARQLSelect can be used in SPARQLExecute to query a SPARQL endpoint.
- The result of executing SPARQLSelect[pattern] is a list of associations {<|"var1"val1,1,…|>,…} where each "vari" is the name of a SPARQLVariable appearing in pattern and each vali,j is the value appearing in the corresponding position of a matching subgraph.
- In SPARQLSelect[pattern], pattern can be any of the following forms:
-
RDFTriple[s,p,o] triple pattern {RDFTriple[…],RDFTriple[…],…} basic graph pattern {patt1,patt2,…} group graph pattern SPARQLFilter[expr] filter solutions of surrounding patterns by expr patt/;expr filter solutions by expr SPARQLOptional[patt] optional pattern patt1|patt2|... union pattern Except[patt] eliminate solutions which are compatible with patt SPARQLPropertyPath[…] property path pattern "var"expr bind expression expr to a variable with name "var" SPARQLValues[vars, values] specify values for variables SPARQLSelect[patt] subquery SPARQLGraph[g,patt] match patt agains the graph with name g SPARQLService[url,patt] federate matching patt to the SPARQL endpoint located at url - In a triple pattern RDFTriple[s,p,o] a SPARQLVariable stands for any expression.
- The following options can be given:
-
"Distinct" False whether duplicate solutions are deleted "Limit" Infinity maximum numer of solutions to return "Offset" 0 number of solutions to skip "OrderBy" None expression by which solutions are ordered "Reduced" False whether duplicate solutions can be deleted
Examples
open all close allBasic Examples (2)
Needs["GraphStore`"]schema[s_] := URL["http://schema.org/" <> s];
ex[s_] := URL["http://example.org/" <> s];fruitGraph = RDFStore[{
RDFTriple[ex["banana"], schema["color"], "yellow"],
RDFTriple[ex["strawberry"], schema["color"], "red"],
RDFTriple[ex["strawberry"], ex["shape"], ex["round"]]
}];fruitGraph//SPARQLSelect[RDFTriple[SPARQLVariable["fruit"], schema["color"], SPARQLVariable["color"]]]Find the color and the shape of fruits:
fruitGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["fruit"], schema["color"], SPARQLVariable["color"]],
RDFTriple[SPARQLVariable["fruit"], ex["shape"], SPARQLVariable["shape"]]
}]Needs["GraphStore`"]Find formulas (P274) and the Japanese name of chemicals (Q11173) containing (P527) calcium (Q706) in Wikidata:
rdfs[s_] := URL["http://www.w3.org/2000/01/rdf-schema#" <> s];
wd[s_] := URL["http://www.wikidata.org/entity/" <> s];
wdt[s_] := URL["http://www.wikidata.org/prop/direct/" <> s];SPARQLExecute[
"https://query.wikidata.org/sparql",
SPARQLSelect[
{
RDFTriple[SPARQLVariable["chemical"], wdt["P31"], wd["Q11173"]],
RDFTriple[SPARQLVariable["chemical"], wdt["P274"], SPARQLVariable["formula"]],
RDFTriple[SPARQLVariable["chemical"], wdt["P527"], wd["Q706"]],
RDFTriple[SPARQLVariable["chemical"], rdfs["label"], SPARQLVariable["label"]] /; SPARQLEvaluation["lang"][SPARQLVariable["label"]] == "ja"
} -> {"formula", "name" -> SPARQLEvaluation["str"][SPARQLVariable["label"]]},
"Limit" -> 5
]
]Scope (9)
Triple Pattern (1)
Needs["GraphStore`"]ex[s_] := URL["http://example.org/" <> s];exampleGraph = RDFStore[{
RDFTriple[ex["s1"], ex["p"], 4],
RDFTriple[ex["s2"], ex["p"], 8],
RDFTriple[ex["s3"], ex["p"], 8]
}];A triple pattern is a triple that can contain variables:
exampleGraph//SPARQLSelect[RDFTriple[ex["s1"], ex["p"], SPARQLVariable["x"]]]Variables can appear in any position:
exampleGraph//SPARQLSelect[RDFTriple[SPARQLVariable["x"], ex["p"], 8]]A triple pattern does not need to contain any variables:
exampleGraph//SPARQLAsk[RDFTriple[ex["s3"], ex["p"], 8]]Basic Graph Pattern (1)
Needs["GraphStore`"]dct[s_] := URL["http://purl.org/dc/terms/" <> s];
ex[s_] := URL["http://example.org/" <> s];articlesGraph = RDFStore[{
RDFTriple[ex["article1"], dct["title"], "some title"],
RDFTriple[ex["article1"], dct["creator"], "Jenny"],
RDFTriple[ex["article1"], dct["creator"], "Oliver"],
RDFTriple[ex["article1"], dct["created"], 2018],
RDFTriple[ex["article2"], dct["title"], "other title"],
RDFTriple[ex["article2"], dct["creator"], "Jenny"],
RDFTriple[ex["article2"], dct["created"], 2017]
}];A basic graph pattern is a list of triple patterns. Retrieve "title" and "created" of all articles:
articlesGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["a"], dct["title"], SPARQLVariable["title"]],
RDFTriple[SPARQLVariable["a"], dct["created"], SPARQLVariable["created"]]
} -> {"title", "created"}]Variables and literals can appear in any position. Find authors of articles created in 2018:
articlesGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["a"], dct["created"], 2018],
RDFTriple[SPARQLVariable["a"], dct["creator"], SPARQLVariable["author"]]
} -> "author"]Different triple patterns in a basic graph pattern are not required to match different triples in a graph. The following contains solutions binding "art1" and "art2" to the same article:
articlesGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["art1"], dct["creator"], SPARQLVariable["auth"]],
RDFTriple[SPARQLVariable["art2"], dct["creator"], SPARQLVariable["auth"]]
}]//Take[#, 3]&To find authors that have published more than one article filter the solution by requiring "art1" and "art2" to be different:
articlesGraph//SPARQLSelect[
{
RDFTriple[SPARQLVariable["art1"], dct["creator"], SPARQLVariable["auth"]],
RDFTriple[SPARQLVariable["art2"], dct["creator"], SPARQLVariable["auth"]]
} /; SPARQLVariable["art1"] ≠ SPARQLVariable["art2"] -> "auth",
"Distinct" -> True
]Filter (1)
Needs["GraphStore`"]ex[s_] := URL["http://example.org/" <> s];bookGraph = RDFStore[{
RDFTriple[ex["faust1"], ex["yearPublished"], 1808],
RDFTriple[ex["faust2"], ex["yearPublished"], 1832],
RDFTriple[ex["RomeoAndJuliet"], ex["yearPublished"], 1597]
}];bookGraph//SPARQLSelect[RDFTriple[SPARQLVariable["book"], ex["yearPublished"], SPARQLVariable["year"]] /; SPARQLVariable["year"] < 1820]Optional (1)
Needs["GraphStore`"]foaf[s_] := URL["http://xmlns.com/foaf/0.1/" <> s];
ex[s_] := URL["http://example.org/" <> s];personGraph = RDFStore[{
RDFTriple[ex["alice"], foaf["nick"], "Alice"],
RDFTriple[ex["alice"], foaf["mbox"], URL["mailto:alice@example.org"]],
RDFTriple[ex["bob"], foaf["nick"], "Bob"]
}];Find the name and optionally the e-mail address of all people:
personGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["p"], foaf["nick"], SPARQLVariable["name"]],
SPARQLOptional[RDFTriple[SPARQLVariable["p"], foaf["mbox"], SPARQLVariable["mail"]]]
} -> {"name", "mail"}]When the SPARQLOptional wrapper is omitted the solution for "Bob" is missing:
personGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["p"], foaf["nick"], SPARQLVariable["name"]],
RDFTriple[SPARQLVariable["p"], foaf["mbox"], SPARQLVariable["mail"]]
} -> {"name", "mail"}]Union (1)
Needs["GraphStore`"]Data about things, using different "name" properties:
rdfs[s_] := URL["http://www.w3.org/2000/01/rdf-schema#" <> s];
dct[s_] := URL["http://purl.org/dc/terms/" <> s];
ex[s_] := URL["http://example.org/" <> s];thingsGraph = RDFStore[{
RDFTriple[ex["earth"], rdfs["label"], "Earth"],
RDFTriple[ex["rubiksCube"], rdfs["label"], "Rubik's Cube"],
RDFTriple[ex["guide"], dct["title"], "The Hitchhiker's Guide"]
}];Find the "name" of all things:
thingsGraph//SPARQLSelect[RDFTriple[SPARQLVariable["thing"], rdfs["label"], SPARQLVariable["name"]] | RDFTriple[SPARQLVariable["thing"], dct["title"], SPARQLVariable["name"]] -> "name"]The same query using a property path:
thingsGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["thing"], {rdfs["label"] | dct["title"]}, SPARQLVariable["name"]] -> "name"]Negation (1)
Needs["GraphStore`"]foaf[s_] := URL["http://xmlns.com/foaf/0.1/" <> s];
ex[s_] := URL["http://example.org/" <> s];personGraph = RDFStore[{
RDFTriple[ex["alice"], foaf["nick"], "Alice"],
RDFTriple[ex["alice"], foaf["mbox"], URL["mailto:alice@example.org"]],
RDFTriple[ex["bob"], foaf["nick"], "Bob"]
}];Find people without e-mail address:
personGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], foaf["nick"], SPARQLVariable["name"]],
Except[RDFTriple[SPARQLVariable["person"], foaf["mbox"], SPARQLVariable["mbox"]]]
}]In this case a query based on NOT EXISTS gives the same result:
personGraph//SPARQLSelect[RDFTriple[SPARQLVariable["person"], foaf["nick"], SPARQLVariable["name"]] /; !SPARQLEvaluation["exists"][RDFTriple[SPARQLVariable["person"], foaf["mbox"], SPARQLVariable["mbox"]]]]Property Paths (1)
Needs["GraphStore`"]schema[s_] := URL["http://schema.org/" <> s];
ex[s_] := URL["http://example.org/" <> s];familyGraph = RDFStore[{
RDFTriple[ex["August"], schema["parent"], ex["Johann"]],
RDFTriple[ex["Alma"], schema["parent"], ex["August"]],
RDFTriple[ex["Wolfgang"], schema["parent"], ex["August"]],
RDFTriple[ex["Walther"], schema["parent"], ex["August"]]
}];familyGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["child"], {schema["parent"]..}, ex["Johann"]]]familyGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["child"], {schema["parent"], schema["parent"]}, ex["Johann"]]]Assignment (1)
Needs["GraphStore`"]schema[s_] := URL["http://schema.org/" <> s];
ex[s_] := URL["http://example.org/" <> s];productGraph = RDFStore[{
RDFTriple[ex["pizza"], schema["price"], 10],
RDFTriple[ex["pizza"], ex["commission"], .20],
RDFTriple[ex["pasta"], schema["price"], 8],
RDFTriple[ex["pasta"], ex["commission"], .25],
RDFTriple[ex["friedRice"], schema["price"], 10],
RDFTriple[ex["friedRice"], ex["commission"], .20]
}];Look up the price including delivery fee:
productGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]],
RDFTriple[SPARQLVariable["food"], ex["commission"], SPARQLVariable["c"]],
"totalPrice" -> SPARQLVariable["price"](1 + SPARQLVariable["c"])
} -> {"food", "totalPrice"}]The same query using a "select expression":
productGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]],
RDFTriple[SPARQLVariable["food"], ex["commission"], SPARQLVariable["c"]]
} -> {"food", "totalPrice" -> SPARQLVariable["price"](1 + SPARQLVariable["c"])}]Filter products by total price:
productGraph//SPARQLSelect[{
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]],
RDFTriple[SPARQLVariable["food"], ex["commission"], SPARQLVariable["c"]],
"totalPrice" -> SPARQLVariable["price"](1 + SPARQLVariable["c"])
} /; SPARQLVariable["totalPrice"] < 11 -> {"food", "totalPrice"}]Look up the price for certain products:
productGraph//SPARQLSelect[{
SPARQLValues["food", {ex["pizza"], ex["pasta"]}],
RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]]
}]Aggregates (1)
Needs["GraphStore`"]salaryGraph = ImportString["
base <http://example.org/>
<bob> <department> <physics>; <salary> 3000 .
<anna> <department> <physics>; <salary> 2000 .
<tom> <department> <math>; <salary> 5000 .
<helga> <department> <it>; <salary> 3000 .
<carl> <department> <it>; <salary> 1000 .
", "Turtle"];ex[s_] := URL["http://example.org/" <> s];salaryGraph//SPARQLQuery[
SPARQLSelect[RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]]] /* SPARQLAggregate["avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]]
]The average salary by department:
salaryGraph//SPARQLQuery[
SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]],
RDFTriple[SPARQLVariable["person"], ex["department"], SPARQLVariable["dept"]]
}] /* SPARQLAggregate[
{
"dept",
"avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]
},
SPARQLVariable["dept"]
]
]Filter the groups by the total department salary:
salaryGraph//SPARQLQuery[
SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]],
RDFTriple[SPARQLVariable["person"], ex["department"], SPARQLVariable["dept"]]
}] /* SPARQLAggregate[
{
"dept",
"avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]
},
SPARQLVariable["dept"],
SPARQLEvaluation["sum"][SPARQLVariable["salary"]] > 4000
]
]Order by decreasing average salary:
salaryGraph//SPARQLQuery[
SPARQLSelect[{
RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]],
RDFTriple[SPARQLVariable["person"], ex["department"], SPARQLVariable["dept"]]
}] /* SPARQLAggregate[
{
"dept",
"avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]
},
SPARQLVariable["dept"],
SPARQLEvaluation["sum"][SPARQLVariable["salary"]] > 4000,
SPARQLEvaluation["avg"][SPARQLVariable["salary"]] -> "Descending"
]
]Neat Examples (1)
Needs["GraphStore`"]Make a Graph of parent taxons (P171) of the Whale (Q42196) using Wikidata:
wikibase[s_] := URL["http://wikiba.se/ontology#" <> s];
bd[s_] := URL["http://www.bigdata.com/rdf#" <> s];
wd[s_] := URL["http://www.wikidata.org/entity/" <> s];
wdt[s_] := URL["http://www.wikidata.org/prop/direct/" <> s];SPARQLExecute[
"https://query.wikidata.org/sparql",
SPARQLSelect[{
SPARQLPropertyPath[wd["Q42196"], {wdt["P171"]...}, SPARQLVariable["taxon"]],
SPARQLOptional[RDFTriple[SPARQLVariable["taxon"], wdt["P171"], SPARQLVariable["parentTaxon"]]],
SPARQLService[wikibase["label"], RDFTriple[bd["serviceParam"], wikibase["language"], "en"]]
} -> {"taxon", "taxonLabel", "parentTaxon"}]
]//Function[whaleTaxons,
Graph[
Labeled[#taxon, First[#taxonLabel]]& /@ whaleTaxons,
If[KeyExistsQ[#, "parentTaxon"], #taxon -> #["parentTaxon"], Nothing]& /@ whaleTaxons
]
]Related Guides
Text
Wolfram Research (2019), SPARQLSelect, Wolfram Language function, https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html.
CMS
Wolfram Language. 2019. "SPARQLSelect." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html.
APA
Wolfram Language. (2019). SPARQLSelect. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html
BibTeX
@misc{reference.wolfram_2026_sparqlselect, author="Wolfram Research", title="{SPARQLSelect}", year="2019", howpublished="\url{https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_sparqlselect, organization={Wolfram Research}, title={SPARQLSelect}, year={2019}, url={https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html}, note=[Accessed: 12-June-2026]}