Saturday, March 25, 2023

Wikidata sparql examples

 Research in programming Wikidata/Cities - Wikiversity

Which country has the most sister cities?
#defaultView:BubbleChart
SELECT ?countryLabel (COUNT(?sister) as ?sisterCount) WHERE {       # Selecting number of distinct sister cities of particular country cities which are ... 
  SELECT DISTINCT ?countryLabel ?sister WHERE {                           
    VALUES ?cityTypes {wd:Q3957 wd:Q515 wd:Q1549591 wd:Q1637706}
    ?city wdt:P31 ?cityTypes.                                       # ... instances of different types of cities ...
    ?city wdt:P17 ?country.                                         # ... with filled property "country" ...
    ?city wdt:P190 ?sister.                                         # ... with filled property "sister city"
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  }                                 
}
GROUP BY ?countryLabel

ORDER BY DESC(?sisterCount)

Wikidata:SPARQL tutorial - Wikidata

# (film) items with "cast member P161" including "tom hanks Q2263"

SELECT DISTINCT ?item ?itemLabel WHERE {
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
 {
  SELECT DISTINCT ?item WHERE {
   ?item p:P161 ?statement0. # cast member
   ?statement0 (ps:P161/(wdt:P279*)) wd:Q2263. # tom hanks
  }
  LIMIT 100
 }
}

# simplified, same
SELECT DISTINCT ?item ?itemLabel 
WHERE {
  ?item p:P161 ?statement0. # cast member
  ?statement0 (ps:P161/(wdt:P279*)) wd:Q2263. # tom hanks
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 100


No comments: