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/10.

Number of artists / cultural organisations from Québec[edit]

Hi! I am looking to find out how many artists (Q483501) or cultural institutions (Q3152824) / art organizations (Q7062022) from the province of Quebec (Q176) are listed actually on Wikidata (all disciplines combined). I have no clue as to where to start in creating a SPARQL query, but here are some properties I found that could be useful (I think), if someone (anyone) is willing to give me a hand. Thanks in advance for your help.

  • Wikidata property related to art (Q27918607)
  • Québec (Q176)
  • Artists (Q483501)
  • Cultural institution (Q3152824)
  • not-for-profit arts organization (Q7062022)
  • nonprofit organization (Q163740)
  • field of this occupation : Art (Q735)
  • sub-class of : Art (Q2018526)
  • part of / field of work : Culture (Q11042)
  • has quality of : art genre (Q1792379) , art style (Q1792644)
  • Catégorie:Arts (Q4104783)
  • Catégorie:Association ou organisme culturel au Québec (Q49656487)
  • Catégorie:Association ou organisme culturel (Q8809115)

Finding items with articles in any Wikipedia but no in other projects (Documentation about schema:X usage)[edit]

Hi there,

I'm trying to get people from a given country with sitelinks to Wikipedia, but not to other Wikimedia projects.

With this query I can get all people from Q298, with sitelinks;

SELECT DISTINCT ?item ?itemLabel ?itemDescription  ?article 
WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P27 wd:Q298.
    ?article schema:about ?item
    SERVICE wikibase:label { bd:serviceParam wikibase:language "es,en". }

}
Try it!

And with this I can get all ppl with sitelinks on English Wikipedia:

SELECT DISTINCT ?item ?itemLabel ?itemDescription  ?article 
WHERE {
  ?item wdt:P31 wd:Q5;
    wdt:P27 wd:Q298.
    ?article schema:about ?item.
     ?article schema:isPartOf <https://en.wikipedia.org/>.
    SERVICE wikibase:label { bd:serviceParam wikibase:language "es,en". }

}
Try it!

My questions really are two:

  • How can I use wild cards on these queries to do something *.wikipedia.org
  • Where can I find documentation about the "schema:" syntaxis? I've just found examples, but not a explanation how to use it.

Thanks in advance! Diego (WMF) (talk) 02:43, 28 September 2021 (UTC)[reply]

@Diego (WMF): As to documentation, we don't go overboard on that sort of thing, but there's a definition of the structure in the RDF dump format page; and the data model diagram helps a little bit, at least by showing ?article in relation to ?item.
Here are a couple of answers for you, dealing mainly with the difference between any sitelinks, and sitelinks to language wikipedias.
case: has en wiki article but no other sitelinks to any wikimedia property. In this query we cheat and use the wikibase:sitelinks predicate to check there is only one sitelink.
SELECT DISTINCT ?item ?itemLabel ?itemDescription  ?article WHERE 
{
  ?item wdt:P27 wd:Q298.
  ?item wdt:P31 wd:Q5.
  ?article schema:about ?item.
  ?article schema:isPartOf <https://en.wikipedia.org/>.
  ?item wikibase:sitelinks "1"^^xsd:integer.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "es,en". }
} limit 10
Try it!
case: has en wiki article but no other language wiki article (but it might have e.g. commons, wikisource). The main trick here is to find a way to exclude items that have sitelinks to other language wikipedias than EN wiki, without reaching for a FILTER(), which would be hideously expensive at runtime. Queries restricted to triple patterns will always be orders of magnitude quicker. This is, in effect, your wildcard; here's my devious suggestion:
SELECT DISTINCT ?item ?itemLabel ?itemDescription  ?article WHERE 
{
  ?item wdt:P27 wd:Q298.
  ?item wdt:P31 wd:Q5.
  ?article schema:about ?item.
  ?article schema:isPartOf <https://en.wikipedia.org/>. # there is an en wiki article
  FILTER NOT EXISTS                            # exclude items if
  {
    ?article1 schema:about ?item.              # there may be another article
    ?article1 schema:inLanguage ?lang .        # in a language
    filter not exists {wd:Q328 wdt:P424 ?lang} # which language (long story short) is not "en"
    ?article1 schema:isPartOf ?thing.          # which article is part of something
    ?thing wikibase:wikiGroup "wikipedia" .    # and that thing is a language wiki
  }
    SERVICE wikibase:label { bd:serviceParam wikibase:language "es,en". }
} limit 10
Try it!
hth --Tagishsimon (talk) 03:51, 28 September 2021 (UTC)[reply]
Thanks, all this is very helpful. Diego (WMF) (talk) 22:07, 30 September 2021 (UTC)[reply]
But I'm still curious where that "wikiGroup" comes from. I don't see it in the RDF page you provided. There are more keys similar to that one? Thanks in advance Diego (WMF) (talk) 22:14, 30 September 2021 (UTC)[reply]
@Diego (WMF): It seems to be the only wikiBase: predicate attached to the site_url <en.wikipedia.org> - SPARQL. There seem to be 17 different groups into which wikimedia websites are divided - SPARQL.
"wikiGroup" is documented in the Sitelinks section of the RDF dump page, at the foot of the first block of code, but as I inferred earlier, datamodel documentation is v.terse.
There are a heaps of other wikibase: predicates - it seems to be the domain (?) for WD predicates for concepts not well served by pre-existing domains such as rdfs: owl:, skos:, &c &c. All the wikibase: predicates are documented on the dump page, but per the query above, none serve as predicates of the site_url. At risk of going wider than your interest, some I regularly reach for are wikibase:statements and wikibase:sitelinks, which provide counts of statements & sitelinks on items. And the wikibase: predicates of a property [1], useful for interrogating predicates used in items; example. --Tagishsimon (talk) 23:28, 30 September 2021 (UTC)[reply]
Thank you very much @Tagishsimon, Diego (WMF) (talk) 10:16, 12 October 2021 (UTC)[reply]

Items that are "instance of" two things at the same time[edit]

I think I might be going mad, but should this not show me everything that instance of (P31) both volume (Q1238720) and scientific journal (Q5633421)?

# things that are both somehow "volumes" and "scientific journals"
SELECT ?volume ?volumeLabel WHERE {
   ?volume wdt:P31 wd:Q1238720 .
   ?volume wdt:P31 wd:Q5633421 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!

And yet it does not find The Orchid Album (Q104551298), but it does find Popular Science Monthly, Volume 2 (Q105369310).

This does work:

SELECT DISTINCT ?item ?itemLabel WHERE {
  ?item p:P31 [ ps:P31 wd:Q1238720 ]. 
  ?item p:P31 [ ps:P31 wd:Q5633421 ].
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}
Try it!

Which makes sense, but why does the top query only work for that one item? Inductiveload (talk) 12:54, 5 October 2021 (UTC)[reply]

@Inductiveload: It's rank. In Q104551298 one of the two values is set to preferred, so wdt: only sees it, and not the other value. p:/ps: sees all.
Stick this in your vector.css - much easier to spot rank issues if they're in red or green:

.wb-deprecated { /* deprecated claims with red-ish background */

background-color:#FFE0E0;

}

.wb-preferred { /* preferred claims with green-ish background */

background-color:#E0FFE0;

}

--Tagishsimon (talk) 13:21, 5 October 2021 (UTC)[reply]
I see. IDK what the rank is supposed to mean in that context! Is there a way to make wdt: immune to rank, or that a feature, and p:/ps: is the correct way to ignore it? Thanks for the CSS :-) Inductiveload (talk) 13:26, 5 October 2021 (UTC)[reply]
wdt: is doing what it's designed to do, so p:/ps: is the cure. Once down that path, you can look at, for instance, the actual rank (e.g. to remove deprecated values) or, much the same thing, look out for wikibase:bestRank (iirc) statements.
As to what the rank is seeking to convey in that instance, I've no clue, either, but for your report, it's the garbage in which results in garbage out. Ideally a change from normal rank should be qualified with a reason but... --Tagishsimon (talk) 13:36, 5 October 2021 (UTC)[reply]
Got it, thank you! Inductiveload (talk) 14:08, 5 October 2021 (UTC)[reply]

Q-ID for known string of item label[edit]

Hi,

I try to get the ID-Q-Number of persons where the item label is the persons name. I tried this:

SELECT ?person
      WHERE { ?person wdt:P734 ?familyname .
                            
       Values ?familyname  {'Bertha Krupp'}.  
       SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de".}
Try it!

Properly it's super easy... However I don't manage it right now. Thank you for help or hint!  – The preceding unsigned comment was added by EvaSeidlmayer (talk • contribs) at 15:38:33 (UTC).

You should probably just use the item for "Krupp" as a family name: Krupp (Q37069298):
SELECT ?person ?personLabel WHERE {
  VALUES ?familyNameItem { wd:Q37069298 }
  ?person wdt:P734 ?familyNameItem .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de" }
}
Try it!
However, if you had to use a string, you can do it be selecting the family name(s) with a suitable native label (P1705):
SELECT ?person ?personLabel WHERE {
  ?person wdt:P734 ?familyName .
  ?familyName wdt:P1705 "Krupp"@mul
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de" }
}
Try it!
I am unsure if it is always a safe bet to use the mul there, however. Wikidata:WikiProject_Names/Properties#Family_name indicates that family names should always have a mul. However, if you have the QID for "Krupp as a family name" already, I'd use that anyway.
Also I see I misread the question and though you were looking for the family name only!
Inductiveload (talk) 15:42, 5 October 2021 (UTC)[reply]
I would use the actual labels and query like this:
SELECT ?item WHERE { ?item rdfs:label 'Bertha Krupp'@en }
Try it!
This query yields all items with the exact English label "Bertha Krupp". It works with other languages as well, just change the language code after the @ sign. —MisterSynergy (talk) 15:50, 5 October 2021 (UTC)[reply]


ha! @MisterSynergy great this is what I was looking for! Thank you a lot @Inductiveload this @mult was new to me!

EvaSeidlmayer (talk) 15:54, 5 October 2021 (UTC)[reply]
Note that this will not find "Bertha B. Krupp" or "Bertha Krupp von Bohlen und Halbach", but one of these will:
# People with family name "Krupp" and given name "Bertha"
SELECT DISTINCT ?person ?personLabel WHERE {
  ?person wdt:P734 wd:Q37069298 .
  ?person wdt:P735 wd:Q16420820 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de" }
}
Try it!
or
# People with family name "Krupp" and "Bertha" somewhere in the label
SELECT DISTINCT ?person ?personLabel WHERE {
  ?person wdt:P734 wd:Q37069298 .
  ?person rdfs:label ?label .
  FILTER REGEX(?label, "Bertha")
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de" }
}
Try it!
Whatever works! Inductiveload (talk) 16:07, 5 October 2021 (UTC)[reply]

optional WHERE statement?[edit]

Hi, using an f-String for replacement in Python, I loop through a list of historical persons trying to get their family information. Unfortunately, my query only gives me information if the person has an entry for her/his father AND an entry for her/his mother AND an entry for one or more child(ren). Since many persons do not have entries for one or the other, I don't receive any information from the Wikidata.

How can I make the WHERE statements facultative/optional? I mean like "try; except: continue" . I would like to get something like an empty string, if there is no information in the database.

In the example, Tilo Wilmowsky (Q2433711) does not have an entry for his mother.


SELECT ?personLabel ?motherLabel ?mother ?fatherLabel ?father ?childLabel ?child 
            WHERE { VALUES ?person { wd:Q2433711 } 
            ?person wdt:P22 ?father . 
            ?person wdt:P25 ?mother . 
            ?person wdt:P40 ?child .
            SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de" }
            }
Try it!


Right now I use a workaround with three separate queries. However, you might know a more elegant way... Thank you in advance!!

EvaSeidlmayer (talk) 11:44, 6 October 2021 (UTC)[reply]
@EvaSeidlmayer: I think this is what you're after. It's now optional if the person has any relatives; you'll get the person's label back if they have one. Presumably you get a blank row back if there is not label; if that's a problem, maybe add ?person to your select since there is always a value for that.
SELECT ?personLabel ?motherLabel ?mother ?fatherLabel ?father ?childLabel ?child 
            WHERE { VALUES ?person { wd:Q2433711 } 
            OPTIONAL { ?person wdt:P22 ?father . } 
            OPTIONAL { ?person wdt:P25 ?mother . }
            OPTIONAL { ?person wdt:P40 ?child . }
            SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de" }
            }
Try it!
hth --Tagishsimon (talk) 11:39, 6 October 2021 (UTC)[reply]
fantastic! ...and much more elegant!! EvaSeidlmayer (talk) 13:10, 6 October 2021 (UTC)[reply]
Tagishsimon wrote "Presumably you get a blank row back if there is not label". No, the label service will always return a value. If no label exist in any of the selected languages, it will return the item title (i.e. the Q number). --Dipsacus fullonum (talk) 20:33, 7 October 2021 (UTC)[reply]

astronautas[edit]

Quiero una tabla con todos los astronautas y algunos datos. Pero no soy capaz...

gracias SELECT ?item ?itemLabel ?nombre ?pa_s_de_nacionalidad ?pa_s_de_nacionalidadLabel ?nombreLabel ?misi_n_del_astronauta ?misi_n_del_astronautaLabel ?premio_recibido ?premio_recibidoLabel ?fecha_de_nacimiento ?inicio_del_periodo_de_actividad WHERE {

 ?item wdt:P31 wd:Q5.
 OPTIONAL {  }
 ?item wdt:P106 wd:Q11631.
 OPTIONAL { ?item wdt:P2561 ?nombre. }
 OPTIONAL { ?item wdt:P27 ?pa_s_de_nacionalidad. }
 OPTIONAL { ?item wdt:P450 ?misi_n_del_astronauta. }
 OPTIONAL { ?item wdt:P2873 ?premio_recibido. }
 OPTIONAL { ?item wdt:P569 ?fecha_de_nacimiento. }
 OPTIONAL { ?item wdt:P2031 ?inicio_del_periodo_de_actividad. }

}

@Quimper200: If I understand correctsly, you are missing a call to the label service to get labels:
SELECT ?item ?itemLabel ?nombre ?pa_s_de_nacionalidad ?pa_s_de_nacionalidadLabel ?nombreLabel ?misi_n_del_astronauta ?misi_n_del_astronautaLabel ?premio_recibido ?premio_recibidoLabel ?fecha_de_nacimiento ?inicio_del_periodo_de_actividad WHERE {

 ?item wdt:P31 wd:Q5.
 OPTIONAL {  }
 ?item wdt:P106 wd:Q11631.
 OPTIONAL { ?item wdt:P2561 ?nombre. }
 OPTIONAL { ?item wdt:P27 ?pa_s_de_nacionalidad. }
 OPTIONAL { ?item wdt:P450 ?misi_n_del_astronauta. }
 OPTIONAL { ?item wdt:P2873 ?premio_recibido. }
 OPTIONAL { ?item wdt:P569 ?fecha_de_nacimiento. }
 OPTIONAL { ?item wdt:P2031 ?inicio_del_periodo_de_actividad. }
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es,en". } # Helps get the label in your language, if not, then en language
}
Try it!
--Tagishsimon (talk) 11:22, 9 October 2021 (UTC)[reply]

National flags ordered by population[edit]

I don’t know why this simple query always times out. Any idea why? It should return a list of countries, sorted by decreasing population, with their national flags.

SELECT ?country ?countryLabel ?flag ?population

WHERE {
    ?country wdt:P31 wd:Q6256;
             wdt:P1082 ?population.

    ?flag wdt:P31 wd:Q186516;
          wdt:P1001 ?country.
  
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

ORDER BY DESC(?population)
LIMIT 10
Try it!
@Jollywatt: It does seem a little slow. Putting it into a named subquery form seems to solve it, but without any real insight into the problem. In other news, ?country wdt:P31 wd:Q6256 may be a poor way of selecting countries; I suspect some countries have sovereign state (Q3624078) with a preferred rank, which will mean there is no wdt:P31 wd:Q6256 for that item. And both country and sovereign state may well return defunct countries, which may not be what you want. My quick & dirty method for getting a list of countries, avoiding these issues, is to select members of the United Nations, fwiw.
SELECT ?country ?countryLabel ?flag ?population with { 
  SELECT ?country ?flag ?population
  WHERE {
    ?country wdt:P31 wd:Q6256; 
    wdt:P1082 ?population. 

    ?flag wdt:P31 wd:Q186516;
          wdt:P1001 ?country.
  } } as %i
WHERE
{
  INCLUDE %i
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
--Tagishsimon (talk) 11:23, 9 October 2021 (UTC)[reply]
@Jollywatt: I cannot say exactly why your query times out, but queries involving country items often are slow due to the very large size of these items (many statements). The query will be much faster (about 1 second) if you use flag (P163) instead of instance of (P31) national flag (Q186516) and applies to jurisdiction (P1001), and will also return more results at the same time:
SELECT ?country ?countryLabel ?flag ?population

WHERE {
    ?country wdt:P31 wd:Q6256;
             wdt:P1082 ?population;
             wdt:P163 ?flag.

    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?population)
Try it!
--Dipsacus fullonum (talk) 16:14, 9 October 2021 (UTC)[reply]
Thanks guys! It still remains mysterious, because the query itself doesn’t seem very complex to me (and there aren’t that many present day countries — about 200). Anyway, the following is what I’ve gone with (it grabs images of the countries' flags). You can then use the Wikimedia commons API to grab the `.svg` files themselves, as described [here](https://stackoverflow.com/a/46441957/1937384).
This is useful for people wanting to learn some geography and memorize national flags in the most effective order!
# Countries by descending population with images of their flags
SELECT ?country ?countryLabel ?flagImage ?population
WHERE {
    ?country wdt:P31 wd:Q6256;
             wdt:P1082 ?population;
             wdt:P163 [ wdt:P18 ?flagImage ].
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY DESC(?population)
Try it!
Jollywatt (talk) 03:09, 11 October 2021 (UTC)[reply]

Bio pages in ES WP, not in LAD WP[edit]

Wikidata item Q8888860 (Category:Uruguayan Jews) has 48 member pages in the Spanish (ES) WP. How can I check whether those (biography) pages have an article in the Ladino (LAD) WP? Have I provided enough information to formulate a query? Thank you! -- Deborahjay (talk) 10:54, 12 October 2021 (UTC)[reply]

@Deborahjay: This query does more than you asked for. It finds all items in the category Category:Uruguayan Jews (Q8888860) in any Wikimedia project, and for each of them gives the name of the Ladino aricle if it exists.
SELECT ?item ?itemLabel ?lad_article ?lad_title ?found_in
WITH
{
  SELECT 
    ?item
    (GROUP_CONCAT(?endpoint; SEPARATOR=", ") AS ?found_in)
  WHERE
  {
    ?category schema:name "Categoría:Judíos de Uruguay"@es .
    ?category schema:isPartOf <https://es.wikipedia.org/> .
    ?category schema:about ?category_item .
    ?other_cat schema:about ?category_item .
    ?other_cat schema:isPartOf ?wikimedia_site .
    ?other_cat schema:name ?category_title .
    BIND (STRBEFORE(STRAFTER(STR(?wikimedia_site), "https://"), "/") AS ?endpoint)
    OPTIONAL
    {
      SERVICE wikibase:mwapi
      {
        bd:serviceParam wikibase:endpoint ?endpoint .
        bd:serviceParam wikibase:api "Generator" .
        bd:serviceParam mwapi:generator "categorymembers" .
        bd:serviceParam mwapi:gcmtitle ?category_title .
        bd:serviceParam mwapi:gcmlimit "max" .
        ?item wikibase:apiOutputItem mwapi:item .
      }
    }
    FILTER BOUND (?item)
  }
  GROUP BY ?item
} AS %get_items
WHERE
{
  hint:Query hint:optimizer "None" .
  INCLUDE %get_items
  OPTIONAL
  {
    ?lad_article schema:about ?item .
    ?lad_article schema:isPartOf <https://lad.wikipedia.org/> .
    ?lad_article schema:name ?lad_title .
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!
--Dipsacus fullonum (talk) 13:15, 12 October 2021 (UTC)[reply]
The "added value" of other WP language projects does expand the content in a meaningful way, including a focus on improving the WD items with some missing labels. I started with the ES WP category because it had more pages than the EN WP. Also the similarity between Spanish and Ladino will make those pages more likely sources for translation. Looking forward to progress! -- Deborahjay (talk) 13:27, 12 October 2021 (UTC)[reply]
I made it that because I used as model a query I sometimes use on dawiki to find existing articles not in some category, but where the corresponding article in some other Wikipedia are in that Wikipedia's corresponding category. That is usually a sign of missing categorization. And it was easiest to leave in searching in all categories. --Dipsacus fullonum (talk) 15:06, 12 October 2021 (UTC)[reply]

How to get a list of humans, including some of their properties, who died on a particular date?[edit]

I am so happy I found this page! I've been improving and adding to the list of notable deaths for some years now and I only now realized that the Wikidata db could save me oceans of time.
I already created some software to semi-automate the creation of the older monthly deaths pages. I still need a query that will return a list of humans whose date of death matches the input (date) variable.

Per human next properties should be returned:

Preferably I'd like to sort on surname but I have not found that data item property.

This is how for I got myself (input death date is 25 Aug. 2001):

SELECT ?item ?itemLabel ?itemDescription ?sitelinks where
{
  ?item wdt:P31 wd:Q5;
  wikibase:sitelinks ?sitelinks.
  ?item p:P570 ?statement_0.
  ?statement_0 psv:P570 ?statementValue_0.
  ?statementValue_0 wikibase:timePrecision ?precision_0.
  FILTER(?precision_0 >= 11 )
  ?statementValue_0 wikibase:timeValue ?P570_0.
  BIND("+2001-08-25T00:00:00Z"^^xsd:dateTime AS ?P570_0)
  SERVICE wikibase:label {bd:serviceParam wikibase:language "en"}
 }
ORDER BY DESC(?sitelinks)
Try it!

So I don't know how to include the subjects's birth date, manner of death and cause of death, including references (if any).

Thank a lot in advance! Regards, Mill 1 (talk) 13:50, 12 October 2021 (UTC)[reply]

@Mill 1: It is complicated to include references for two properties because each item can have multiple statements for cause of death and manner of death, and each of these statements can have multiple references each consisting of multiple reference statements. The simple solution is get a result for each reference statement, but you will get a lot of results for the same items when they have many references with many reference statements. That solution is like this:
SELECT
  ?item ?itemLabel ?itemDescription ?sitelinks ?date_of_birth
  ?cause_of_deathLabel ?cod_ref_propLabel ?cod_ref_valueLabel
  ?manner_of_deathLabel ?mod_ref_propLabel ?mod_ref_valueLabel
WHERE
{
  ?item wdt:P31 wd:Q5 .
  ?item wdt:P570 "+2001-08-25T00:00:00Z"^^xsd:dateTime .
  ?item wikibase:sitelinks ?sitelinks .
  OPTIONAL { ?item wdt:P569 ?date_of_birth . }
  OPTIONAL
  {
    ?item p:P509 ?cod_stm .
    ?cod_stm ps:P509 ?cause_of_death .
    OPTIONAL
    {
      ?cod_stm prov:wasDerivedFrom ?cod_ref .
      ?cod_ref ?cod_ref_pr ?cod_ref_value .
      ?cod_ref_prop wikibase:reference ?cod_ref_pr .
    }
  }
  OPTIONAL
  {
    ?item p:P1196 ?mod_stm .
    ?mod_stm ps:P1196 ?manner_of_death .
    OPTIONAL
    {
      ?mod_stm prov:wasDerivedFrom ?mod_ref .
      ?mod_ref ?mod_ref_pr ?mod_ref_value .
      ?mod_ref_prop wikibase:reference ?mod_ref_pr .
    }
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?sitelinks)
Try it!
To get a better overview you can group by e.g. each statement for cause of death and then by each statement for manner of death. It can be done like this:
SELECT
  ?item ?itemLabel ?itemDescription ?sitelinks ?date_of_birth
  ?cod ?cod_refs ?mod ?mod_refs
WITH
{
  SELECT
    ?item ?cod_stm (SAMPLE(?codLabel) AS ?cod)
    (GROUP_CONCAT(CONCAT(?cod_ref_propLabel, ": ", ?cod_ref_valueLabel); SEPARATOR=", ") AS ?cod_refs)
  WHERE
  {
    ?item wdt:P31 wd:Q5 .
    ?item wdt:P570 "+2001-08-25T00:00:00Z"^^xsd:dateTime .
    OPTIONAL
    {
      ?item p:P509 ?cod_stm .
      ?cod_stm ps:P509 ?cod .
      OPTIONAL
      {
        ?cod_stm prov:wasDerivedFrom ?cod_ref .
        ?cod_ref ?cod_ref_pr ?cod_ref_value .
        ?cod_ref_prop wikibase:reference ?cod_ref_pr .
      }
    }
    SERVICE wikibase:label
    {
      bd:serviceParam wikibase:language "en" .
      ?cod rdfs:label ?codLabel .
      ?cod_ref_prop rdfs:label ?cod_ref_propLabel .
      ?cod_ref_value rdfs:label ?cod_ref_valueLabel .
    }
  }
  GROUP BY ?item ?cod_stm
} AS %get_cod_refs
WITH
{
  SELECT
    ?item ?cod_stm ?cod ?cod_refs (SAMPLE(?modLabel) AS ?mod)
    (GROUP_CONCAT(CONCAT(?mod_ref_propLabel, ": ", ?mod_ref_valueLabel); SEPARATOR=", ") AS ?mod_refs)
  WHERE
  {
    INCLUDE %get_cod_refs
    OPTIONAL
    {
      ?item p:P1196 ?mod_stm .
      ?mod_stm ps:P1196 ?mod .
      OPTIONAL
      {
        ?mod_stm prov:wasDerivedFrom ?mod_ref .
        ?mod_ref ?mod_ref_pr ?mod_ref_value .
        ?mod_ref_prop wikibase:reference ?mod_ref_pr .
      }
    }
    SERVICE wikibase:label
    {
      bd:serviceParam wikibase:language "en" .
      ?mod rdfs:label ?modLabel .
      ?mod_ref_prop rdfs:label ?mod_ref_propLabel .
      ?mod_ref_value rdfs:label ?mod_ref_valueLabel .
    }
  }
  GROUP BY ?item ?mod_stm ?cod_stm ?cod ?cod_refs
} AS %get_mod_refs
WHERE
{
  INCLUDE %get_mod_refs
  ?item wikibase:sitelinks ?sitelinks .
  OPTIONAL { ?item wdt:P569 ?date_of_birth . }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?sitelinks)
Try it!
This query still have problems. It will aggregate reference statements from different references to the same statements together, and still give multiple results when an item has multiple statements for cause and/or manner of deaths. To solve this you will need additional levels of grouping. But this query will probably be good enough for most cases. --Dipsacus fullonum (talk) 07:02, 13 October 2021 (UTC)[reply]

Finding Wikisource pages that are not connected to a Wikipedia page[edit]

I'm trying to figure out a way to see if we can detect Wikisource pages that don't have a Wikipedia page. I have the following naive query, but this does not consider the fact that many WS pages connect to the edition, and the WP/Commons pages connect to the work.

# Wikisource-linked items without Wikipedia links
SELECT ?work ?page_titleWS WHERE {

  ?work wdt:P31/wdt:P279* wd:Q17537576 .

  ?wsarticle schema:about ?work;
             schema:isPartOf <https://en.wikisource.org/>;
             schema:name ?page_titleWS.
  
  MINUS {
    ?wparticle schema:about ?work;
      schema:isPartOf <https://en.wikipedia.org/>;
      schema:name ?page_titleWP.
  }
}
LIMIT 50
Try it!

As is my new tradition, I have run head-first into query timeouts when trying to do anything involving edition or translation of (P629). Is there any way to figure out a list of Wikisource pages that are not connected to Wikipedia, either directly, or via a parent work through a edition or translation of (P629) relation? Inductiveload (talk) 09:47, 14 October 2021 (UTC)[reply]

@Inductiveload: Looks like the normal optimiser fail; uncountable millions of items are a subclass of a creative work and, presumably, the report engine chooses to look for all of these first. The named subquery is your friend, presenting only the results of the schema.about enquiries to the P31/P279 bottom check.
# Wikisource-linked items without Wikipedia links
SELECT ?work ?page_titleWS WITH {
  SELECT ?work ?page_titleWS WHERE {
    ?wsarticle schema:about ?work;
               schema:isPartOf <https://en.wikisource.org/>;
               schema:name ?page_titleWS.
    MINUS { ?wparticle schema:about ?work;
                       schema:isPartOf <https://en.wikipedia.org/>. }
  } } as %i
WHERE {
  INCLUDE %i
  ?work wdt:P31/wdt:P279* wd:Q17537576 . hint:Prior hint:gearing "forward" .   
  }
Try it!
--Tagishsimon (talk) 13:42, 14 October 2021 (UTC)[reply]
Very nice! But is there any practical way to query via the edition or translation of (P629) relationship. I.e. (WS page)→Q(edition)–P629→Q(work)→(missing WP article)? I can manage with an actual script if needed, but a SPARQL query would be more efficient, if a bit mind-bending! Inductiveload (talk) 21:41, 15 October 2021 (UTC)[reply]
@Inductiveload: Sure it's possible. Less sure what 'it' is, exactly. Below I look for the P629 of the ?wsarticle ?work and knock out any P629s that have an EN wiki article. So the ?wsarticle ?work has no EN article, does have a P629 relationship, and its P629 has no EN wiki article.
# Wikisource-linked items without Wikipedia links
SELECT ?work ?page_titleWS WITH {
  SELECT ?work ?page_titleWS WHERE {
    ?wsarticle schema:about ?work;
               schema:isPartOf <https://en.wikisource.org/>;
               schema:name ?page_titleWS.
    MINUS { ?wparticle schema:about ?work;
                       schema:isPartOf <https://en.wikipedia.org/>. }
  } } as %i
WHERE {
  INCLUDE %i
  ?work wdt:P31/wdt:P279* wd:Q17537576 . hint:Prior hint:gearing "forward" .   
  ?work wdt:P629 ?P629 . 
  MINUS { ?wparticle_P629 schema:about ?P629;
                       schema:isPartOf <https://en.wikipedia.org/>. }
  } group by ?work ?page_titleWS
Try it!
and then in this one, we include all ?wsarticle / ?work items that have no EN wiki, and if they have a P629 with an EN wiki article, we remove them
# Wikisource-linked items without Wikipedia links
SELECT ?work ?page_titleWS  WITH {
  SELECT ?work ?page_titleWS WHERE {
    ?wsarticle schema:about ?work;
               schema:isPartOf <https://en.wikisource.org/>;
               schema:name ?page_titleWS.
    MINUS { ?wparticle schema:about ?work;
                       schema:isPartOf <https://en.wikipedia.org/>. }
  } } as %i
WHERE {
  INCLUDE %i
  ?work wdt:P31/wdt:P279* wd:Q17537576 . hint:Prior hint:gearing "forward" .   
  OPTIONAL {?work wdt:P629 ?P629 . 
  MINUS { ?wparticle_P629 schema:about ?P629;
                       schema:isPartOf <https://en.wikipedia.org/>. } }
  } group by ?work ?page_titleWS
Try it!
(I've used group by ?work ?page_titleWS, btw, b/c I think some ?work have multiple P629s, which throws the row count off when trying to compare the 178,263 rows from the first query, with the 177,569 from that last query.) Not sure if either of these are the directions you wanted to go in. --Tagishsimon (talk) 22:59, 16 October 2021 (UTC)[reply]

Wikimedia categories in bnwiki[edit]

I need a query contains instance of (P31): Wikimedia category (Q4167836) and

  1. has only one sitelink to Bengali Wikipedia (Q427715);
  2. has sitelink to Bengali Wikipedia (Q427715) but not in English Wikipedia (Q328). – Afeef (talk) 03:49, 16 October 2021 (UTC)[reply]
@Afeef: With luck, these are what you are after.
SELECT ?item ?itemLabel ?category_titleBN
WHERE 
{
  ?item wdt:P31 wd:Q4167836. 
  ?bn_category schema:about ?item;
                       schema:isPartOf <https://bn.wikipedia.org/>;
                       schema:name ?category_titleBN. 
  ?item wikibase:sitelinks "1"^^xsd:integer
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],bn,en". }
}
Try it!
SELECT ?item ?itemLabel ?category_titleBN WITH { 
  SELECT ?item ?category_titleBN  WHERE 
  {
    ?item wdt:P31 wd:Q4167836. 
    ?bn_category schema:about ?item ;
                 schema:isPartOf <https://bn.wikipedia.org/> ;
                 schema:name ?category_titleBN . 
  } } as %i
WHERE
{
  INCLUDE %i
  FILTER NOT EXISTS { ?en_category schema:about ?item;#
                       schema:isPartOf <https://en.wikipedia.org/>. }          
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],bn,en". }
}
Try it!
--Tagishsimon (talk) 23:12, 16 October 2021 (UTC)[reply]
@Tagishsimon: Thank you! Face-smile.svgAfeef (talk) 03:01, 17 October 2021 (UTC)[reply]

Get all the possible values of "instance of" property[edit]

Hello, What is the query to get all the possible values of "instance of" property?

Thanks,

This seems to work:
SELECT ?P31 ?P31Label WITH {
  SELECT DISTINCT ?P31 WHERE
{
  ?item wdt:P31 ?P31. 
  } } as %i
WHERE
{
  INCLUDE %i
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language
}
Try it!
--Tagishsimon (talk) 15:30, 16 October 2021 (UTC)[reply]
I should say, the basic query is as shown below, and all of the rest is about getting labels for the P31 values whilst avoiding a timeout.
SELECT DISTINCT ?P31 WHERE
{
  ?item wdt:P31 ?P31. 
}
Try it!
--Tagishsimon (talk) 15:32, 16 October 2021 (UTC)[reply]

Count only Wikipedia links for list of articles[edit]

Hi. I'm trying to create list like en:Wikipedia:Articles in many other languages but not on English Wikipedia/Update (English) for Japanese Wikipedia.

First, I've tried:

SELECT  ?item ?label ?w ?linkcount WHERE{
  ?item wikibase:sitelinks ?linkcount .
  values ?linkcount {90 91 92 93 94 95 96}
  ?item rdfs:label ?label. 
  FILTER(lang(?label)="en")
  FILTER(!strstarts(?label, "Category:"))
  FILTER(!strstarts(?label, "Wikipedia:"))
  FILTER(!strstarts(?label, "Template:"))
  FILTER(!strstarts(?label, "Module:"))
  FILTER NOT EXISTS{?sc schema:about ?item; schema:isPartOf <https://ja.wikipedia.org/> . }
    
  OPTIONAL{?item wdt:P31 ?what.?what rdfs:label ?w. FILTER(lang(?w)="en")}
}
Try it!

It works well, except linkcount is not wikipedia language links, but wikimedia sitelinks.

How can I cange it counts or specify only wikipedia links? --Suisui (talk) 05:53, 17 October 2021 (UTC)[reply]

Need help adjusting the query you helped me with[edit]

Hi,
The last query you created in this request works great! However, after testing it with several dates my specs changed a little; could you change the query so that it returns the 'date of death' and its references INSTEAD OF the references belonging to 'manner of death' and 'cause of death'?:

  • item
  • itemLabel
  • itemDescription
  • sitelinks
  • item date of birth
  • item date of death (including references)
  • item cause of death (NO references)
  • item manner of death (NO references)

And finally: could you limit the results to humans who have a page on the English (en) Wikipedia ?

Although the query has to be simplified I lack the knowledge to do it myself. Thank again, Mill 1 (talk) 08:01, 17 October 2021 (UTC)[reply]