Search code examples
sparqlwikidata

Select all statements using alternate identifier in Wikidata SPARQL


When I use a Q ID to do a query, I can get all the statements and identifiers -- EVERYTHING, e.g.

SELECT ?value ?valueLabel ?prop ?propLabel {
    wd:Q112133367 ?prop ?value .
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

This query successfully returns about 30 rows of data.

However, that assumes that I know the Q ID in advance. I am trying to retrieve the same results, but using an external identifier, e.g. the IMDb ID because I don't know the Q ID in advance.

This query works to look up the Q ID given the IMDb external ID:

SELECT ?item  ?itemLabel 
WHERE 
{ 
  ?item wdt:P345 "tt13276352"
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

But this only returns a single row of data (the Q ID). I have tried adding some additional values to the query, but the query times out, e.g.

SELECT ?item  ?itemLabel ?foo ?prop ?value 
WHERE 
{ 
  ?item wdt:P345 "tt13276352" . 
  ?foo ?prop ?value .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

I am stuck thinking about this too much from the point of view of a relational database so I don't see how to select all the same bits of data as when I use the Q ID.


Solution

  • The problem is that you have introduced an extra variable ?foo.

    Re-use the ?item variable instead:

    SELECT ?item ?itemLabel ?prop ?value 
    WHERE 
    { 
      ?item wdt:P345 "tt13276352" . 
      ?item ?prop ?value .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
    }
    

    Link to Wikidata Query Service for the above.