Get properties by QID?

1.5k views Asked by At

I can get item and its properties by label:

SELECT distinct ?item ?itemLabel ?itemDescription
  (SAMPLE(?DR) as ?DR) (SAMPLE(?article)as ?article)
WHERE {
  ?item wdt:P31 wd:Q5.
  ?item ?label "Einstein"@en
  OPTIONAL{?item wdt:P569 ?DR .}
  ?article schema:about ?item .
  ?article schema:inLanguage "en" .
  ?article schema:isPartOf <https://en.wikipedia.org/>.
  OPTIONAL{?item wdt:P570 ?RIP .}
  OPTIONAL{?item wdt:P18 ?image .}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?itemLabel ?itemDescription

See on Wikidata Query Services.

How can I do the same using QID instead label?

3

There are 3 answers

1
UninformedUser On BEST ANSWER

Using the URI instead of the variable ?item will get the information based on the entity Albert Einstein:

PREFIX  schema: <http://schema.org/>
PREFIX  bd:   <http://www.bigdata.com/rdf#>
PREFIX  wdt:  <http://www.wikidata.org/prop/direct/>
PREFIX  wikibase: <http://wikiba.se/ontology#>

SELECT DISTINCT  ?item ?itemLabel ?itemDescription (SAMPLE(?DR) AS ?DRSample) (SAMPLE(?article) AS ?articleSample)
WHERE
  { ?article  schema:about       ?item ;
              schema:inLanguage  "en" ;
              schema:isPartOf    <https://en.wikipedia.org/>
    FILTER ( ?item = <http://www.wikidata.org/entity/Q937> )
    OPTIONAL
      { ?item  wdt:P569  ?DR }
    OPTIONAL
      { ?item  wdt:P570  ?RIP }
    OPTIONAL
      { ?item  wdt:P18  ?image }
    SERVICE wikibase:label
      { bd:serviceParam
                  wikibase:language  "en"
      }
  }
GROUP BY ?item ?itemLabel ?itemDescription
6
maxlath On

If you already have the QID of the entity you are looking for and simply look for its properties and labels, you're better off using the Wikidata API wbgetentities module

In A. Einstein (Q937) case, that would give the following API call: https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q937&format=json

1
Inkling On

You can utilize the already known QID by using BIND:

BIND(wd:Q937 AS ?item).
...