Search code examples
sparqlwikidata

Wikidata SPARQL - get company entities and the location of their headquarters


I'm having trouble extracting location attributes of company HQ's.

My query: finds all companies or sub-classes, and returns some basic properties such as ISIN and URL, and the Headquarter location.

I have tried to use this example to extend the Headquarter part of the query to return location information such as city, country, and coordinate latitude and longitude. However I am getting stuck on pulling the values or labels through.

Thank you

SELECT
  ?item ?itemLabel ?web ?isin ?hq ?hqloc ?inception

# valueLabel is only useful for properties with item-datatype
WHERE 
{
  ?item p:P31/ps:P31/wdt:P279* wd:Q783794.

  OPTIONAL{?item wdt:P856 ?web.} # get item
  OPTIONAL{?item wdt:P946 ?isin.} # get item
  OPTIONAL{?item wdt:P571 ?inception.} # get item
  OPTIONAL{?item wdt:P159 ?hq.}  

  OPTIONAL{?item p:P159 ?hqItem. # get property
           ?hqItem ps:P159 wd:Q515. # get property-statement wikidata-entity
           ?hqItem pq:P17 ?hqloc. # get country of city
           }

  ?article schema:about ?item .
  ?article schema:inLanguage "en" .
  ?article schema:isPartOf <https://en.wikipedia.org/>. 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }    
}
LIMIT 10

Solution

  • A more simplified query to select some of the values you mentioned:

    SELECT
    ?company ?companyLabel ?isin ?web ?country ?countryLabel ?inception
    
    WHERE 
    {
         ?article schema:inLanguage "en" .
         ?article schema:isPartOf <https://en.wikipedia.org/>. 
         ?article schema:about ?company . 
    
         ?company p:P31/ps:P31/wdt:P279* wd:Q783794.
    
         ?company wdt:P946 ?isin. 
         OPTIONAL {?company wdt:P856 ?web.}
         OPTIONAL {?company wdt:P571 ?inception.}
         OPTIONAL {?company wdt:P17 ?country.}
    
         SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }    
    } LIMIT 10
    

    What I changed:

    • changed some labels to be more explicit (ex: "?item" -> "?company")
    • usage of P17 to directly select the country
    • I removed the OPTIONAL on ISIN to show that there exist some values. You did not get a result because it seems that many company instances on Wikidata lack that information.

    From here, selecting the other values should be easy.