Wikidata:Request a query

Request a query
Fishing in the Wikidata river requires both an idea where to look for fish and a suitable fishing method. If you have the former, this page can help you find the latter.

This is a page where SPARQL 1.1 Query Language (Q32146616) queries can be requested. Please provide feedback if a query is written for you.

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 2024/04.

largest lakes no enwiki edit

Am I able to get the lakes with the largest surface area but have no enwiki sitelink 115.188.127.196 06:02, 10 April 2024 (UTC)Reply

Hello,
Here is my attempt at writing your query: https://w.wiki/9kpa
It works but times out unless LIMIT 10 is set. Can anyone more knowledgeable take a look at it and suggest improvements? Thanks! Vincent Tep (talk) 19:49, 14 April 2024 (UTC)Reply
As explained here, turning off this single line of code: SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } prevents the query from timing out. It returns 54,443 hits. Vincent Tep (talk) 19:57, 14 April 2024 (UTC)Reply
Time is running out huh? Didn't the english band Muse write a song about that? :-) filter (not) exists can be slow some times - https://w.wiki/9kqs . Infrastruktur (talk) 21:23, 14 April 2024 (UTC)Reply

How to list authors from 'Has Parts' of a legal decision? edit

Hello! There are many wikidata entries for Supreme Court decisions (from the US and Sweden, to name two) and I would like to do the same for Canada. It appears that the existing practice is to represent a 'Majority Opinion' from a 'Dissenting Opinion' by using the 'Has Parts' property. (see example: Q125349256)

I am trying to generate a query that will extract the author of a legal opinion from Has Parts but I am stuck. https://w.wiki/9jsP

I believe that one way I can collect this information is using PS or PQ to get the qualifiers from Has Parts, but I've been unable to formulate anything that works. Any hints or help is appreciated! Copystar (talk) 17:13, 12 April 2024 (UTC)Reply

Does this work or do you need it tweaked?
SELECT ?item  ?itemLabel ?date ?qqLabel ?v ?vLabel
WHERE {
  ?item wdt:P31 wd:Q19930933. 
  ?item wdt:P577 ?date. FILTER(?date >= "2024-01-01"^^xsd:dateTime) 
  ?item p:P527 ?hp.    
  ?hp a wikibase:BestRank .
  ?hp ?q ?v . filter (?q in (pq:P50, pq:P7122))
  ?qq wikibase:qualifier ?q .

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ASC(?date)
Try it!
Infrastruktur (talk) 20:43, 15 April 2024 (UTC)Reply
This is wonderful - thank you so much for helping get past this roadblock! It does need some tweaking however.
Is it possible to extract the parent values of 'Parts of / P27' (Majority Opinion of) and (Dissenting Opinion of) so that the child values of 'author and name' have the necessary context?
The ideal end result would express (as in this example Q125275161) that the parent of Majority Opinion, had a child of Author "Andromache Karakatsanis", and three other people in that category of the 'opinion joined by.' At the moment, the results make it unable to determine if an opinion joined by is for the majority opinion or a dissenting opinion.
In Wikipedia, there have been several attempts to keep track of this information manually in table format https://en.wikipedia.org/wiki/2022_reasons_of_the_Supreme_Court_of_Canada
My hope is to make a query-based simplified version of this Wikipedia table that can be dynamically updated. Again, thank you @Infrastrukturso much for helping with this! Copystar (talk) 11:32, 16 April 2024 (UTC)Reply
Second version: https://w.wiki/9n5T . Clicking on the claim links will take you directly to the claims, which is useful if the item is large. Infrastruktur (talk) 21:27, 16 April 2024 (UTC)Reply
This is marvelous! Thank you so much for your help with this. This fits the bill! All the best! Copystar (talk) 01:24, 17 April 2024 (UTC)Reply

One and only one external identifier edit

Hi !

Is it possible to create a query where I can get the items with one and only one, specific or not, external identifier ? As an example, Sam James Harper (Q98442698) only have one external identifier (INSPIRE-HEP author ID (P2930)). How can I got all the items with ONLY this external identifier and no other external identifiers ? How can I got all the items who have only a specific number of external identifiers values ?

Thank you ! Simon Villeneuve (talk) 11:06, 14 April 2024 (UTC)Reply

I think this should work:
SELECT ?item ?itemLabel ?id WHERE {
  ?item wikibase:identifiers 1 .
  ?item wdt:P2930 ?id
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!

I found wikibase:identifiers by running

DESCRIBE wd:Q98442698
Try it!

Piecesofuk (talk) 15:24, 14 April 2024 (UTC)Reply

Thank you for the reply and the additional information. It will be very helpful to me! Simon Villeneuve (talk) 17:08, 14 April 2024 (UTC)Reply
I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. author  TomT0m / talk page 17:53, 21 April 2024 (UTC)Reply

Cross two variables in a table edit

Hi! This query

#title:Byzantinists by citizenship and gender
SELECT ?nLabel ?gLabel (COUNT(?item) AS ?number)
WHERE {
  ?item wdt:P106 wd:Q26132815 ; wdt:P27 ?n ; wdt:P21 ?g .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
GROUP BY ?nLabel ?gLabel
ORDER BY ?nLabel
Byzantinists by citizenship and gender

as of now gives in column 3 (?number) che counts of items combining the conditions in columns 1 (?nLabel) and 2 (?gLabel); in order to compact the view of the table, would it be possible to convert the values of column 2 (?gLabel) into headers of column, so as to have basically the various countries as the values of column 1, the values of sex or gender (P21) as headers of columns and all the numbers in the other cells? I.e. something like

- female (Q6581072) male (Q6581097)
Italy (Q38) 24 54
Greece (Q41) 14 58

Thanks in advance, --Epìdosis 13:23, 14 April 2024 (UTC)Reply

I don't know if you're interested in queries, but I made several solutions just for fun.
1. Conditional counting.
#title:Byzantinists by citizenship and gender
SELECT ?nLabel (SUM(?male_) AS ?male) (SUM(?female_) AS ?female) (SUM(?other_) AS ?other) 
WHERE {
  ?item wdt:P106 wd:Q26132815 ; wdt:P27 ?n ; wdt:P21 ?g .
  bind (IF(?g = wd:Q6581097, 1, 0) as ?male_)
  bind (IF(?g = wd:Q6581072, 1, 0) as ?female_)
  bind (IF(?g not in (wd:Q6581097, wd:Q6581072), 1, 0) as ?other_)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
GROUP BY ?nLabel
ORDER BY ?nLabel
Byzantinists by citizenship and gender
2. Split into mutually exclusive subsets.
#title:Byzantinists by citizenship and gender
SELECT ?nLabel (COUNT(?male_) AS ?male) (COUNT(?female_) AS ?female) (COUNT(?other_) AS ?other) 
WHERE {
  {
    ?item wdt:P106 wd:Q26132815 ; wdt:P27 ?n .
    ?item wdt:P21 wd:Q6581097 .
    bind ("m" as ?male_)
  } UNION {
    ?item wdt:P106 wd:Q26132815 ; wdt:P27 ?n .
    ?item wdt:P21 wd:Q6581072 .
    bind ("f" as ?female_)
  } UNION {
    ?item wdt:P106 wd:Q26132815 ; wdt:P27 ?n .
    ?item wdt:P21 ?g . filter (?g not in (wd:Q6581097, wd:Q6581072))
    bind ("o" as ?other_)
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
GROUP BY ?nLabel
ORDER BY ?nLabel
Byzantinists by citizenship and gender
3. Conditional binding.
#title:Byzantinists by citizenship and gender
SELECT ?nLabel (COUNT(?male_) AS ?male) (COUNT(?female_) AS ?female) (COUNT(?other_) AS ?other) 
WHERE {
  ?item wdt:P106 wd:Q26132815 ; wdt:P27 ?n ; wdt:P21 ?g .
  bind (coalesce(IF(?g = wd:Q6581097, 1, 1/0)) as ?male_)
  bind (coalesce(IF(?g = wd:Q6581072, 1, 1/0)) as ?female_)
  bind (coalesce(IF(?g not in (wd:Q6581097, wd:Q6581072), 1, 1/0)) as ?other_)
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
GROUP BY ?nLabel
ORDER BY ?nLabel
Byzantinists by citizenship and gender
Infrastruktur (talk) 17:58, 14 April 2024 (UTC)Reply

Number of items before a specific date edit

Kindly requesting the number/count of people with occupation (P106) = librarian (Q182436) worldwide added to Wikidata before 1st Feb 2024.

And, the number of people added with the same criteria above after 1st Feb 2024. Wallacegromit1 (talk) 19:46, 14 April 2024 (UTC)Reply

Search gives 20713 librarians in total [1]. Ordering by creation date and counting manually there seem to have been added 158 since 1. February 2024, which means 20555 was added before that date. The dates listed are modification dates btw. The creation date can be found in page information. Infrastruktur (talk) 22:17, 14 April 2024 (UTC)Reply
@Infrastruktur
Thank You So Much, this was really helpful.   Done Wallacegromit1 (talk) 04:42, 15 April 2024 (UTC)Reply
I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. author  TomT0m / talk page 17:52, 21 April 2024 (UTC)Reply

checking whether a first name is male of female edit

I wanna check whether a given name is male or female (or both) in my pywikibot-script. My first idea was to import all the male names / female names into my script once and check then against these data. But the simple query:

#title:get all male given names
SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q12308941.
  SERVICE wikibase:label { bd:serviceParam wikibase:language 'de' }
}
get all male given names

runs into timeout. I can limit the number of items returned, but then how to get all names? As an alternative I could try to query for a specific name:

#title:get a specific male given name
SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q12308941.
  ?item rdfs:label ?itemLabel .
   FILTER (LANG(?itemLabel) = "de"). 
   FILTER (?itemLabel = "Franz").
}
get a specific male given name

which also runs into timeout.

Acc. to https://stackoverflow.com/questions/38527828/how-to-query-wikidata-items-using-its-labels I tried:

SELECT DISTINCT ?item ?label
WHERE
{
  SERVICE wikibase:mwapi
  {
    bd:serviceParam wikibase:endpoint "www.wikidata.org";
                    wikibase:api "Generator";
                    mwapi:generator "search";
                    mwapi:gsrsearch "inlabel:Franz"@de;
                    mwapi:gsrlimit "max".
    ?item wikibase:apiOutputItem mwapi:title.
  }
  ?item rdfs:label ?label. FILTER( LANG(?label)="de" )

  # … at this point, you have matching ?item(s) 
  # and can further restrict or use them
  # as in any other SPARQL query

  # Example: the following restricts the matches
  # to college towns (Q1187811) only

  ?item wdt:P31 wd:Q12308941 .
  }
Try it!

which gives me a list of names similar to the given name "Franz". I could work with this for the price of having to query each given name separately., e.g. accessing only items with label="Franz" or getting all in one query?

Is there any smarter solution? best --Herzi Pinki (talk) 20:58, 14 April 2024 (UTC)Reply

Hello @Herzi Pinki,
Your first query doesn't time out if you turn off the label service. In order to get the labels, you can wrap your query in a WITH clause, and then use the label service in the main part of your query, like this. This query returns 48,861 hits in about 40 seconds.
What I cannot explain though is that this only works if the label service line is wrapped in an OPTIONAL clause, otherwise this approach fails too. Vincent Tep (talk) 19:42, 16 April 2024 (UTC)Reply
@Vincent Tep: a lot of not straightforward tricks. and it still times out for me. :-( thanks anyhow --Herzi Pinki (talk) 22:36, 16 April 2024 (UTC)Reply
I tried it multiple times and it timed out once too. I suggest you try again. If it doesn't work I can send you a .csv of the results. Vincent Tep (talk) 06:41, 17 April 2024 (UTC)Reply
I want to embed the query in a script to check gender against names, so a static .csv will not really help. best --Herzi Pinki (talk) 09:51, 17 April 2024 (UTC)Reply
your initial query works fine when you disable the label service. Maybe you can do this (retrieve only item IDs), and then make a separate call/query to get their labels? Vincent Tep (talk) 12:01, 17 April 2024 (UTC)Reply
splendid idea:
SELECT ?item ?itemLabel
WHERE {
  ?item rdfs:label ?itemLabel .
   FILTER (LANG(?itemLabel) = "de"). 
VALUES ?item {

wd:Q741
wd:Q923
wd:Q1557
wd:Q4642
wd:Q20164
wd:Q20790
wd:Q24223
wd:Q29884
wd:Q37080
wd:Q42771
}.
  SERVICE wikibase:label { bd:serviceParam wikibase:language 'de' }
}
Try it!
I cannot paste the full query here, as urls get too long. But it seems to work in 1000 junks, or even in 10000 junks Query for first 1000. thanks a lot, --Herzi Pinki (talk) 13:48, 17 April 2024 (UTC)Reply
@Herzi Pinki: In one query :
#title:get all male given names
SELECT ?item ?itemLabel
WHERE {
  {
      select * {
          ?item wdt:P31 wd:Q12308941.
      }
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language 'de' }
}
get all male given names
it works. The trick is to wrap the item retrieval into a subquery and call the query service outside. author  TomT0m / talk page 14:26, 17 April 2024 (UTC)Reply
even better. there is some room above for more names to search for (estimated another +20% before the query breaks). thanks a lot --Herzi Pinki (talk) 15:04, 17 April 2024 (UTC)Reply
Thanks @TomT0m, I had the same idea (pilfered from the Query Optimization page), but I wrapped my query in a WITH clause, whereas you didn't. Yours is shorter to write, more straightforward, runs twice as fast and seems to returns 4 more hits. Thank you! Vincent Tep (talk) 19:19, 17 April 2024 (UTC)Reply
I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. Herzi Pinki (talk) 15:11, 17 April 2024 (UTC)Reply

results where items are Q items edit

I'm working with the following query to find items related. How can I change it to only return rows with a Q item? Also, I'd like to filter out "described by source" type relations as well as negative ones like "opposite of." Thanks, Marc Mason

SELECT ?wdLabel ?ps_Label ?ps_ ?ps {
  VALUES (?item) {(wd:Q197)}
  ?item ?p ?statement .
  ?statement ?ps ?ps_ .
  ?wd wikibase:claim ?p.
  ?wd wikibase:statementProperty ?ps.
  OPTIONAL {   ?statement ?pq ?pq_ .   ?wdpq wikibase:qualifier ?pq .   }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } } 
ORDER BY ?wd ?statement ?ps_
Try it!

173.69.181.96 16:48, 15 April 2024 (UTC)Reply

Something like that ?
SELECT ?wdLabel ?ps_Label ?ps_ ?ps {
  VALUES (?item) {(wd:Q197)}
  ?item ?p ?statement .
  ?statement ?ps ?ps_ .
  ?wd wikibase:claim ?p;
      wikibase:statementProperty ?ps;
      wikibase:propertyType wikibase:WikibaseItem .
  
  OPTIONAL {   ?statement ?pq ?pq_ .   ?wdpq wikibase:qualifier ?pq .   }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } } 
ORDER BY ?wd ?statement ?ps_
Try it!
(I just used wikibase:propertyType which has the datatype of the property as value, here we want item datatype, wikibase:WikibaseItem author  TomT0m / talk page 10:56, 16 April 2024 (UTC)Reply

conncetions between basketball and hiphop edit

hi, I would like a query in which are explicated the possible connection between basketball and hiphop 87.0.83.82 14:20, 17 April 2024 (UTC)Reply

Concat the URL with a name and not with all URL edit

I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. author  TomT0m / talk page 17:50, 21 April 2024 (UTC)Reply

Hi! I've created this query that works perfectly. The only thing is that I would like the URL to appear under a title "This is a link" instead of the whole URL. I am using the code CONCAT but it doesn't work. Thanks! Leire

#defaultView:Map
#Fimmakers
SELECT DISTINCT ?filmmaker ?filmmakerLabel ?lugar ?coord ?birthdate ?year (CONCAT("[[", ?TabakaleraURL, "|This is a link]]") AS ?TabakaleraURL)
 
WHERE
{
       ?filmmaker wdt:P31 wd:Q5 .
       ?filmmaker wdt:P106 wd:Q2526255 . #zinemagilea
    ?filmmaker wdt:P19 ?lugar. 
  ?filmmaker wdt:P1344 wd:Q9081343.
  ?lugar wdt:P625 ?coord.
  
  ?filmmaker  wdt:P10069  ?tabakalera.
  OPTIONAL{?filmmaker wdt:P569 ?birthdate .} # P569 : Date de naissance
   BIND(year(?birthdate) as ?year)
  FILTER(?year > 1800)
  wd:P10069 wdt:P1630 ?formatterurl .
 BIND(IRI(REPLACE(?tabakalera, '^(.+)$', ?formatterurl)) AS ?TabakaleraURL).
 
 

       SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
 
}
Try it!

Lmerice (talk) 17:53, 18 April 2024 (UTC)Reply

@Lmerice: From a discussion on phabricator I learned that in any view different of the table view, if there is a variable ?var and a variable ?varLabel (var suffixed by Label), the "?varLabel" is used for the label of the links. So here is the query, just rewrote a bit but the only real change is the introduction of ("this is a link " as ?TabakaleraURLLabel) in the "select" clause :
#defaultView:Map
#Fimmakers
SELECT DISTINCT ?filmmaker ?filmmakerLabel ?lugar ?coord ?birthdate ?year ?TabakaleraURL ("this is a link " as ?TabakaleraURLLabel)
 
WHERE
{
  ?filmmaker wdt:P31 wd:Q5 ;
             wdt:P106 wd:Q2526255 ; #zinemagilea
             wdt:P19 ?lugar ;
             wdt:P1344 wd:Q9081343.
  ?lugar wdt:P625 ?coord.
 
  
  OPTIONAL{ ?filmmaker wdt:P569 ?birthdate . } # P569 : Date de naissance
  BIND(year(?birthdate) as ?year)
  FILTER(?year > 1800)
  
  ?filmmaker  wdt:P10069  ?tabakalera.
  wd:P10069 wdt:P1630 ?formatterurl .
  BIND(IRI(REPLACE(?tabakalera, '^(.+)$', ?formatterurl)) AS ?TabakaleraURL).

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
 
}
Try it!
author  TomT0m / talk page 09:35, 19 April 2024 (UTC)Reply
That was just what I needed. Thanks! Lmerice (talk) 16:36, 21 April 2024 (UTC)Reply

Query using all subproperties of parent (P8810) edit

Can I query for all parents of people living in a city like University of Oxford (Q34433) by using parent (P8810) in my query and get the information via subproperty of (P1647) without using child (P40), mother (P25), or father (P22) directly? ChristianKl21:47, 18 April 2024 (UTC)Reply

@ChristianKl:_Yes but not simple, plus there are no person living in Oxford University right now on Wikidata so I tried with another city insteaD :
select distinct ?personne ?personneLabel ?parent ?parentLabel {
  ?personne ?parent_prop_c ?parent .                 # get the data with subproperties, the actual predicate is a variable ?parent_prop_c
  
  ?parent_prop wdt:P1647* wd:P8810 .                 # subproperties of "parent" entities 
  ?parent_prop wikibase:directClaim ?parent_prop_c . # main value "direct claim" predicate for those entities in ?parent_prop_c
  
  ?personne wdt:P551 wd:Q12191 . hint:Prior hint:runFirst true . #get the persons who lives in the place (directly, not the places located in it)
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  
}
Try it!
author  TomT0m / talk page 09:13, 19 April 2024 (UTC)Reply
Oh, and I forgot, it does not get simpler if you want to get say, the grandfathers, as you can't use propertypaths with variables. Getting grandfathers or, say, all male ascendants can be done in two lines if you don't use subproperties in SPARQL :
This get significantly harder or even impossible with the subproperties. author  TomT0m / talk page 18:29, 21 April 2024 (UTC)Reply

Things both Japan and Russia edit

I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. author  TomT0m / talk page 17:51, 21 April 2024 (UTC)Reply

Hello, how to query things having wdt:P17==Q17 AND wdt:P17==Q159 ? Bouzinac💬✒️💛 15:29, 19 April 2024 (UTC)Reply

@Bouzinac: Like this :
select distinct ?item ?itemLabel {
  ?item wdt:P17 wd:Q17, wd:Q159 .
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
author  TomT0m / talk page 15:46, 19 April 2024 (UTC)Reply
https://w.wiki/9ouJ ce sont des choses pas forcément fausses, dans la mesure où y a différent territorial entre les deux pays Bouzinac💬✒️💛 15:59, 19 April 2024 (UTC)Reply
Point amusant : si tu t'amuses à requêter Japon+Brésil, tu te retrouves à Rome :) https://w.wiki/9ouS Bouzinac💬✒️💛 16:02, 19 April 2024 (UTC)Reply
Si tu veux t'amuser plus efficacement, j'ai mis un formulaire dessus grace à la fonctionnalité "template" : https://w.wiki/9ouw author  TomT0m / talk page 16:14, 19 April 2024 (UTC)Reply
If you would like to have also not present countryships: https://w.wiki/9qvi. --Infovarius (talk) 20:29, 22 April 2024 (UTC)Reply

Two or more statements on a certain property edit

I think that this discussion is resolved and can be archived. If you disagree, don't hesitate to replace this template with your comment. author  TomT0m / talk page 17:51, 21 April 2024 (UTC)Reply

How to request a list of all elements where there are two or more statements on a certain property (without deprecated statements)? For example, list of all elements where more than one Great Chinese Encyclopedia code or Den Store Danske ID Proeksad (talk) 16:18, 19 April 2024 (UTC)Reply

@Proeksad: There is a helper template for that : {{Multiple statements}}. Just {{sparql|query=select ?item { {{multiple statements|P10565|?item}} } }} gives :
select ?item { 
   {
     select distinct ?item {
       ?item p:P10565  ?itemP10565stmt1, ?itemP10565stmt2
                        filter(?itemP10565stmt1 != ?itemP10565stmt2) .
       ?itemP10565stmt1 a wikibase:BestRank .
              ?itemP10565stmt1 a wikibase:BestRank .
     } 
   } }
Try it!
You can combine with other criteria if you like. But Wikidata:Database reports/Constraint violations should do the same in this case. author  TomT0m / talk page 16:27, 19 April 2024 (UTC)Reply
Thank you! Proeksad (talk) 17:04, 19 April 2024 (UTC)Reply
How to request without deprecated statements? For example, Q1469#P4342. @TomT0m Proeksad (talk) 17:48, 19 April 2024 (UTC)Reply
@Proeksad I fixed the template, you can retry the query and now it will use only better ranked statements and ignore deprecated values. If you want all valid values you can try to set the valid parameter :
select ?item { 
   {
     select distinct ?item {
       ?item p:P10565  ?itemP10565stmt1, ?itemP10565stmt2
                        filter(?itemP10565stmt1 != ?itemP10565stmt2) .
       ?itemP10565stmt1 wikibase:rank ?itemP10565stmt1_rank 
              filter (?itemP10565stmt1_rank != wikibase:DeprecatedRank) .
              ?itemP10565stmt2 wikibase:rank ?itemP10565stmt2_rank 
              filter (?itemP10565stmt2_rank != wikibase:DeprecatedRank) .
     } 
   } }
Try it!
author  TomT0m / talk page 11:33, 20 April 2024 (UTC)Reply

Entities with articles in many languages but not English. edit

How can I find the Wikidata entities with the most wiki links which doesn't have an English page? (Or, more generally, the Wikidata entities with the most interwiki links which does not have a link in such-and-such a language.) Grendelkhan (talk) 03:57, 23 April 2024 (UTC)Reply

I don't have a direct solution, but I did identify some parts:
  • You can select the count of some query, as in the example "Number of humans in wikidata"
  • The current top topic is "largest lakes no enwiki", the solution at https://w.wiki/9kqs may help you with figuring out how to detect whether there is no English wikipedia. It looks like that is this codeblock:
optional {
:   ?article schema:about ?item ; 
:    schema:isPartOf <https://en.wikipedia.org/> .
:  }
:  filter(!bound(?article))
Try it!
  • I think for items linked to a wikipedia page, for each language you get one rdfs:label in the article, for the title of the article in that language. For example here for the United Nations item: https://w.wiki/9seF
I guess the total query would need to select the item ID and number of those labels, while excluding the ones which are not allowed using the filter. I tried combining it all, but my attempt seems to run forever even with just a few items: https://w.wiki/9seW M.alten.tue (talk) 13:18, 25 April 2024 (UTC)Reply

Find English labels only, but if no label in English allow any language edit

I'm basically getting all the data on a wikidata page, to use it for a tool. At the moment, I use this query to get that data:

SELECT ?item ?p_property ?p_propertyLabel ?statementLink ?simplevalue ?simplevalueLabel 
WHERE
{
  wd:Q1065 ?property ?statementLink . 
  ?statementLink ?simplevalueLink ?simplevalue .
  wd:Q1065 ?propdirect ?simplevalue.
  wd:Q1065 rdfs:label ?item.
  
  
  #find property label (thanks to tagishsimon)
  ?p_property wikibase:claim ?property .
  
  #find only properties & values with the right namespace
  FILTER(STRSTARTS(STR(?propdirect), STR(wdt:)))
  FILTER(STRSTARTS(STR(?property), STR(p:))) 
  FILTER(STRSTARTS(STR(?simplevalueLink),  STR(ps:)))
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } # Helps get the label in your language, if not, then en language

  #only get English language item names
  FILTER(LANGMATCHES(LANG(?item), "en"))
}
Try it!

Originally I didn't have that bit at the end, but I would get for example Chinese or French labels for my item and very rarely English. Now it obviously filters out anything not English, but I'm concerned that if an item has no label in English the query will return nothing. I could just check for this in my code, and fire off a second request without the filter and without asking for the item name, but I would prefer to keep the number of requests low.

Is there a way to filter on English labels only, unless no English label exists in which case any label will do? Or should I just do another request? M.alten.tue (talk) 12:59, 25 April 2024 (UTC)Reply

All riders of the Tour de France 2023 with sport number and ranking edit

To get all riders of the Tour the France 2023 is easy:

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P1344 wd:Q106203524.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!

Now I want also if exits the sport number (P1618) and the ranking (P1352) of the Tour de France 2023. Usually this two are qualifiers of the Tour de France 2023.

Can someone help me please? Strahtw (talk) 13:28, 25 April 2024 (UTC)Reply