Talk:Wikibase/Indexing/SPARQL Query Examples

See also Wikidata Talk:SPARQL query service/queries

Why no comments?
It is hard to understand the queries without comments. Does SPARQL interface allow them? --Techtonik (talk) 15:00, 6 June 2015 (UTC)
 * Yes it does. Some queries have them. Please add comments, comments are good :) --Smalyshev (WMF) (talk) 07:41, 11 June 2016 (UTC)

programatic access
I am trying to execute SPARQL queries at the endpoint http://wdqs-beta.wmflabs.org/ from an Apache Jena program. It works fine for the DBpedia endpoint, but its failing on this one because the response is in HTML - presumably for human consumption. Is there a query parameter that can be set to control the response type? Code:

public static void main(String[] args) {

String queryStr = "select distinct ?Concept where {[] a ?Concept} LIMIT 10";

Query query = QueryFactory.create(queryStr);

//does not work:  Endpoint returned Content-Type: text/html

//try ( QueryExecution qexec = QueryExecutionFactory.sparqlService("http://wdqs-beta.wmflabs.org/", query) ) {

//works

try ( QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query) ) {

// Set the DBpedia specific timeout.

((QueryEngineHTTP)qexec).addParam("timeout", "10000") ;

// Execute.

ResultSet rs = qexec.execSelect;

ResultSetFormatter.out(System.out, rs, query);

} catch (Exception e) {

e.printStackTrace;

}

}

--I9606 (talk) 00:27, 4 September 2015 (UTC)

Note that changing the URL of the endpoint to http://wdqs-beta.wmflabs.org/bigdata/namespace/wdq/sparql did not make any difference and that the same code seems to work fine on all the other endpoints that I have tried.

Female mayors
Hello,

I do not understand why Martine Aubry does not appear in the top 100 of female mayors : [https://query.wikidata.org/#PREFIX%20wikibase%3A%20%3Chttp%3A%2F%2Fwikiba.se%2Fontology%23%3E%0APREFIX%20wd%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%20%0APREFIX%20wdt%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2F%3E%0APREFIX%20rdfs%3A%20%3Chttp%3A%2F%2Fwww.w3.org%2F2000%2F01%2Frdf-schema%23%3E%0APREFIX%20p%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2F%3E%0APREFIX%20q%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fqualifier%2F%3E%0APREFIX%20v%3A%20%3Chttp%3A%2F%2Fwww.wikidata.org%2Fprop%2Fstatement%2F%3E%0A%0ASELECT%20DISTINCT%20%3Fcity%20%3FcityLabel%20%3Fmayor%20%3FmayorLabel%20%3Fpopulation%20WHERE%20{%0A%20%20%3Fcity%20wdt%3AP31%2Fwdt%3AP279*%20wd%3AQ515%20.%20%20%23%20find%20instances%20of%20subclasses%20of%20city%0A%20%20%3Fcity%20p%3AP6%20%3Fstatement%20.%20%20%20%20%20%20%20%20%20%20%20%20%23%20with%20a%20P6%20%28head%20of%20goverment%29%20statement%0A%20%20%3Fstatement%20v%3AP6%20%3Fmayor%20.%20%20%20%20%20%20%20%20%20%20%20%23%20...%20that%20has%20the%20value%20%3Fmayor%0A%20%20%3Fmayor%20wdt%3AP21%20wd%3AQ6581072%20.%20%20%20%20%20%20%20%23%20...%20where%20the%20%3Fmayor%20has%20P21%20%28sex%20or%20gender%29%20female%0A%20%20FILTER%20NOT%20EXISTS%20{%20%3Fstatement%20q%3AP582%20%3Fx%20}%20%20%23%20...%20but%20the%20statement%20has%20no%20P582%20%28end%20date%29%20qualifier%0A%20%0A%20%20%23%20Now%20select%20the%20population%20value%20of%20the%20%3Fcity%0A%20%20%23%20%28wdt%3A%20properties%20use%20only%20statements%20of%20%22preferred%22%20rank%20if%20any%2C%20usually%20meaning%20%22current%20population%22%29%0A%20%20%3Fcity%20wdt%3AP1082%20%3Fpopulation%20.%0A%20%20%23%20Optionally%2C%20find%20English%20labels%20for%20city%20and%20mayor%3A%0A%20%20SERVICE%20wikibase%3Alabel%20{%0A%20%20%20%20bd%3AserviceParam%20wikibase%3Alanguage%20%22fr%22%20.%0A%20%20}%0A%20}%20ORDER%20BY%20DESC%28%3Fpopulation%29%20LIMIT%20100 try the query]

Best regards,

--Gloumouth1 (talk) 11:25, 15 September 2015 (UTC)
 * Hello Gloumouth1. Lille is not an instance of city, nor an instance of an item which is a subclass of city. There is a query to list all items which are subclass of city. Yep, it's weird that commune of France is not a subclass of city. — Envlh (talk) 17:05, 18 September 2015 (UTC)

No claim items
Would you please write an query to list the items which are linked to enwiki and don't have any claims? Yamaha5 (talk) 13:36, 18 September 2015 (UTC)

Sitelinks
This page would use an example on how to extract/select sitelinks (in some language) for the Wikidata items corresponding to the query. --Nemo 09:39, 11 October 2015 (UTC)

References and qualifiers
Besides sitelinks it might also be interesting to see a simple query for references and qualifiers. The example with "Properties that are defined as "qualifier only" but are used in references" is fairly complex, and I find it difficult to generalize that one to other queries. &mdash; Fnielsen (talk) 12:07, 11 October 2015 (UTC)
 * I was helped by reading this paper: Reifying RDF: What Works Well With Wikidata?. &mdash; Fnielsen (talk) 13:43, 16 October 2015 (UTC)
 * I am also interested in simple examples involving references or qualifiers. For instance, how can the query for Wikidata items with a PMID (P698) or PMCID (P932) be modified to give all Wikidata items that have statements referenced to Wikidata items with a PMID (P698) or PMCID (P932)? --Daniel Mietchen (talk) 01:59, 4 January 2016 (UTC)


 * This page of example SPARQL queries on Wikidata itself may be helpful, see for example the following sections there:
 * US presidents and their spouses, in date order
 * Working with qualifiers
 * Working with references


 * The page could use some work -- for example, it would be better if more of the queries were shown explicitly on the page (perhaps in collapse boxes), rather than just as links. It also could use some more relevant queries.  But you may find it worth a look.  Jheald (talk) 17:42, 20 January 2016 (UTC)

African women without an article in French?
Hello, as part of WikiLovesWomen writing challenge, I was looking for dieas of article and have not been able to create the query I was willing which would list wikidata elenments :
 * for women
 * with a country that is on the continent Africa (excluding P30=Q15 with an attribute, as we don't want women from France, which has a P30=Q15 with an extra P518 as the P30 is not for the whole country)
 * bonus: ordered by number of existing interwiki for the subject, descending order

If someone knows how to do that, thanks in advance! Symac (talk) 14:09, 20 January 2016 (UTC)


 * For requests like this, you may get more response by posting at the SPARQL queries talk page on Wikidata itself, which I think may watched by more people than this page at MediaWiki. Jheald (talk) 17:47, 20 January 2016 (UTC)
 * thanks for the pointer, I have added my request there Symac (talk) 20:42, 20 January 2016 (UTC)

Who's birthday is it today
It doesnt run as an example. I think its to expensive for the enpoint to querry right now.

[|try try-it]

I am having this right now with a lot of my querries i am trying.

Is there somewhere a good tutorial on how i could setup my own sparql enpoint? Like DBpedia has

cheers

Air accidents
This should include subclasses. Thierry Caro (talk) 15:58, 20 March 2016 (UTC)

Wikibase/Indexing/SPARQL_Query_Examples
Not sure why, but I can't seem to be able to make this appear in "examples" on https://query.wikidata.org --Jura1 (talk) 16:35, 10 June 2016 (UTC)

Sorry there seems to be a bug omitting the last query...

Method in madness
I am trying to sort some of the queries into categories, because otherwise it is hard to comprehend them now (good problem to have, but still :). I think is it possible to achieve the situation when no category would have more than 10-15 items, and subdivide them as needed. Let's see if it works. --Smalyshev (WMF) (talk) 07:49, 11 June 2016 (UTC)

inventors killed by their own invention
Currently, the query 'inventors killed by their own invention' only returns a single result (Francis Edgar Stanley), but the list of inventors killed by their own inventions gives about thirty examples. Does this mean that the query is not good enough, that the metadata lacks or is there another cause? And how can we rectify this?

81.246.4.66 11:50, 27 July 2016 (UTC)

No/unknown value?
Could we have a sample query for those each, please? Thanks! --Magnus Manske (talk) 00:02, 13 August 2016 (UTC)

Inconsistent result
I am running this query and results are inconsistent. Sometimes wWikiTreeID has value and sometimes it is empty. On object Q1392324 P2949 is defined.

SELECT ?p ?w ?wWikiTreeID WHERE { {      ?p wdt:P22 wd:Q1392324. ?p wdt:P22 ?w. Optional {?w wdt:P2949 ?wWikiTreeID. } } }

Result: [{"p":"http://www.wikidata.org/entity/Q1376676","w":"http://www.wikidata.org/entity/Q1392324"}, {"p":"http://www.wikidata.org/entity/Q11724775","w":"http://www.wikidata.org/entity/Q1392324"}]