User:Battleofalma/Building queries BL
QueriesEdit
Basic query building on Wikidata using paintings as the subjectEdit
"Find all of one thing" So, find all paintings on Wikidata.
SELECT ?item #the labels for the things in the query
WHERE { ?item wdt:P31 wd:Q3305213 #instance of paintings, no labels.
}
"Find all of one thing, with this quality" Find all paintings on Wikidata by Paul Cezanne.
SELECT ?item ?itemLabel #added a Label here
WHERE { ?item wdt:P31 wd:Q3305213 . #instance of paintings,
?item wdt:P170 wd:Q35548 . #creator is Paul Cezanne
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } #added labelservice here
}
"Find all of one thing, with this quality, and something about them" Find all paintings on Wikidata, that are Impressionist, and their creators.
SELECT ?item ?itemLabel ?artistLabel #added a Label here for artist as well.
WHERE { ?item wdt:P31 wd:Q3305213 . #instance of paintings,
?item wdt:P135 wd:Q40415 . #movement is Impressionism
?item wdt:P170 ?artist #asking for the creator statement of the results.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } #added labelservice here
}
Taking away criteria from your search using MINUS functionEdit
"Find all of one thing, with this quality, information, but not these things" Find all paintings on Wikidata, that are Impressionist, and their creators, but not by Monet.
SELECT ?item ?itemLabel ?artist ?artistLabel #Item label here
WHERE { ?item wdt:P31 wd:Q3305213 . #instance of paintings,
?item wdt:P135 wd:Q40415 . #movement is Impressionism
?item wdt:P170 ?artist #show me the artists
MINUS { ?item wdt:P170 wd:Q296 . } #the "items" (paintings) should be "minus" creator = Monet
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } #added labelservice here
}
"Find all of one thing, with this quality, information, but not these things" Find all paintings on Wikidata, that are Impressionist, and their creators, and their locations, but not by Monet.
SELECT ?item ?itemLabel ?artist ?artistLabel ?locationLabel ?coordsLabel #Added labels up here for coordinates and locations
WHERE { ?item wdt:P31 wd:Q3305213 . #instance of paintings,
?item wdt:P135 wd:Q40415 . #movement is Impressionism
?item wdt:P170 ?artist . #show me the artists
?item wdt:P276 ?location . #show me the location of the items (e.g galleries)
?location wdt:P625 ?coords . #show me the coordinates of those locations
MINUS { ?item wdt:P170 wd:Q296 . } #the "items" (paintings) should be "minus" creator = Monet
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } #added labelservice here
}
Same as above but show me a map.
#defaultView:Map
SELECT ?item ?itemLabel ?artist ?artistLabel ?locationLabel ?coords ?coordsLabel
#Added labels up here for coords, coordinates and locations otherwise map doesn't show
WHERE { ?item wdt:P31 wd:Q3305213 . #instance of paintings,
?item wdt:P135 wd:Q40415 . #movement is Impressionism
?item wdt:P170 ?artist . #show me the artists
?item wdt:P276 ?location . #show me the location of the items (e.g galleries)
?location wdt:P625 ?coords . #show me the coordinates of those locations
MINUS { ?item wdt:P170 wd:Q296 . } #the "items" (paintings) should be "minus" creator = Monet
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } #added labelservice here
}
Using FILTER to specify results must be this or thisEdit
"Show me paintings by Anthony Van Dyck that are in either the Louvre or Rijksmuseum"
The following query uses these:
- Items: visual artwork (Q4502142) , Anthony van Dyck (Q150679) , Rijksmuseum (Q190804) , Louvre Museum (Q19675)
- Properties: instance of (P31) , subclass of (P279) , creator (P170) , location (P276)
SELECT DISTINCT ?item ?itemLabel ?location ?locationLabel ?creator ?creatorLabel WHERE { ?item (wdt:P31/wdt:P279*) wd:Q4502142 . #using broader "visual artwork" here and both "instance of" and "subclass of" to include paintings/prints etc ?item wdt:P170 wd:Q150679 . #creator is Anthony Van Dyck ?item wdt:P276 ?location . #define location ?item wdt:P170 ?creator . #define creator FILTER ( ?location = wd:Q190804 | | ?location = wd:Q19675 ) #location defined earlier is either... SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
The following query uses these:
- Properties: instance of (P31) , subclass of (P279) , present in work (P1441)
SELECT ?item ?itemLabel WHERE { ?item (wdt:P31 / wdt:P279) wd:Q95074 . #a fictional character or subclass of a fictional character ?item wdt:P1441 ?work #present in work FILTER ( ?work = wd:Q15228 || ?work = wd:Q8337 ) #that work is either Lord of the Rings OR Harry Potter SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
Using OPTIONAL to find statements that don't apply to all resultsEdit
The following query uses these:
- Properties: instance of (P31) , subclass of (P279) , present in work (P1441) , cause of death (P509) , killed by (P157)
SELECT DISTINCT ?person ?personLabel ?deathLabel ?killerLabel WHERE { ?person (wdt:P31 / wdt:P279) wd:Q95074 . ?person wdt:P1441 wd:Q478360 . ?person wdt:P509 ?death OPTIONAL { ?person wdt:P157 ?killer . } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
Using P, PS, to fine nodes (multiple statement values)Edit
The following query uses these:
- Items: Winston Churchill (Q8016)
- Properties: position held (P39)
#Positions held by Winston Churchill SELECT DISTINCT ?countryLabel ?populationStatement ?population WHERE { VALUES ?country {wd:Q8016} ?country p:P39 ?populationStatement . # the statement "node", which all other data is connected to ?populationStatement ps:P39 ?population . # the actual value of population statement SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } } ORDER BY ?countryLabel