Search code examples
sparqlwikidatawikidata-query-service

How can I create a SPARQL query on Wikidata that gets all books that have a Project Gutenberg Ebook ID?


I notice that some books on Wikidata have Project Gutenberg ebook IDs. Is it possible to construct a query on the Wikidata Query Service that looks for all books that have this property? I'm new to SPARQL.


Solution

  • As you can see if you hover over the link for the Project Gutenberg e-book Id on https://www.wikidata.org/wiki/Q6511, the actual property code to be used in SPARQL is https://www.wikidata.org/wiki/Property:P2034, or wdt:P2034 for short.

    To retrieve all items with e-book IDs through SPARQL is then simply a matter of doing a basic triple match:

    SELECT ?work ?bookId
    WHERE { ?work wdt:P2034 ?bookId }
    

    Of course you can extend this query how you see fit. For example to retrieve the (English) title of the book instead of just the identifier, you can do something like this:

    SELECT ?title ?ebook_id
    WHERE { 
         ?work rdfs:label ?title; 
               wdt:P2034 ?ebook_id. 
         FILTER(langMatches(lang(?title), "en")) 
    }
    

    It's all a matter of adding the right combination of triple patterns and filters.