Where do Mayors Come From?

What would have otherwise taken a lot of tedious research is now possible within a simple query on Wikidata with SPARQL..In this article, you will see how to build queries for Wikidata with Python and SPARQL by taking a look where mayors in Europe are born.All the code for this article and the interactive visualization can be found in this repository.Wikidata is a free and collaborative Linked Open Data (LOD) knowledge base which can be edited by humans and machines..To access the structured data you can query Wikidata by using its SPARQL endpoint which enables you to run advanced queries, or by using its REST API.But you might ask, what are those Wikidata items?.SPARQL is a query language used to retrieve data stored as RDF (Resource Description Framework) and it is standardized by the W3C..It is a powerful language to query Linked data and has been chosen for Wikidata..In this example, we want to list all countries in the European Union:SELECT ?country ?countryLabel WHERE { ?country wdt:P463 wd:Q458..We will cover more complicated prefixes later on in this tutorial when you will get deeper into the SPARQL data representation.Finally, you will see a confusing part SERVICE wikibase:label { bd:serviceParam wikibase:language "en"..In this query, this would be the countryLabel variable storing the label for the country variable..Note that the label is only retrieved for items that have a label in the particular language selected (in this case "en" for English), as there might be items that are not translated into this particular language.Interesting side-note: When running the query you will notice Kingdom of the Netherlands with Wikidata item Q29999 in the list of European countries..SPARQL enables us to retrieve those too which leads us to the next query:SELECT ?country ?countryLabel ?population ?area ?medianIncomeWHERE { ?country wdt:P463 wd:Q458..?country wdt:P1082 ?population..}}You can try this query here.Remember that we said SPARQL consists mostly of joins?.The reason for this is that each country item that has no population, area or median income as a property is ignored by the query.. More details

Leave a Reply