Shortcuts: WD:RAQ, w.wiki/LX

Wikidata:Request a query

From Wikidata
(Redirected from Wikidata:RAQ)
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 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 2024/08.

[edit]

Does anyone know why this query does not sort the results by the amount of interwiki links each article has? (the top item on this list says that the CNT=585 while in reality it is 38)

SELECT DISTINCT ?movie ?movieLabel (COUNT(?sitelink) AS ?cnt) 
WHERE {
  ?movie wdt:P31 wd:Q11424. # item is a film
  ?movie wdt:P577 ?release_date. # item has a publication date (release date)
  FILTER (?release_date >= "1980-01-01T00:00:00Z"^^xsd:dateTime && ?release_date < "1990-01-01T00:00:00Z"^^xsd:dateTime) # filter by date range
  ?sitelink schema:about ?movie.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
  MINUS {
    _:article schema:about ?movie; schema:isPartOf <https://he.wikipedia.org/>. # exclude items with an article on hewiki
  }
}
GROUP BY ?movie ?movieLabel 
ORDER BY DESC(?cnt) # order by the count of sitelinks in descending order
Try it!

WikiJunkie (talk) 13:49, 20 August 2024 (UTC)[reply]

You can COUNT(DISTINCT ?sitelink) or instead have ?movie wikibase:sitelinks ?cnt. Flipping Switches (talk) 23:12, 20 August 2024 (UTC)[reply]

All items of video games that don't have articles on the Hebrew Wikipedia

[edit]

I tried to create the code by myself but haven't succeeded so far. WikiJunkie (talk) 20:41, 6 August 2024 (UTC)[reply]

@WikiJunkie: here is a simple query for that :
SELECT ?q WHERE {
  ?q wdt:P31/wdt:P279* wd:Q7889 .
  MINUS { ?page schema:about ?q ; schema:isPartOf <https://he.wikipedia.org/> }
}
Try it!
There is 113374 results, you probably want to filter them.
Cheers, VIGNERON (talk) 16:49, 11 August 2024 (UTC)[reply]

Query timeout

[edit]

My Sandbox stopped working with the following error message: any hint?

The following SPARQL query timed out or returned no result:

SELECT ?grouping (SAMPLE(?_higher_grouping) as ?higher_grouping) (COUNT(DISTINCT ?entity) as ?count) WHERE {
  ?entity wdt:P31 wd:Q3305213; wdt:P195 ?collection. ?collection wdt:P17 wd:Q1284 .
  ?entity wdt:P195 ?grouping .
  OPTIONAL { ?grouping wdt:P17/wdt:P298 ?_higher_grouping }.
} GROUP BY ?grouping ?higher_grouping
HAVING (?count >= 6)
ORDER BY DESC(?count)
LIMIT 1000 Pietro (talk) 12:53, 7 August 2024 (UTC)[reply]
SELECT ?grouping (SAMPLE(?_higher_grouping) as ?higher_grouping) (COUNT(DISTINCT ?entity) as ?count) WHERE {
  ?entity wdt:P31 wd:Q3305213; wdt:P195 ?collection. ?collection wdt:P131* wd:Q1284 .
#   ?collection wdt:P17 wd:Q1284 .  # This gives zero results: region instead of country
  ?entity wdt:P195 ?grouping .
  OPTIONAL { ?grouping wdt:P17/wdt:P298 ?_higher_grouping }.
} GROUP BY ?grouping ?higher_grouping
HAVING (?count >= 6)
ORDER BY DESC(?count)
LIMIT 1000
Try it!
Flipping Switches (talk) 19:06, 7 August 2024 (UTC)[reply]
Thanks! Pietro (talk) 08:14, 8 August 2024 (UTC)[reply]

Searching for items that exist on multiple editions of Wikipedia

[edit]

I am searching for instance/sub-classes of LGBT+ identity (Q115870510) that have 3 or more non-English Wikipedia site-links. I attempted a query here, however it times-out. I will commit to improving documentation so that it is easier for others trying similar queries https://w.wiki/AszG

SELECT ?item ?itemLabel ?linkcount WHERE {  
?item wdt:P31/wdt:P279* wd:Q115870510 .
?item wikibase:sitelinks ?linkcount .

  FILTER (?linkcount >= 10) . 
  FILTER NOT EXISTS {
    ?article schema:about ?item 
    ?article schema:inLanguage "en" .
    ?article schema:isPartOf <https://en.wikipedia.org/>
  }
 
SERVICE wikibase:label { bd:serviceParam wikibase:language " 
 [AUTO_LANGUAGE],en". }
}

ORDER BY DESC(?linkcount)
Try it!

Shushugah (talk) 11:19, 10 August 2024 (UTC)[reply]

SELECT ?item ?itemLabel ?linkcount WHERE {
  # instance of LGBT  
  ?item wdt:P31/wdt:P279* wd:Q115870510 ;
        wikibase:sitelinks ?linkcount .
  FILTER(?linkcount > 2)
  MINUS {
    ?article schema:about ?item ;
             schema:isPartOf <https://en.wikipedia.org/>
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
ORDER BY DESC(?linkcount)
Try it!
Flipping Switches (talk) 13:34, 10 August 2024 (UTC)[reply]
Each FILTER and MINUS alone gives results, but they don't intersect. Flipping Switches (talk) 09:00, 17 August 2024 (UTC)[reply]

Lexemes with mismatching reading patterns

[edit]

I need a list of lexemes that have forms with diverging values for sinogram reading pattern (P5244) in order to find lexemes that should be split. Thank you 🙏 – Shisma (talk) 07:48, 14 August 2024 (UTC)[reply]

@Shisma: could you give a bit more context? I had a quick look at lexeme's forms using sinogram reading pattern (P5244), like Lexeme:L8441#F1 but I'm not sure where I should look for a divergence? Do you mean when there is two values like on Lexeme:L640276#F1 and Lexeme:L640276#F1? (which are currently the only two forms in that case see https://w.wiki/Ayxi). Cheers, VIGNERON (talk) 16:30, 22 August 2024 (UTC)[reply]
@VIGNERON:. Sorry. I should have provided an example. I meant a situation like this. One Lexeme, multiple forms with different reading patterns. – Shisma (talk) 17:32, 22 August 2024 (UTC)[reply]
@Shisma: no problem, so you mean like this:
SELECT * WHERE {
  ?l ontolex:lexicalForm ?f1 ;
     ontolex:lexicalForm ?f2 .
  ?f1 wdt:P5244 ?value1 .
  ?f2 wdt:P5244 ?value2 .
  FILTER ( ?value1 != ?value2 )
}
Try it!
1234 results right now (I didn't de-duplicate lexemes as you may want to know that information, here is the same query with unique lexemes : https://w.wiki/AzBv and 322 results). Cheers, VIGNERON (talk) 06:34, 23 August 2024 (UTC)[reply]

List of countries and regions which were at the 2024 Olympics, by population

[edit]

Hi all. Is there any way we can generate a list of those regions and nations which took part in this year's Olympic games, or a list of those which have a National Olympic Committee (Q183288)? Listed by population, please. Cheers! Llywelyn2000 (talk) 09:49, 15 August 2024 (UTC)[reply]

German Bundesländer (Q1221156) and their Cities (Q515)

[edit]

I need a list of german cities sorted by Bundesland (state, Q1221156). The result of my query is only 20 items. Is there a better way to query cities by bundesland?

SELECT ?state ?stateLabel ?city ?cityLabel WHERE {
  ?state wdt:P31 wd:Q1221156;
    wdt:P150 ?city.
  ?city (wdt:P31/(wdt:P279*)) wd:Q515.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY (?stateLabel) (?cityLabel)
Try it!

Marlons Friends (talk) 10:51, 20 August 2024 (UTC)[reply]

I see 151 result. --Infovarius (talk) 19:43, 20 August 2024 (UTC)[reply]
@Marlons Friends, Infovarius: I also see 151 results with a lot of duplication, and 44 results with DISTINCT. Both seems low.
Instead of taking the states and then looking for city in it, I tried the symmetric approach: taking the cities and looking for the states, it gives a bit more results (81 right now) :
SELECT DISTINCT ?city ?cityLabel ?land ?landLabel WHERE {
  ?city wdt:P31/wdt:P279* wd:Q515 ;
        wdt:P17 wd:Q183 ;
        wdt:P131 ?land .
  ?land wdt:P31 wd:Q1221156 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
Try it!
I found that some cities are not directly indicated as in the Land but in something itself in the Land. The query is then bigger, but doable on the QLever endpoint like this : https://qlever.cs.uni-freiburg.de/wikidata/a2MhLM and it gives a lot more results (you need to download them to see them all unlike on the Wikidata Query Service).
PS: also, do you want only the cities and all communes (Gemeinde). Cheers, VIGNERON (talk) 07:03, 23 August 2024 (UTC)[reply]
@ Infovarius: Thank you. it seems that most cities in wikidata simply are not connected to "Bundesländer"?
@ VIGERON: Thank you, that is an incredible insight. I did not know qlever. I have been testing this https://qlever.cs.uni-freiburg.de/wikidata/6QLofu#csv and got more than 100k results. It seems I got more or less all populated places in Germany with their Bundesland and coordinates. That was what I needed. Are there more places or would it even be smarter to query OSM? Marlons Friends (talk) 13:51, 23 August 2024 (UTC)[reply]

List of items "described by source" starting with a specific letter in the qualifier "section, verse, paragraph, or clause"

[edit]

Hi! How can I create a list of items that are described by a source (P1343), for example Q867541, that statement has the qualifier section, verse, paragraph, or clause (P958), that qualifier starts with, let's say, letter "M", and get those items sorted in alphabetical order (sorted by P958 value), getting the qualifiers volume (P478) and page (P304) too? Thanks in advance. strakhov (talk) 17:57, 21 August 2024 (UTC)[reply]

@Strakhov: hmm, are you sure of what you asked? Apparently, section, verse, paragraph, or clause (P958) is almost never used for Encyclopædia Britannica 11th edition (Q867541). This query only give one result:
SELECT ?q ?section WHERE {
  ?q p:P1343 ?node .
  ?node ps:P1343 wd:Q867541 ;
        pq:P958 ?section .
  FILTER ( STRSTARTS(?section, "M" ) )
}
Try it!
Cheers, VIGNERON (talk) 16:25, 22 August 2024 (UTC)[reply]
Thanks a lot, VIGNERON. It was a random encyclopedia item. Actually I had in mind a few other reference works. strakhov (talk) 16:30, 22 August 2024 (UTC)[reply]
@Strakhov: ah ok, here is the same query but for Brockhaus Enzyklopädie (19 ed.) (Q17377889) and with the optional qualifier.
SELECT ?q ?section ?volume ?page WHERE {
  ?q p:P1343 ?node .
  ?node ps:P1343 wd:Q17377889 ;
        pq:P958 ?section .
  FILTER ( STRSTARTS(?section, "M" ) )
  OPTIONAL { ?node pq:P304 ?page }
  OPTIONAL { ?node pq:P478 ?volume }
}
Try it!
Cheers, VIGNERON (talk) 06:41, 23 August 2024 (UTC)[reply]

Trying to finding a 1 line mul solution reveals a bug

[edit]

The upcoming mul change needs single line label/FILTER statements replaced with 3 line ones. In an attempt to circumvent this I coded

SELECT DISTINCT ?item ?qcount ?qlabel ?quantity WHERE{
  VALUES ?item {wd:Q17122887}
   { ?item p:P516 [ps:P516 ?qname; pq:P1114 ?qcount].
   SERVICE wikibase:label {bd:serviceParam wikibase:language "en,mul".
     ?qname rdfs:label ?qlabel.
   }
    #works
    #BIND(CONCAT(" ",?qlabel) AS ?quantity)
    # does not work
    BIND(CONCAT(STR(?qcount)," ",?qlabel) AS ?quantity)
  }
}
Try it!

But the concatenation of 2 variables does not work. This smells like a bug to me. Vicarage (talk) 06:46, 23 August 2024 (UTC)[reply]

@Vicarage: what doesn't work exactly? or what did you expect? I do see the query doing what is asked and "16 Draco" correctly concatenated in the last column. Also, these items have a "en" label but no "mul" labels ; for checking, I tried with different items with "mul" labels and it also worked. Cheers, VIGNERON (talk) 09:24, 23 August 2024 (UTC)[reply]

Oops, its using qname that fails, not qcount. The mul bit works fine, I tested it on en,de for other stuff

SELECT DISTINCT ?item ?qcount ?qlabel ?quantity WHERE{
  VALUES ?item {wd:Q17122887}
   { ?item p:P516 [ps:P516 ?qname; pq:P1114 ?qcount].
   SERVICE wikibase:label {bd:serviceParam wikibase:language "en,mul".
     ?qname rdfs:label ?qlabel.
   }
    # works
    #BIND(CONCAT(" ",?qlabel) AS ?quantity)
    # works 
    # BIND(CONCAT(STR(?qcount)," ",?qlabel) AS ?quantity)
    # does not work
    BIND(CONCAT(STR(?qname)," ",?qlabel) AS ?quantity)
  }
}
Try it!
@Vicarage: indeed, this is a strange bug. Especially as it works if you do it in 2 lines :
SELECT DISTINCT ?item ?qcount ?qlabel ?quantity WHERE{
  VALUES ?item {wd:Q17122887}
   { ?item p:P516 [ps:P516 ?qname; pq:P1114 ?qcount].
   SERVICE wikibase:label {bd:serviceParam wikibase:language "en,mul".
     ?qname rdfs:label ?qlabel.
   }

    BIND(STR(?qname) AS ?qnameSTR)
    BIND(CONCAT(?qnameSTR," ",?qlabel) AS ?quantity)
  }
}
Try it!
I'm guessing the problem is caused by ?qname being an URI but it still odd... @Lucas Werkmeister: any idea what is happening?
Cdlt, VIGNERON (talk) 10:48, 23 August 2024 (UTC)[reply]
In my experience using the label service’s output anywhere other than the SELECT only works reliably with a named subquery:
SELECT DISTINCT ?item ?qcount ?qlabel ?quantity1 ?quantity2 ?quantity3 WITH {
  SELECT ?item ?qname ?qcount ?qlabel WHERE {
    VALUES ?item {wd:Q17122887}
    ?item p:P516 [ps:P516 ?qname; pq:P1114 ?qcount].
    SERVICE wikibase:label {
      bd:serviceParam wikibase:language "en,mul".
      ?qname rdfs:label ?qlabel.
    }
  }
} AS %results WHERE {
  INCLUDE %results.
  # works
  BIND(CONCAT(" ",?qlabel) AS ?quantity1)
  # works 
  BIND(CONCAT(STR(?qcount)," ",?qlabel) AS ?quantity2)
  # does not work
  BIND(CONCAT(STR(?qname)," ",?qlabel) AS ?quantity3)
}
Try it!
Lucas Werkmeister (talk) 14:17, 24 August 2024 (UTC)[reply]
disappointing news for anyone hoping that mul could be introduced with minimum disruption. The FILTER approach does seem reliable, so I guess I will stick to that. Vicarage (talk) 15:19, 24 August 2024 (UTC)[reply]