greatvovan
greatvovan

Reputation: 3157

How to query specific entities from Wikidata by ID using a SPARQL query?

I need a SPARQL query returning a specific Wikidata ID or a list of IDs. In real life such query is useless but I need it for testing purposes.

The easiest variant I could come up with was:

SELECT DISTINCT ?s
WHERE 
{
  ?s ?p ?o
  FILTER (?s = wd:Q151345).
}

I have to use DISTINCT because ?s ?p ?o matches every triple within Q151345, if I omit it it output the item as many times as many property-value pairs it has.

Is there any easier way?

Upvotes: 2

Views: 2430

Answers (2)

Steliyan Georgiev
Steliyan Georgiev

Reputation: 71

@greatvovan You can try

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

SELECT DISTINCT ?s WHERE {
    SELECT ?s WHERE 
    {
        VALUES ?s {  wd:Q151345 wd:Q2996394  }
        ?s ?p ?o
    }
 }

Upvotes: 2

Karima Rafes
Karima Rafes

Reputation: 1108

With the keyword VALUES, it's possible to use multiple instances.

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

SELECT ?s
WHERE 
{
  VALUES ?s {  wd:Q151345 wd:Q2996394  }
  ?s ?p ?o
}
LIMIT 10

Demo : http://linkedwiki.com/query/Query_multiple_instance_of_in_same_query

Doc: https://www.w3.org/TR/sparql11-query/#inline-data

Upvotes: 3

Related Questions