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 2021/05.

Using wikibase:around and also return the aggregate count of statements for each returned item[edit]

Hello,

I am trying this query to return items in geographic radius and also count the number of statements for each return item. But I cant get it to work. How do you do this?

SELECT ?place ?location ?distance ?whatLabel ?placeLabel (COUNT (?statement) as ?statements) WHERE {
    SERVICE wikibase:around { 
      ?place wdt:P625 ?location.
      bd:serviceParam wikibase:center "Point(19.242296 57.955333)"^^geo:wktLiteral . 
      bd:serviceParam wikibase:radius "50" . 
      bd:serviceParam wikibase:distance ?distance .
    } 
    OPTIONAL {?place wdt:P31 ?what. }
    ?place ?p ?statement.
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "sv,en,[AUTO_LANGUAGE]". }
}

Try it!

--- Vitplister (talk) 04:06, 30 April 2021 (UTC)

Count of female artists with articles in the Portuguese Wikipedia[edit]

I was trying {{

SELECT (COUNT(*) AS ?count)
WHERE
{
       ?women wdt:P31 wd:Q5 .
       ?women wdt:P21 wd:Q6581072 .
       ?women wdt:P106/wdt:P279* wd:Q483501 . # artists
    ?article schema:about ?women .
    ?article schema:inLanguage "pt" .
      ?article schema:isPartOf <https://pt.wikipedia.org/>.
}

Try it!

but it times out, any ideas? Thanks. Mike Peel (talk) 11:07, 23 April 2021 (UTC)

SELECT (COUNT(DISTINCT ?woman) AS ?count)
WITH
{
  SELECT DISTINCT ?occ { ?occ wdt:P279* wd:Q483501 }
}
as %occ
WHERE
{
  INCLUDE %occ
  ?woman wdt:P106 ?occ ; ^ schema:about [ schema:isPartOf <https://pt.wikipedia.org/>] .
  hint:Prior hint:rangeSafe true .  
  ?woman wdt:P21 wd:Q6581072 ; wdt:P31 wd:Q5 .
}

Try it!

The above gives 18487 --- Jura 11:59, 23 April 2021 (UTC)

@Jura1: Thanks! Is there something that would work for male artists as well? If I change female (Q6581072) to male (Q6581097), the query times out again. Thanks. Mike Peel (talk) 14:14, 23 April 2021 (UTC)
@Mike Peel: I tried some, but didn't get beyond all (fe-)males with occupations in ptwiki. --- Jura 09:13, 24 April 2021 (UTC)

Grammatical featue[edit]

Hello,

I want a list of german substantives with their forms and the grammatical grammatical features of the forms. I changed a query I found in the Query examples [[1]] and with that query I get substantives and the forms but I dont get the grammatical feature of a form and this is a important information. --Hogü-456 (talk) 19:35, 23 April 2021 (UTC)

@Hogü-456: I don't have much of a clue about the lexeme data structure. However this tweak might be along the road to what you're after?
# Forms of nouns in German
# Based on a query by Uziel302
SELECT ?lexemeId ?lemma ?form ?word ?grammatikalisches_Geschlecht ?grammatikalisches_GeschlechtLabel ?gF ?gFLabel WHERE {
  ?lexemeId dct:language wd:Q188;
    wikibase:lexicalCategory wd:Q1084;
    wikibase:lemma ?lemma;
    ontolex:lexicalForm ?form.
  ?form ontolex:representation ?word.
  ?form wikibase:grammaticalFeature ?gF.
  OPTIONAL { ?lexemeId wdt:P5185 ?grammatikalisches_Geschlecht. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it! --Tagishsimon (talk) 23:02, 23 April 2021 (UTC)
@Tagishsimon: Hello Simon thank you for the query. What you created is what I have looked for and this is really helpful. --Hogü-456 (talk) 12:25, 24 April 2021 (UTC)

If recursive[edit]

Hi! Given this query

SELECT ?l ?iso
WHERE {
  ?w wdt:P50 wd:Q692 .
  ?w wdt:P407 ?l .
  OPTIONAL { ?l wdt:P219 ?iso }
}
ORDER BY ?iso

Try it!

some ?l have no ?iso; in these cases, I'd want to have a query saying: IF ?l has no ?iso, search if ?l has subclass of (P279), then if it has P279 value look into P279 and show me its value of ISO 639-2 code (P219) (if it has one) as ?iso2. Is it possible? Thanks, --Epìdosis 11:19, 29 April 2021 (UTC)

@Epìdosis: Here's one approach; seems to be pertinent only to Early Modern English (Q1472196) (other of the items that have an ?iso_ also have an ?iso2_ somewhere along the line, but your specification does not call for their display. I've also thrown a DISTINCT into the query, which you may or may not want.
SELECT distinct ?l ?iso
WHERE {
  ?w wdt:P50 wd:Q692 .
  ?w wdt:P407 ?l .
  OPTIONAL { ?l wdt:P219 ?iso_ }
  OPTIONAL { ?l wdt:P279*/wdt:P219 ?iso2_ }
  BIND(IF(!BOUND(?iso_),?iso2_,?iso_) as ?iso) 
}
ORDER BY ?iso
Try it! --Tagishsimon (talk) 12:28, 29 April 2021 (UTC)

Subway stations that aren't subway stations[edit]

Hello, I'd like to find any item that has any connecting line (P81) being rapid transit railway line (Q15079663) but hasn't yet the instance of (P31) = metro station (Q928830) --Bouzinac💬✒️💛 09:17, 30 April 2021 (UTC)

This, probably. You need to check your assumption that these should have Q928830 or a subclass thereof stamped on them. And it's less certain they need their railway station (Q55488) removing ... some, a few, will be a combined railway / light rail station.
In other news, how exactly does Glasgow subway line (Q106328343) differ from Glasgow Subway (Q506290)? They seem to be *exactly* the same thing.
SELECT DISTINCT ?item ?itemLabel ?line ?lineLabel
WHERE 
{
  ?item wdt:P81 ?line. 
  ?line wdt:P31/wdt:P279* wd:Q15079663. hint:Prior hint:gearing "forward".
  FILTER NOT EXISTS {?item wdt:P31/wdt:P279* wd:Q928830 . hint:Prior hint:gearing "forward".}
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it! --Tagishsimon (talk) 11:27, 30 April 2021 (UTC)
Hello Tagishsimon (talkcontribslogs), just perfect, thank you
Anyways, just check Q2546530#P361 and Q2546530#P81. A subway station should be part of a network on a certain subway line. It happens some subway networks can be one lined only, but network different from (P1889) line. --Bouzinac💬✒️💛 14:39, 30 April 2021 (UTC)
@Bouzinac: Strongly disagree. There is a single thing, calling for a single item carrying a plurality of P31 values. The subway - Glasgow Subway (Q506290) - is the line, per its description "underground metro line in Glasgow, Scotland". There is no good reason to have a discrete items carring the rapid transit railway line (Q15079663) and circle route (Q145179) P31 values, when these can & should be on the Glasgow Subway (Q506290). A station can be part of (P361), and have a connecting line (P81) both pointing to the same item; this being the case, can you suggest any other reason which validates the duplicate you've created? --Tagishsimon (talk) 15:14, 30 April 2021 (UTC)
I'm sorry but they are two different concept. A subway line and a subway network. What happens if Glasgow builds a second subway line? Merkland Street subway station (Q6819640)'s part of (P361) cannot nowadays be Glasgow Subway (Q506290) but connecting line (P81)=Glasgow subway line (Q106328343) still true.
Another example in a very different field : Château Perrier (Q28003614) and Château Perrier (Q22979594). Single concepts should be having their own item. In the Glasgow case, they happen to be "almost" the same but not and connecting line (P81) // part of (P361) be having their correct item --Bouzinac💬✒️💛 15:38, 30 April 2021 (UTC)
@Bouzinac: No. A museum and the building it is housed in are indeed distict things: the museum can be uplifted and taken to a different building. In Glasgow there is a single metro line, Glasgow subway. Call it what you like, the line, the subway, the network are the same thing. In this circustance, a single item suffices, and splitting the item serves no useful purpose. If a second line is ever built, we can revisit the question - but this is not very likely. To repeat, a "network" comprising a single line is the same thing as the single line. Merkland Street subway station (Q6819640) is part of (P361) and has connecting line (P81) Glasgow Subway (Q506290) --Tagishsimon (talk) 16:01, 30 April 2021 (UTC)
Disagree again, sorry. Look at the definition of rapid transit (Q5503) : it is a network and possibly can be running buses, subways, suburban trains, not only subway lines per se. --Bouzinac💬✒️💛 16:05, 30 April 2021 (UTC)

Need a query to get "health specialty P115" from a medical condition name "arterial hypertension (Q41861)"[edit]

Need a query to get "health specialty P115" from a medical condition name "arterial hypertension (Q41861)"  – The preceding unsigned comment was added by 2405:201:c009:a9ce:918a:fb8a:d0b:44c2 (talk • contribs) at 14:50, 30 April 2021‎ (UTC).

home venue (P115) is not "health speciality", maybe you mean health specialty (P1995).
SELECT ?p1995Label WHERE { wd:Q41861 wdt:P1995 ?p1995 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
--Larske (talk) 15:36, 2 May 2021 (UTC)

Black History of the Pacific Northwest[edit]

Hello all, I would like to format a query that helps me pinpoint prominent Black historical figures who have either lived in or were born in Oregon and Washington.

I have had some difficulty pulling a query that accesses all the prominent figures I know exist.

This is what I have and I change Washington to Oregon to find those figures... while this is what I have so far- would someone take a look and see if there are ways I could do better.

  1. African Americans who resided in Washington, by occupation and birthdate

SELECT DISTINCT ?item ?dob ?occupationLabel ?itemLabel WHERE {

 { SELECT ?item WHERE {  } }
 ?item (wdt:P19/(wdt:P131*)) wd:Q1223;
   wdt:P31 wd:Q5;
   wdt:P172 wd:Q49085;
   wdt:P106 ?occupation.
 OPTIONAL { ?item wdt:P569 ?dob. }
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr,it". }
 OPTIONAL {  }

} ORDER BY (?dob)


Much appreciation --Libcuri (talk) 22:23, 2 May 2021 (UTC)

Help with: currently inhabited urban areas and names of their administrations[edit]

Hi. I currently have the following query:

SELECT DISTINCT ?location ?place_eng ?admin_eng ?country_eng WHERE {
  {
    ?settlement wdt:P279 wd:Q486972.
    ?place wdt:P31 ?settlement.
    ?place wdt:P1082 ?population.
    FILTER(?population > 400)
    OPTIONAL { ?place wdt:P625 ?location. }
    ?place wdt:P131 ?admin.
    ?place wdt:P17 ?country.
  }

  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?place rdfs:label ?place_eng.
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?admin rdfs:label ?admin_eng.
  }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "en".
    ?country rdfs:label ?country_eng.
  }
}

Try it!

(I recommend commenting out the labels if you want to try the query.)

In a nutshell, what I'm trying to get is:

  • Place names for human settlements (Q486972) with more than 400 population
  • Their coordinates, if available
  • Their administrative unit
  • Their country

With English labels for each. The problem is that this query is too slow, and it either times out or freezes the browser tab where I have the the query service running. Without the labels, I can run the query and get many results, but as soon as I try to get the labels for them, it times out (even if I just try with the labels for ?place).

On top of that, I also need labels in a few other languages (Spanish, Italian, French, German, Portuguese), and I'm aware of how much slower the label service can make a query, so this sounds like a daunting task for a newbie like me. But if I can make the query work for one language, I can just repeat it for each other language and join the data later.

Can you help me optimise it or explain how I could break it up into multiple queries whose results I can easily join together?

--2A01:C23:6C27:1A00:9483:7D99:7061:1A6E 08:54, 3 May 2021 (UTC)

Querying a list of members of German Bundestag results in timeout exception[edit]

Hello! I am struggling with constant timeout exceptions. I wonder what's wrong with my query... What I'm trying to achieve: I need to query all members of German Bundestag (former + actual) and several properties of them (age, gender, party etc.). I realised that I need to split the query in batches in order to avoid time outs. So I split by year of birth. This worked a couple of times (no time outs), however since 2 weeks or so I'm running into timeout exceptions even when I make the batches really small (e.g. date of birth between 1950 and 1960).

Please find my query below:

SELECT DISTINCT ?mdb ?mdbLabel ?faction ?abstract ?dateOfBirth ?dateOfDeath ?abgeordnetenwatchID ?thumbnailURI ?party ?gender ?websiteURI ?instagram ?facebook ?twitter WHERE {

 ?mdb wdt:P31 wd:Q5.
 ?mdb p:P39 ?humansWithPositionHeld.
 ?humansWithPositionHeld ps:P39 wd:Q1939555.
 OPTIONAL { ?humansWithPositionHeld pq:P4100 ?faction. }
 ?mdb rdfs:label ?mdbString.
 ?mdb schema:description ?abstract.
 FILTER(lang(?abstract) = "de").
 OPTIONAL {?mdb wdt:P569 ?dateOfBirth. }
 OPTIONAL {?mdb wdt:P570 ?dateOfDeath. }
 OPTIONAL {?mdb wdt:P5355 ?abgeordnetenwatchID. }
 OPTIONAL {
   ?mdb wdt:P18 ?image_.
   BIND(REPLACE(wikibase:decodeUri(STR(?image_)), "http://commons.wikimedia.org/wiki/Special:FilePath/", "") AS ?imageFileName_)
   BIND(REPLACE(?imageFileName_, " ", "_") AS ?imageFileNameSafe_)
   BIND(MD5(?imageFileNameSafe_) AS ?imageFileNameHash_)
   BIND(CONCAT("https://upload.wikimedia.org/wikipedia/commons/thumb/", SUBSTR(?imageFileNameHash_, 1 , 1 ), "/", SUBSTR(?imageFileNameHash_, 1 , 2 ), "/", ?imageFileNameSafe_, "/300px-", ?imageFileNameSafe_) AS ?thumbnailURI)
 }
 OPTIONAL {?mdb wdt:P102 ?party. }
 OPTIONAL {
   ?mdb wdt:P21 ?gender_.
   ?gender_ rdfs:label ?genderLabel_. 
   FILTER(lang(?genderLabel_) = "en"). 
 }
 BIND(IF(BOUND(?genderLabel_ ), ?genderLabel_, "unknown") AS ?gender).
 OPTIONAL {?mdb wdt:P856 ?websiteURI. }
 OPTIONAL {?mdb wdt:P2003 ?instagram. }
 OPTIONAL {?mdb wdt:P2013 ?facebook. }
 OPTIONAL {?mdb wdt:P2002 ?twitter. }
 FILTER('1950-01-01'^^xsd:dateTime <= ?dateOfBirth && ?dateOfBirth < '1960-01-01'^^xsd:dateTime).
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de". }

}

Something like:
SELECT DISTINCT ?mdb ?mdbLabel ?faction ?abstract ?dateOfBirth ?dateOfDeath ?abgeordnetenwatchID ?thumbnailURI ?party ?gender ?websiteURI ?instagram ?facebook ?twitter WITH {
  SELECT ?mdb ?humansWithPositionHeld WHERE {
 ?mdb wdt:P31 wd:Q5.
 ?mdb p:P39 ?humansWithPositionHeld.
 ?humansWithPositionHeld ps:P39 wd:Q1939555.
} } as %i
WHERE
{
  INCLUDE %i
 OPTIONAL { ?humansWithPositionHeld pq:P4100 ?faction. }
 OPTIONAL {?mdb wdt:P569 ?dateOfBirth. }
 OPTIONAL {?mdb wdt:P570 ?dateOfDeath. }
 OPTIONAL {?mdb wdt:P5355 ?abgeordnetenwatchID. }
 OPTIONAL {
   ?mdb wdt:P18 ?image_.
   BIND(REPLACE(wikibase:decodeUri(STR(?image_)), "http://commons.wikimedia.org/wiki/Special:FilePath/", "") AS ?imageFileName_)
   BIND(REPLACE(?imageFileName_, " ", "_") AS ?imageFileNameSafe_)
   BIND(MD5(?imageFileNameSafe_) AS ?imageFileNameHash_)
   BIND(CONCAT("https://upload.wikimedia.org/wikipedia/commons/thumb/", SUBSTR(?imageFileNameHash_, 1 , 1 ), "/", SUBSTR(?imageFileNameHash_, 1 , 2 ), "/", ?imageFileNameSafe_, "/300px-", ?imageFileNameSafe_) AS ?thumbnailURI)
 }
 OPTIONAL {?mdb wdt:P102 ?party. }
 OPTIONAL {
   ?mdb wdt:P21 ?gender_.
   ?gender_ rdfs:label ?genderLabel_. 
   FILTER(lang(?genderLabel_) = "en"). 
 }
 BIND(IF(BOUND(?genderLabel_ ), ?genderLabel_, "unknown") AS ?gender).
 OPTIONAL {?mdb wdt:P856 ?websiteURI. }
 OPTIONAL {?mdb wdt:P2003 ?instagram. }
 OPTIONAL {?mdb wdt:P2013 ?facebook. }
 OPTIONAL {?mdb wdt:P2002 ?twitter. }
# FILTER('1950-01-01'^^xsd:dateTime <= ?dateOfBirth && ?dateOfBirth < '1960-01-01'^^xsd:dateTime).
 SERVICE wikibase:label { bd:serviceParam wikibase:language "de". 
                         ?mdb rdfs:label ?mdbLabel.
                         ?mdb schema:description ?abstract. }
}
Try it! --Tagishsimon (talk) 15:47, 6 May 2021 (UTC)