Wikidata:Request a query
Request a query ![]() This is a page where SPARQL queries [Q114898838] can be requested. Please provide feedback if a query is written for you. You can also request help to rewrite queries that don't work anymore, due to the WDQS graph split. For sample queries, see Examples and Help:Dataset sizing. Property talk pages include also summary queries for these. For help writing your own queries, or other questions about queries, see Wikidata talk:SPARQL query service/queries and Wikidata:SPARQL query service/query optimization. Help resources about Wikidata Query Service (Q20950365) and SPARQL: Wikidata:SPARQL query service/Wikidata Query Help and Category:SPARQL. To report an issue about the Query Service (interface, results views, export...) please see Wikidata:Contact the development team/Query Service and search. |
![]() |
On this page, old discussions are archived. An overview of all archives can be found at this page's archive index. The current archive is located at 2025/05. |
![]() |
SpBot archives all sections tagged with {{Section resolved|1=~~~~}} after 2 days.
|
Most-recently-added statements (or items if that's easier) using a given qualifier, on any main property
editTitle says it all. Starting from this standard query, this page suggests I can use square brackets with the haswbstatement keyword, but I can't get it to work. Thanks! Swpb (talk) 15:42, 16 April 2025 (UTC)
- Yes, You can, like here (without [P560=Q10868751] You will get two results instead of one). So it works – why do You think it's not? You can see qualifiers used with whist property on one of this these lists (summarized, and with values – maybe this is what You are looking for). regards, Piastu (talk) 08:28, 17 April 2025 (UTC)
SELECT (COUNT(?item) AS ?items) ?pq {
?item p:P642 ?statement.
?statement ?pq ?pq_ .
?wdpq wikibase:qualifier ?pq .
} GROUP BY ?pq
SELECT ?item ?label ?pq ?pq_ {
?item p:P642 ?statement.
?statement ?pq ?pq_ .
?wdpq wikibase:qualifier ?pq .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,mul".
?item rdfs:label ?label.
}
}
@Piastu: Thanks for your response, but none of these queries are what I asked for. I want to see items using of (DEPRECATED) (P642) as a qualifier, with any main property. Something like "haswbstatement:*=*[P641=*]", except that doesn't work. I know how to get such items without regard to creation date, but I want to see the latest ones so I can talk to editors who are still using a deprecated property. Swpb (talk) 14:40, 17 April 2025 (UTC)
Weapons produced in EU query adjustment
editAt Wikidata:List of weapons produced by companies of EU countries there is
Warning: … is calling Template:Wikidata list with more than one value for the "(?country" parameter. Only the last value provided will be used.
That is because the query filters for all the countries of the European Union.
Previously, the query was filtering things via but I changed that at Special:Diff/2336617693 because checking that way may be slower performance and currently the table is not updated by the listeria bot possibly because of some performance issue and because Germany (Q183)part of (P361)European Union (Q458) was removed since it already has Germany (Q183)member of (P463)European Union (Q458) (I haven't checked if all the other EU countries also have that set).
It filters by the countries like so
FILTER((?country = wd:Q27)
|| (?country = wd:Q28)
|| (?country = wd:Q29)
[…]
How to filter differently? And if that is not enough to get the listeriabot to update the page what else needs to be done so that the page gets updated again? Btw, that tables shows many Wikidata items that have key data missing, in particular data about service retirement (P730) and related properties. Prototyperspective (talk) 17:50, 29 April 2025 (UTC)
- Hi, you can use
VALUES ?country { wd:Q27 wd:Q28 wd:Q29 ... }
to set specific values for the the?country
variable :). Samoasambia ✎ 11:21, 2 May 2025 (UTC)- Thanks, that worked. ListeriaBot still doesn't update the table without there being any error on its status page but maybe it takes a day or two. Prototyperspective (talk) 17:47, 2 May 2025 (UTC)
- The listeriabot still doesn't update the page. The bot status page nevertheless says OK. Earlier, another query was improved in a way that made it get updated by the listeriabot – can this query be improved anyhow to also enable it here? Made an issue about it here and that list linked there is working now somewhat and just not getting updated every week while the list of this thread doesn't work at all. Prototyperspective (talk) 15:01, 6 May 2025 (UTC)
Correlate date ranges
editHi there! I want to query the rulers of Mongol Empire, with the time each one ruled, and the Capital City where they did it. The idea is to use the Capital City for the time they started to rule, and the empty one (Xanadu) if none is in that range. I can select the different elements using the following query:
SELECT
DISTINCT
?item ?itemLabel ?date_start ?date_end ?capital ?capitalLabel ?who ?whoLabel ?date_start_capital ?date_end_capital
WHERE {
VALUES ?item { wd:Q12557 } .
?item p:P36 ?capital_stmt . # capital
?capital_stmt ps:P36 ?capital .
OPTIONAL {
?capital_stmt
pqv:P580 ?date_start_node_capital;
pqv:P582 ?date_end_node_capital.
?date_start_node_capital wikibase:timeValue ?date_start_capital.
?date_end_node_capital wikibase:timeValue ?date_end_capital.
}
?item p:P35 ?who_stmt . # head of state
?who_stmt ps:P35 ?who .
?who_stmt
pqv:P580 ?date_start_node;
pqv:P582 ?date_end_node.
?date_start_node wikibase:timeValue ?date_start.
?date_end_node wikibase:timeValue ?date_end.
SERVICE wikibase:label { bd:serviceParam wikibase:language "es" }
}
But I get lost when trying to correlate the dates. Any ideas? Otherwise I'll do it on code later on, but I wanted to check if it can be resolved directly during query time. Thanks in advance! Pruna.ar (talk) 22:38, 1 May 2025 (UTC)
Visualizing a King/President from a specific place in a specific time
editHello, I’m having some troubles understanding how to make things work with Wikidata.
My idea is: if a person over select Italy, in 1970, the name of the president of that time appears on a popup related to political information.
What I’m trying to understand is: before Italy was actually Italy, so before 1861, how do I query the item without hard coding the query like here.?
Same problem with Germany (and all the other countries). Here a query that goes back to 1871 for Germany, but I don’t know how to get the data before in a dynamic way.
Do you think is possible to query Wiki-data/pedia to obtain that specific information? Theoretically if the user select Rome, there should be some sort of continuity between the actual president of the republic, and roman emperors.
Ideally, it would have to work also for towns. But eventually I can distinguish between queries for nation-states and queries for towns
Thanks! Aoppo (talk) 12:42, 5 May 2025 (UTC)
- Hi @Aoppo:. In VALUES clause (You used it in Germany, last UNION) You can define sets of values
VALUES (?country ?leader_p ?leader_ps) {(wd:Q38 p:P6 ps:P6) (wd:Q172579 p:P6 ps:P6) (wd:Q172579 p:P35 ps:P35) (wd:Q172579 p:P1906 ps:P1906) }
(in this case three variables in each set), like this: https://w.wiki/D$t$. It's still kind of hardcoding, but with less code :) - Or You can code properties list with or
(P6 or P35)
like that:?country (p:P6|p:P35) ?statement.
. In this case in list of VALUES you need one variable with country list. (Also, if You don't want records without start date, You can querty for?statement pq:P580 ?start.
(without OPTIONAL clause), instead of adding FILTER later this variable later.) Something like that: https://w.wiki/D$vd. regards, Piastu (talk) 07:34, 6 May 2025 (UTC)- And if You want to get list of countries dynamically, instead of hardcoding it, You probably should to look at Property:P1365, Property:P1366, and use one of them in subselct to get list of countries. Like this: https://w.wiki/D$wj (maybe You need to modify
(p:P6|p:P35)
and add something to list). regards, Piastu (talk) 07:59, 6 May 2025 (UTC)
- And if You want to get list of countries dynamically, instead of hardcoding it, You probably should to look at Property:P1365, Property:P1366, and use one of them in subselct to get list of countries. Like this: https://w.wiki/D$wj (maybe You need to modify
Query of Nigerian female politician not on wikidata
editI need 100 lists of female notable politicians that has no items on wikidata Ojewuyib (talk) 12:35, 6 May 2025 (UTC)
- Unfortunately it is not possible to query for Wikidata items that do not exist. Samoasambia ✎ 10:47, 7 May 2025 (UTC)
- Most Wikipedia pages are already linked to Wikidata, to find pages that are not, you can use Petscan: https://petscan.wmcloud.org/?psid=34502547 . I entered the category 'Nigerian politicians' set depth to 6 (depth of subcategories). Then on the 'Wikidata' tab I checked 'only pages without item'. If you want to do this check on Nigerian Pidgin Wikipedia set language to 'pcm' and change the category to the correct equivalent. Infrastruktur (talk) 11:01, 11 May 2025 (UTC)
- @Ojewuyib: Ping. Infrastruktur (talk) 11:04, 11 May 2025 (UTC)
Random article that exists in more than N languages
editHello, I am trying to build a query that works similarly to the "Random article" feature on Wikipedia, where it only gives articles of worldwide notability. Even using WP's random good article generator will often give articles similar to one of the many good articles about specific road structures, for instance. Articles should:
1. Exist in the English wikipedia
2. Exist in more than N wikipedias.
Thank you very much! Thepixelpest (talk) 09:57, 11 May 2025 (UTC)
- Here is a query which sort of achieve this, I am not sure I interpreted your request correctly, also this doesn't seem to run fast on WDSQ but run well on QLever, try it (it is also using their template feature)
- Try it!
SELECT ?item ?n ?count ?enlanguagelink WHERE { { SELECT ?item ?n ?enlanguagelink (COUNT(?wikipedias) AS ?count) WHERE { { SELECT ?i ?n WHERE { VALUES ?power { 10000 100000 1000000 10000000 100000000 } VALUES ?sample { 0 1 } BIND (FLOOR(RAND() * ?power + ?sample) AS ?n) . } } BIND (IRI(CONCAT("http://www.wikidata.org/entity/Q", STR(?n))) AS ?item) . ?sitelink schema:about ?item . ?sitelink schema:isPartOf ?wikipedias . FILTER (CONTAINS(STR(?wikipedias),"wikipedia.org")) ?enlanguagelink schema:about ?item . ?enlanguagelink schema:isPartOf <https://en.wikipedia.org/> } GROUP BY ?item ?n ?enlanguagelink } FILTER (?count > 10) . } ORDER BY substr(STR(?n),2,5) LIMIT 1
- I think I was more creative than rigorous in implementing the concept of "random", I guess there are many directions this can be improved, also if you want to increase the minimum number of sitelinks you may need to add more values to "?samples" to be sure to always get a results, but more samples also means longer query time. Ttmms (talk)