WOLFRAM

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

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
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Triple Pattern  
Basic Graph Pattern  
Filter  
Show More Show More
Optional  
Union  
Negation  
Property Paths  
Assignment  
Aggregates  
Neat Examples  
See Also
Related Guides
Cite this Page

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 all

Basic Examples  (2)

Wolfram Language code: Needs["GraphStore`"]

Data about fruits:

Wolfram Language code: schema[s_] := URL["http://schema.org/" <> s]; ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: fruitGraph = RDFStore[{ RDFTriple[ex["banana"], schema["color"], "yellow"], RDFTriple[ex["strawberry"], schema["color"], "red"], RDFTriple[ex["strawberry"], ex["shape"], ex["round"]] }];

Find the color of fruits:

Wolfram Language code: fruitGraph//SPARQLSelect[RDFTriple[SPARQLVariable["fruit"], schema["color"], SPARQLVariable["color"]]]

Find the color and the shape of fruits:

Wolfram Language code: fruitGraph//SPARQLSelect[{ RDFTriple[SPARQLVariable["fruit"], schema["color"], SPARQLVariable["color"]], RDFTriple[SPARQLVariable["fruit"], ex["shape"], SPARQLVariable["shape"]] }]
Wolfram Language code: Needs["GraphStore`"]

Find formulas (P274) and the Japanese name of chemicals (Q11173) containing (P527) calcium (Q706) in Wikidata:

Wolfram Language code: 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];
Wolfram Language code: 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)

Wolfram Language code: Needs["GraphStore`"]

Example data:

Wolfram Language code: ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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:

Wolfram Language code: exampleGraph//SPARQLSelect[RDFTriple[ex["s1"], ex["p"], SPARQLVariable["x"]]]

Variables can appear in any position:

Wolfram Language code: exampleGraph//SPARQLSelect[RDFTriple[SPARQLVariable["x"], ex["p"], 8]]

A triple pattern does not need to contain any variables:

Wolfram Language code: exampleGraph//SPARQLAsk[RDFTriple[ex["s3"], ex["p"], 8]]

Basic Graph Pattern  (1)

Wolfram Language code: Needs["GraphStore`"]

Data about articles:

Wolfram Language code: dct[s_] := URL["http://purl.org/dc/terms/" <> s]; ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: 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)

Wolfram Language code: Needs["GraphStore`"]

Data about books:

Wolfram Language code: ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: bookGraph = RDFStore[{ RDFTriple[ex["faust1"], ex["yearPublished"], 1808], RDFTriple[ex["faust2"], ex["yearPublished"], 1832], RDFTriple[ex["RomeoAndJuliet"], ex["yearPublished"], 1597] }];

Books published before 1820:

Wolfram Language code: bookGraph//SPARQLSelect[RDFTriple[SPARQLVariable["book"], ex["yearPublished"], SPARQLVariable["year"]] /; SPARQLVariable["year"] < 1820]

Optional  (1)

Wolfram Language code: Needs["GraphStore`"]

Data about people:

Wolfram Language code: foaf[s_] := URL["http://xmlns.com/foaf/0.1/" <> s]; ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: personGraph//SPARQLSelect[{ RDFTriple[SPARQLVariable["p"], foaf["nick"], SPARQLVariable["name"]], RDFTriple[SPARQLVariable["p"], foaf["mbox"], SPARQLVariable["mail"]] } -> {"name", "mail"}]

Union  (1)

Wolfram Language code: Needs["GraphStore`"]

Data about things, using different "name" properties:

Wolfram Language code: 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];
Wolfram Language code: 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:

Wolfram Language code: thingsGraph//SPARQLSelect[RDFTriple[SPARQLVariable["thing"], rdfs["label"], SPARQLVariable["name"]] | RDFTriple[SPARQLVariable["thing"], dct["title"], SPARQLVariable["name"]] -> "name"]

The same query using a property path:

Wolfram Language code: thingsGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["thing"], {rdfs["label"] | dct["title"]}, SPARQLVariable["name"]] -> "name"]

Negation  (1)

Wolfram Language code: Needs["GraphStore`"]

Data about people:

Wolfram Language code: foaf[s_] := URL["http://xmlns.com/foaf/0.1/" <> s]; ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: personGraph//SPARQLSelect[RDFTriple[SPARQLVariable["person"], foaf["nick"], SPARQLVariable["name"]] /; !SPARQLEvaluation["exists"][RDFTriple[SPARQLVariable["person"], foaf["mbox"], SPARQLVariable["mbox"]]]]

Property Paths  (1)

Wolfram Language code: Needs["GraphStore`"]

Family data:

Wolfram Language code: schema[s_] := URL["http://schema.org/" <> s]; ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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"]] }];

Find descendents of Johann:

Wolfram Language code: familyGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["child"], {schema["parent"]..}, ex["Johann"]]]

Find only grandchildren:

Wolfram Language code: familyGraph//SPARQLSelect[SPARQLPropertyPath[SPARQLVariable["child"], {schema["parent"], schema["parent"]}, ex["Johann"]]]

Assignment  (1)

Wolfram Language code: Needs["GraphStore`"]

Data about food products:

Wolfram Language code: schema[s_] := URL["http://schema.org/" <> s]; ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: 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:

Wolfram Language code: 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":

Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: productGraph//SPARQLSelect[{ SPARQLValues["food", {ex["pizza"], ex["pasta"]}], RDFTriple[SPARQLVariable["food"], schema["price"], SPARQLVariable["price"]] }]

Aggregates  (1)

Wolfram Language code: Needs["GraphStore`"]

Data about salaries:

Wolfram Language code: 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"];

Find the average salary:

Wolfram Language code: ex[s_] := URL["http://example.org/" <> s];
Wolfram Language code: salaryGraph//SPARQLQuery[ SPARQLSelect[RDFTriple[SPARQLVariable["person"], ex["salary"], SPARQLVariable["salary"]]] /* SPARQLAggregate["avgSalary" -> SPARQLEvaluation["avg"][SPARQLVariable["salary"]]] ]

The average salary by department:

Wolfram Language code: 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:

Wolfram Language code: 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:

Wolfram Language code: 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)

Wolfram Language code: Needs["GraphStore`"]

Make a Graph of parent taxons (P171) of the Whale (Q42196) using Wikidata:

Wolfram Language code: 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];
Wolfram Language code: 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 ] ]
Wolfram Research (2019), SPARQLSelect, Wolfram Language function, https://reference.wolfram.com/language/GraphStore/ref/SPARQLSelect.html.

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: 18-August-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: 18-August-2026]}

Top [フレーム]

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