Search code examples
sparqlwikidata

Wikidata SPARQL: Get Property Value


I used the query builder to find all instances where property Q1902is available. A portion of the query returned by the builder is:

SELECT DISTINCT ?item ?statement0 WHERE {
  ?item p:P1902 ?statement0.
  ?statement0 (ps:P1902) _:anyValueP1902.
}
LIMIT 100

How can I get the actual value (or label) of the property returned in the query rather than the link to it?


Solution

  • You need a variable (instead of a blank node) in place of the value. Currently, you are only querying if a value exists. With a variable, you can get the value in the results.

    SELECT DISTINCT ?item ?statement0 ?value
    WHERE {
      ?item p:P1902 ?statement0 .
      ?statement0 ps:P1902 ?value .
    }
    LIMIT 100
    

    If you are not interested in the ?statement0 instances, you can use a sequence path:

    SELECT DISTINCT ?item ?value
    WHERE {
      ?item p:P1902/ps:P1902 ?value .
    }
    LIMIT 100