Shortcuts: WD:RAQ, w.wiki/LX

Wikidata:Request a query

From Wikidata
Jump to navigation Jump to search

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.

Map of all places related to persons with layers by property of the place[edit]

I tried to get a map like Birth places of German poets combined with other places like place of death (P20), residence (P551), work location (P937) as separate layers. I was able to bring all this places on the map, but I didn't figure out how to divide them into separate layers with different colours.

#defaultView:Map{"hide": ["?coord"]}
SELECT DISTINCT ?subj ?subjLabel ?dob ?birthPlace ?birthPlaceLabel ?dod ?deathPlace ?deathPlaceLabel ?coord WHERE {
  ?subj ((wdt:P19|wdt:P20|wdt:P551|wdt:P1321|wdt:P937)/(wdt:P131*)) wd:Q11943.
  OPTIONAL {
    ?subj wdt:P19 ?birthPlace.
    ?birthPlace wdt:P625 ?coord.
  }
  OPTIONAL {
    ?subj wdt:P20 ?deathPlace.
    ?deathPlace wdt:P625 ?coord.
  }
  OPTIONAL { ?subj wdt:P569 ?dob. }
  OPTIONAL { ?subj wdt:P570 ?dod. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!

Any help is appreciated! Rerumscriptor (talk) 12:58, 5 April 2024 (UTC)[reply]

Property paths can be nice to express something in a more terse way, but sometimes you just have to write the query out in a more basic form, e.g.:
SELECT DISTINCT ?subj ?subjLabel ?dob ?dod ?place ?placeLabel ?layer ?coord
WITH {
  SELECT DISTINCT ?place ?coord WHERE {
    ?place wdt:P131* wd:Q11943 .
    ?place wdt:P625 ?coord .
  }
} as %place # re-use results
WHERE {
  {
    INCLUDE %place
    ?subj wdt:P19 ?place .
    bind("birth place" as ?layer)
    OPTIONAL { ?subj wdt:P569 ?dob. filter(!wikibase:isSomeValue(?dob)) }
  } UNION {
    INCLUDE %place
    ?subj wdt:P20 ?place.
    bind("death place" as ?layer)
    OPTIONAL { ?subj wdt:P570 ?dod. filter(!wikibase:isSomeValue(?dod))  }
  } UNION {
    INCLUDE %place
    ?subj wdt:P551 ?place.
    bind("residence" as ?layer)
  } UNION {
    INCLUDE %place
    ?subj wdt:P1321 ?place.
    bind("place of origin" as ?layer)
  } UNION {
    INCLUDE %place
    ?subj wdt:P937 ?place.
    bind("work location" as ?layer)
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
Infrastruktur (talk) 14:39, 5 April 2024 (UTC)[reply]
Thank you, @Infrastruktur, that helped me a lot! I adjusted it a bit, so the map shows places related to people related to Canton of Zürich (Q11943). My result is now:
#defaultView:Map{"hide": ["?coord"]}
SELECT DISTINCT ?subj ?subjLabel ?place ?placeLabel ?layer ?coord 
WITH {
  SELECT DISTINCT ?subj WHERE {
  ?subj ((wdt:P19|wdt:P551|wdt:P20|wdt:P1321|wdt:P937)/(wdt:P131*)) wd:Q11943.
  }
} as %person # re-use results
WHERE {
  {
    INCLUDE %person
    ?subj  wdt:P19 ?place.
    ?place wdt:P625 ?coord.
    BIND("Geburtsort" AS ?layer)
  } UNION {
    INCLUDE %person
    ?subj wdt:P20 ?place.
    ?place wdt:P625 ?coord.
    BIND("Sterbeort" AS ?layer)
  } UNION {
    INCLUDE %person
    ?subj wdt:P551 ?place.
    ?place wdt:P625 ?coord.
    BIND("Wohnsitz" AS ?layer)
  } UNION {
    INCLUDE %person
    ?subj wdt:P937 ?place.
    ?place wdt:P625 ?coord.
    BIND("Wirkungsort" AS ?layer)
  } UNION {
    INCLUDE %person
    ?subj wdt:P1321 ?place.
    ?place wdt:P625 ?coord.
    BIND("Bürgerort" AS ?layer)
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
Rerumscriptor (talk) 13:27, 6 April 2024 (UTC)[reply]

Distance query not finding itself[edit]

Can anyone explain why this query returns itself for Dover Castle, but not for Hack Green bunker, when both have coordinate location (P625) the same? I'm completely baffled.

SELECT DISTINCT ?item ?itemLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en-GB,en,fr,de,es,pt,pl,nl,cs". }
  {
    SELECT DISTINCT ?item
                          WHERE {
#      hint:Query hint:optimizer "None"
wd:Q5637175 wdt:P625 ?targetLoc. # hack green bunker
#wd:Q950970 wdt:P625 ?targetLoc. # dover castle
SERVICE wikibase:around{
  ?item wdt:P625 ?location.
  bd:serviceParam wikibase:center ?targetLoc.
  bd:serviceParam wikibase:radius "0.1".
  bd:serviceParam wikibase:distance ?dist.
}
}
}
}
Try it!

Vicarage (talk) 16:13, 6 April 2024 (UTC)[reply]

@Vicarage: Makes no sense to me. Increasing the radius to 0.4km, the Hack Green query finds Hack House Farmhouse (Q62130029) ... putting that as ?targetLoc finds Hack Green Bunker, so we know its data is in the triplestore ... and finds itself, so we know wikibase:around finds items that have the coord of the wikibase:center (and the Dover Castle query showed us that). --Tagishsimon (talk) 19:54, 6 April 2024 (UTC)[reply]
The guy who wrote the code for that service left the WMF in 2019. You can try asking on Wikidata:Report_a_technical_problem if someone can write a Phabricator bugreport for you. Include the information you provided here. It this bug has existed for 8 years without anyone finding it, it must be something rare, so good thing it can be reproduced. Infrastruktur (talk) 21:39, 6 April 2024 (UTC)[reply]
I've done that: https://www.wikidata.org/wiki/Wikidata:Report_a_technical_problem#WDQS_wikibase:around_issue --Tagishsimon (talk) 21:52, 6 April 2024 (UTC)[reply]

Film directors[edit]

Could someone make a query of Norwegian film directors who died in 1953 or before? Trade (talk) 07:11, 7 April 2024 (UTC)[reply]

@Trade:
SELECT ?item ?itemLabel (YEAR(?dod) as ?year) WHERE 
{ 
  ?item wdt:P106 wd:Q2526255 .
  ?item wdt:P27 wd:Q20 . 
  ?item wdt:P570 ?dod . hint:Prior hint:rangeSafe true.
  FILTER("1953-12-31"^^xsd:dateTime >= ?dod) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],nn,en". } 
}
Try it!
--Tagishsimon (talk) 12:44, 7 April 2024 (UTC)[reply]

Extract qualifiers not working for me :([edit]

I saw the following SPARQL query successfully requesting Publication date (P577) qualifiers (WDQS) :

SELECT ?title ?item ?date ?place ?placeLabel WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  ?item wdt:P161 wd:Q38111.
  ?item wdt:P1476 ?title.
  # Get qualifiers
  ?item p:P577 ?statement. 
  ?statement ps:P577 ?date. 
  ?statement pq:P291 ?place.
}
Try it!

I replicated this exact (?) grammar to request Pronunciation (P:P443) qualifiers, but the following fails (WDQS) so I keep it in an OPTIONAL block :

SELECT ?id ?idLabel ?audio ?audioLabel ?audioFile ?audioLangLabel ?speakerLabel
WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  ?id wdt:P443 ?audio.
  FILTER(CONTAINS(STR(?audio), "LL-Q117707514")) # occitan whistled -Q117707514
  # ?id wdt:P625 ?coord . # geocoordinates
  OPTIONAL {  
    ?id p:P443 ?audioStatement .
    ?audioStatement ps:P443 ?audioFile.
    ?audioStatement pq:P407 ?audioLang.
    ?audioStatement pq:P10894 ?speaker.
    }
}
Try it!

Exemple of item with that exact data I look for : Laruns (Q374876#P443 with qualifiers P:P407 = Occitan sifflé d'Aas (Q117707514) and P:P10894 = Philippe Biu (Q120142380) )

Any clue ? Yug (talk) 08:39, 8 April 2024 (UTC)[reply]

User:VIGNERON pointed out a typo p:443 -> p:P443. So I fixed it above. Thank you VIGNERON for your eagle eyes ! Yug (talk) 08:52, 8 April 2024 (UTC)[reply]
Alternative syntaxe :
SELECT ?id ?idLabel ?audioFile ?audioLangLabel ?speakerLabel
WHERE {
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  ?id p:P443 ?audioStatement .
  ?audioStatement ps:P443 ?audioFile.
  FILTER(CONTAINS(STR(?audioFile), "LL-Q117707514")) # occitan whistled -Q117707514
  # ?id wdt:P625 ?coord . # geocoordinates
  ?audioStatement pq:P407 ?audioLang.
  ?audioStatement pq:P10894 ?speaker.
}
Try it!
Credit: VIGNERON. Yug (talk) 09:04, 8 April 2024 (UTC)[reply]

African Women[edit]

I want to kindly request query for list of items under this categories; women in Africa, Maternal health in Africa, women’s marches in Africa, African women in business, International Women’s Day in Africa around the world, women’s rights in Africa, and feminism in Africa. Thanks Bukky658 (talk) 12:17, 8 April 2024 (UTC)[reply]

@Bukky658: This is not a very good specification. It is unlikely to be answered. At one end, women in Africa -> probably a vast number of. At the other end, women’s rights in Africa, and feminism in Africa -> would need to know what sort of thinsg you actually mean. On this desk, you need to be fairly precise about your ask. Broad waving of arms does not cut it. --Tagishsimon (talk) 15:17, 9 April 2024 (UTC)[reply]
Pardon my broad ask, Specifically I would like to get items for each of the category for Women in Tanzania, Nigeria, & Ghana. I see that this categoies exist on Wikipedia and was wondering if I could get a query result for items in this categories with primary focus on the 3 mentioned countries. Bukky658 (talk) 15:24, 9 April 2024 (UTC)[reply]

Islands of the Baltic Sea[edit]

Hi. I kindly request a query about islands of the Baltic Sea, with a minimal area of 10 square kilometers, with an article at enwiki and no article at hewiki. Thanks. YoavR (talk) 13:41, 9 April 2024 (UTC)[reply]

@YoavR:
SELECT ?item ?itemLabel ?area ?unitLabel ?coord ?article WHERE {
  ?item wdt:P31 wd:Q23442 .                       # island
  ?item wdt:P206 wd:Q545 .                        # in Baltic Sea
  ?item p:P2046 ?stat .                           # area statement
  ?stat psn:P2046/wikibase:quantityAmount ?area . # normalised area
  ?stat psn:P2046/wikibase:quantityUnit ?unit .   # unit
  filter(?area >= 10000000)                       # more than or equal to 10 sq kmm
  OPTIONAL {?item wdt:P625 ?coord . }             # coordinate
  
  ?article schema:about ?item ;                   # article at EN wiki
           schema:isPartOf <https://en.wikipedia.org/> .
           
  filter not exists {?article1 schema:about ?item ; 
  schema:isPartOf <https://he.wikipedia.org/> .}  # no article at HE wiki

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
}
Try it!
--Tagishsimon (talk) 15:12, 9 April 2024 (UTC)[reply]
@YoavR: But, if You want some more, try including parts of Baltic sea :) (/wdt:P361*)
SELECT * {
  {
    SELECT DISTINCT * {
      ?item wdt:P31 wd:Q23442;
            wdt:P206/wdt:P361* wd:Q545;
            wdt:P2046 ?area.
      FILTER(?area > 10)
      ?en_sitelink schema:about ?item ;
                schema:isPartOf <https://en.wikipedia.org/> .
      MINUS {?he_sitelink schema:about ?item ;
                             schema:isPartOf <https://he.wikipedia.org/> .}
    }
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" .
    ?item rdfs:label ?label . ?item schema:description ?description
  }
}
ORDER BY DESC(?area)
Try it!
, regards Piastu (talk) 15:26, 9 April 2024 (UTC)[reply]

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]

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]

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]

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]

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]

Things both Japan and Russia[edit]

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]

Two or more statements on a certain property[edit]

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) .
     } 
   } }
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]