query-18ecedd2af270c75f5be0c37ce5b9b96

rq turtle/ttl

How to get all Wikidata items in a country?Using SPARQL I want to show all French embassies in Liberia (spoiler: there is exactly one) So I wrote this naive query: SELECT ?item WHERE { ?item wdt:P17 wd:Q1014. # country: Liberia ?item wdt:P31 wd:Q3917681. # instance of: embassy ?item wdt:P137 wd:Q142. # operated by: France } . (Q1014)Liberia value of (P17)country , which itself has a (Q3748)Monrovia of (P131)located in the administrative territorial entity , more exactly it is there is such an item in Liberia items. Even though zero: It finds PROBLEMMore generally, I want to get ALL Wikidata items that are in a particular country. How to do? 03:39, 8 September 2016 (UTC)) talk (Syced )also asked at OpenData.StackExchange(Thanks! Thanks! While your tip works in Liberia, it fails in Germany: 05:12, 8 September 2016 (UTC)) talk (Yair rand after SELECT if you want to remove duplicates. --distinct, and it should work. Maybe also add ?item wdt:P131*/wdt:P17 wd:Q1014. with ?item wdt:P17 wd:Q1014.Replace

Use at

PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?item ?itemLabel ?countryLabel WHERE {
  ?item wdt:P131*/wdt:P17 wd:Q183.  # country: Germany
  ?item wdt:P31 wd:Q3917681.       # instance of: embassy
  ?item wdt:P137 ?country.          # operated by: ...
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en"}
  }

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?country") v1("?item"):::projected a1((" ")) c8(["bd:serviceParam"]):::iri c10(["en"]):::literal c3(["wd:Q183"]):::iri c5(["wd:Q3917681"]):::iri v1 --"wdt:P131"--> a1 a1 --"wdt:P17"--> c3 v1 --"wdt:P31"--> c5 v1 --"wdt:P137"--> v2 subgraph s1["http://wikiba.se/ontology#label"] style s1 stroke-width:4px; c8 --"wikibase:language"--> c10 end