title: Wikidata SPARQL - get company entities and the location of their headquarters
body: <p>I'm having trouble extracting location attributes of company HQ's. </p>
  
  <p>My <a href="https://query.wikidata.org/#%23%20Sample%20to%20query%20all%20values%20of%20a%20property%0A%23%20Property%20talk%20pages%20on%20Wikidata%20include%20basic%20queries%20adapted%20to%20each%20property%0ASELECT%0A%20%20%3Fitem%20%3FitemLabel%20%3Fweb%20%3Fisin%20%3Fhq%20%3Fhqloc%20%3Finception%0A%0A%23%20valueLabel%20is%20only%20useful%20for%20properties%20with%20item-datatype%0AWHERE%20%0A%7B%0A%20%20%3Fitem%20p%3AP31%2Fps%3AP31%2Fwdt%3AP279%2A%20wd%3AQ783794.%0A%20%20%20%20%20%20%20%20%0A%20%20OPTIONAL%7B%3Fitem%20wdt%3AP856%20%3Fweb.%7D%20%23%20get%20item%0A%20%20OPTIONAL%7B%3Fitem%20wdt%3AP946%20%3Fisin.%7D%20%23%20get%20item%0A%20%20OPTIONAL%7B%3Fitem%20wdt%3AP571%20%3Finception.%7D%20%23%20get%20item%0A%20%20optional%7B%3Fitem%20wdt%3AP159%20%3Fhq.%7D%20%20%0A%20%20%0A%20%20%0A%20%20optional%7B%3Fitem%20p%3AP159%20%3FhqItem.%20%23%20get%20property%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%3FhqItem%20ps%3AP159%20wd%3AQ515.%20%23%20get%20property-statement%20wikidata-entity%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3FhqItem%20pq%3AP17%20%3Fhqloc.%20%23%20get%20country%20of%20city%0A%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%0A%20%20%3Farticle%20schema%3Aabout%20%3Fitem%20.%0A%20%20%3Farticle%20schema%3AinLanguage%20%22en%22%20.%0A%20%20%3Farticle%20schema%3AisPartOf%20%3Chttps%3A%2F%2Fen.wikipedia.org%2F%3E.%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22.%20%7D%20%20%20%20%0A%7D%0A%0ALIMIT%2010" rel="nofollow noreferrer">query</a>: finds all companies or sub-classes, and returns some basic properties such as ISIN and URL, and the Headquarter location. </p>
  
  <p>I have tried to use <a href="https://opendata.stackexchange.com/questions/10638/wikidata-get-property-of-property/10640">this example</a> to extend the Headquarter part of the query to return location information such as city, country, and <a href="https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/queries/examples#All_museums_(including_subclass_of_museum)_in_Washington,_D.C._with_coordinates" rel="nofollow noreferrer">coordinate latitude and longitude</a>. However I am getting stuck on pulling the values or labels through.</p>
  
  <p>Thank you</p>
  
  <pre><code>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 &lt;https://en.wikipedia.org/&gt;. 
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }    
  }
  LIMIT 10
  </code></pre>