Wikidata:Request a query

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

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

For sample queries, see Examples and Help:Dataset sizing. Property talk pages include also summary queries for these.

For help writing your own queries, or other questions about queries, see Wikidata talk:SPARQL query service/queries and Wikidata:SPARQL query service/query optimization.

Help resources about Wikidata Query Service (Q20950365) and SPARQL: Wikidata:SPARQL query service/Wikidata Query Help and Category:SPARQL.

To report an issue about the Query Service (interface, results views, export...) please see Wikidata:Contact the development team/Query Service and search.
On this page, old discussions are archived. An overview of all archives can be found at this page's archive index. The current archive is located at 2022/05.


Data matching and superset of WikiBio data setEdit

I would like to download the full article text and other metadata of the same pages as the WikiBio data set https://paperswithcode.com/dataset/wikibio both when it was created -- 2016 with 728,000 records and today with however many there are now. the crucial thing that seems to be missing from the Examples is how to specify that I need the full text of the relevant article.

Query about museum's collectionEdit

Hello everyone! I'm looking for a query that can identify all the elements of a wikimedia commons category or a museum collection (that can also capture data such as upload date, license, etc)!

How to optimise this query please ?Edit

Hi all, So I have this query https://w.wiki/55Uu that works pretty well. Basically, it shows the number of Qitems with P269 broken down according to different person identifiers (Q36218176) somewhat related to France. I would like my query to be a little wider and to deal with all identifiers (Q19847637) and/or not related to France. The problem is that every time I try I reach a timeout. From the error messages I got I understand that you can't have nested INCLUDE, but I may be wrong. Does anyone have a solution ? Thanks !

WikiProject Climate Change part 2Edit

Hi guys, it's me again, promise I won't bother you again after this. Do you know if it is possible to get those article's quality in all languages? Like if they have a badge (featured article, c class, start, sub, B, good articles, recommended articles) assigned for all those languages. I could easily get the featured articles for WikiProject Climate Change, but when I tried to incorporate that query part to the query you guys gave me it timed out. I really thought SPARQL will be easier if one works with SQL, but I was so wrong.188.177.32.70 06:38, 2 May 2022 (UTC)

Hi. Finding article badges in WDQS doesn't take significant longer time when you already have found the articles. You didn't say how you want the results presented, so I elected to incorporate the batch labels into the columns with yes or no for each language in the previous query I made. Tell if you rather want something else.
SELECT ?title_en ?article_en
  (IF(BOUND(?article_no), IF(BOUND(?badge_no), ?badge_noLabel, "yes"), "no") as ?no)
  (IF(BOUND(?article_sv), IF(BOUND(?badge_sv), ?badge_svLabel, "yes"), "no") as ?sv)
  (IF(BOUND(?article_da), IF(BOUND(?badge_da), ?badge_daLabel, "yes"), "no") as ?da)
  (IF(BOUND(?article_nn), IF(BOUND(?badge_nn), ?badge_nnLabel, "yes"), "no") as ?nn)
WITH
{
  SELECT ?title_en ?article_en
  WHERE
  {
    SERVICE wikibase:mwapi
    {
      bd:serviceParam wikibase:endpoint "en.wikipedia.org" .

      # Generator to get all transclusions of the Template "WikiProject Climate change"
      bd:serviceParam wikibase:api "Generator" .
      bd:serviceParam mwapi:generator "alltransclusions" .
      bd:serviceParam mwapi:gatfrom "WikiProject Climate change" .
      bd:serviceParam mwapi:gatto "WikiProject Climate change" .
      bd:serviceParam mwapi:gatlimit "max" .

      # Use the info module to get the name of associated page
      # (the template is on talk pages, so this gives the subject pages)
      bd:serviceParam mwapi:prop "info" .
      bd:serviceParam mwapi:inprop "associatedpage" .

      # Output
      # (There is no point in trying to get associated items, because that would be for the found talk pages) 
      ?title wikibase:apiOutput "@associatedpage" .
    }
    BIND (STRLANG(?title, "en") AS ?title_en)
    OPTIONAL { ?article_en schema:name ?title_en . }
    FILTER BOUND(?article_en)
  }
}
AS %get_titles
WHERE
{
  INCLUDE %get_titles
  ?article_en schema:isPartOf <https://en.wikipedia.org/> .
  ?article_en schema:about ?item .

  OPTIONAL
  {
    ?article_no schema:about ?item ; schema:isPartOf <https://no.wikipedia.org/> .
    OPTIONAL { ?article_no  wikibase:badge ?badge_no . }
  }
  OPTIONAL
  {
    ?article_da schema:about ?item ; schema:isPartOf <https://da.wikipedia.org/> .
    OPTIONAL { ?article_da  wikibase:badge ?badge_da . }
  }
  OPTIONAL
  {
    ?article_sv schema:about ?item ; schema:isPartOf <https://sv.wikipedia.org/> .
    OPTIONAL { ?article_sv  wikibase:badge ?badge_sv . }
  }
  OPTIONAL
  {
    ?article_nn schema:about ?item ; schema:isPartOf <https://nn.wikipedia.org/> .
    OPTIONAL { ?article_nn  wikibase:badge ?badge_nn . }
  }
  SERVICE wikibase:label
  {
    bd:serviceParam wikibase:language "en" .
    ?badge_no rdfs:label ?badge_noLabel .
    ?badge_da rdfs:label ?badge_daLabel .
    ?badge_sv rdfs:label ?badge_svLabel .
    ?badge_nn rdfs:label ?badge_nnLabel .
  }
}
Try it!
--Dipsacus fullonum (talk) 08:45, 2 May 2022 (UTC)
Thank you so much for this! Have a great day! 80.62.117.209 19:10, 4 May 2022 (UTC)

Please to make the river reach the seaEdit

This query almost draws a (main) river on a map based on the sequence of tributary river mouths on the main river. However, for obvious reasons, it does not draw the last leg of the river running from tributary mouth 1 to the main mouth. Which is to say, the river does not meet the sea. The syntax for the solution is less obvious - at least to me. We need a dot for the ?coord of the ?river, and a line composed of its ?lat ?long and the ?lat ?long of tributary mouth 1. Any help gratefully received.

(Let me share, for interest, its cousin map, which is completely bonkers, but v.colourful: map connecting tributary river mouths to a main river mouth, as the crow flies ... it turns out WD has issues drawing rivers b/c there's no good means of establishing the sequence of tributaries; distance from main mouth fails when rivers double-back. Elevation data is mostly absent and/or inaccurate.)

#title:line-map of rivers based on distance to the mouth
#defaultView:Map
SELECT ?seq ?item ?itemLabel ?coord2 ?line ?riverLabel (?river AS ?layer)

WITH {
  SELECT DISTINCT ?river ?item ?lat ?long ?coord2 ?dist WHERE { 
      VALUES ?river {wd:Q24639723} .
     
      ?river p:P403 ?stat. 
      ?river wdt:P131/wdt:P131 wd:Q22 .
      ?stat pq:P625 ?coord .                            # get the coords for the main river
  
      ?stat2 ps:P403 ?river.
      ?item p:P403 ?stat2. 
      ?stat2 pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat2 pqv:P625/wikibase:geoLongitude ?long.
      ?stat2 pq:P625 ?coord2 .                          # get coords for all tributary rivers
    BIND(geof:distance(?coord, ?coord2) as ?dist)       # distance between tributary mouth and main river mouth
  } order by ?dist 
} AS %points

WITH {                                                  # work out the sequence of mouths based on distance
  SELECT ?river ?item (COUNT(DISTINCT(?item2)) AS ?seq0) WHERE {
    INCLUDE %points .
 
    {
      SELECT ?river (?item AS ?item2) (?dist AS ?dist2) WHERE {
         INCLUDE %points .
      }
    }
    
    FILTER (?dist2 < ?dist) .
  } GROUP BY ?river ?item 
} AS %seq

WITH {                                                 # unite the sequence and the coordinate details on a single row
  SELECT ?river ?seq0 ?item ?dist ?coord2 ?lat ?long  WHERE {
       INCLUDE %points .
       INCLUDE %seq .
    }
} AS %points_seq     

WHERE {                                               # construct lines from mouth to mouth
  {
    SELECT ?river (?seq0 AS ?seq) ?item ?elev ?coord2 ?lat ?long  WHERE {
       INCLUDE %points_seq .
    }
  }
  {
    SELECT ?river ((?seq0-1) AS ?seq) (?lat AS ?lat2) (?long AS ?long2) WHERE {
       INCLUDE %points_seq .
    }
  }
  BIND(CONCAT('LINESTRING (', STR(?long), ' ', STR(?lat), ',', STR(?long2), ' ', STR(?lat2), ')') AS ?str) .
  BIND(STRDT(?str, geo:wktLiteral) AS ?line) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?seq
line-map of rivers based on distance to the mouth

--Tagishsimon (talk) 11:26, 2 May 2022 (UTC)

I would add one extra solution to the first subquery, %points, with a new point for the mount of the river and distance 0.0. The content of the subquery would be:
SELECT DISTINCT ?river ?item ?lat ?long ?coord2 ?dist WHERE { 
    {
      VALUES ?river {wd:Q24639723} .
     
      ?river p:P403 ?stat. 
      ?river wdt:P131/wdt:P131 wd:Q22 .
      ?stat pq:P625 ?coord .                            # get the coords for the main river
  
      ?stat2 ps:P403 ?river.
      ?item p:P403 ?stat2. 
      ?stat2 pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat2 pqv:P625/wikibase:geoLongitude ?long.
      ?stat2 pq:P625 ?coord2 .                          # get coords for all tributary rivers
      BIND(geof:distance(?coord, ?coord2) as ?dist)       # distance between tributary mouth and main river mouth
    }
    UNION
    {
      VALUES ( ?river ?item ?dist ) { ( wd:Q24639723 wd:Q24639723 0.0 ) }
      ?river p:P625 ?stat.
      ?stat ps:P625 ?coord2.
      ?stat psv:P625/wikibase:geoLatitude ?lat. 
      ?stat psv:P625/wikibase:geoLongitude ?long.
    }
  }
Try it!
--Dipsacus fullonum (talk) 12:29, 2 May 2022 (UTC)
PS. @Tagishsimon: It would be more accurate to use to the coordinate location (P625) qualifier of mouth of the watercourse (P403) instead of the main statement with P625 in both branches of the union. The change would be easy to make. --Dipsacus fullonum (talk) 12:41, 2 May 2022 (UTC)
Thank you, Dipsacus fullonum; your solution exactly what I was after. P403's P625 noted & agreed. --Tagishsimon (talk) 12:49, 2 May 2022 (UTC)
@Dipsacus fullonum: however ... if we now wish to generalise that report, such that we do not set a VALUES ?river, and so cannot depend on hard coding in the VALUES ( ?river ?item ?dist ) { ( wd:Q24639723 wd:Q24639723 0.0 ) } statement in the UNIONed solution... might you have a solution for that?
#title:line-map of rivers based on distance to the mouth
#defaultView:Map
SELECT ?seq ?item ?itemLabel ?coord2 ?line ?riverLabel (?river AS ?layer)

WITH {
SELECT DISTINCT ?river ?item ?lat ?long ?coord2 ?dist WHERE { 
    {
     # VALUES ?river {wd:Q24639723} .
     
      ?river p:P403 ?stat. 
      ?river wdt:P131/wdt:P131 wd:Q22 .
      ?stat pq:P625 ?coord .                            # get the coords for the main river
  
      ?stat2 ps:P403 ?river.
      ?item p:P403 ?stat2. 
      ?stat2 pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat2 pqv:P625/wikibase:geoLongitude ?long.
      ?stat2 pq:P625 ?coord2 .                          # get coords for all tributary rivers
      BIND(geof:distance(?coord, ?coord2) as ?dist)       # distance between tributary mouth and main river mouth
    }
    UNION
    {
      VALUES ( ?river ?item ?dist ) { ( wd:Q24639723 wd:Q24639723 0.0 ) }
      ?river p:P403 ?stat.
      ?stat pq:P625 ?coord2.
      ?stat pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat pqv:P625/wikibase:geoLongitude ?long.
    }
  }

} AS %points

WITH {                                                  # work out the sequence of mouths based on distance
  SELECT ?river ?item (COUNT(DISTINCT(?item2)) AS ?seq0) WHERE {
    INCLUDE %points .
 
    {
      SELECT ?river (?item AS ?item2) (?dist AS ?dist2) WHERE {
         INCLUDE %points .
      }
    }
    
    FILTER (?dist2 < ?dist) .
  } GROUP BY ?river ?item 
} AS %seq

WITH {                                                 # unite the sequence and the coordinate details on a single row
  SELECT ?river ?seq0 ?item ?dist ?coord2 ?lat ?long  WHERE {
       INCLUDE %points .
       INCLUDE %seq .
    }
} AS %points_seq     

WHERE {                                               # construct lines from mouth to mouth
  {
    SELECT ?river (?seq0 AS ?seq) ?item ?elev ?coord2 ?lat ?long  WHERE {
       INCLUDE %points_seq .
    }
  }
  {
    SELECT ?river ((?seq0-1) AS ?seq) (?lat AS ?lat2) (?long AS ?long2) WHERE {
       INCLUDE %points_seq .
    }
  }
  BIND(CONCAT('LINESTRING (', STR(?long), ' ', STR(?lat), ',', STR(?long2), ' ', STR(?lat2), ')') AS ?str) .
  BIND(STRDT(?str, geo:wktLiteral) AS ?line) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?seq
line-map of rivers based on distance to the mouth
--Tagishsimon (talk) 12:57, 2 May 2022 (UTC)
@Tagishsimon: Split %points into two subqueries: %rivers and %points:
#title:line-map of rivers based on distance to the mouth
#defaultView:Map
SELECT ?seq ?item ?itemLabel ?coord2 ?line ?riverLabel (?river AS ?layer)

WITH {
  SELECT DISTINCT ?river ?mouthlat ?mouthlong ?coord WHERE { 
      ?river p:P403 ?stat. 
      ?river wdt:P131/wdt:P131 wd:Q22 .
      ?stat pq:P625 ?coord .                            # get the coords for the main river  
      ?stat pqv:P625/wikibase:geoLatitude ?mouthlat. 
      ?stat pqv:P625/wikibase:geoLongitude ?mouthlong.
  }
} AS %rivers
WITH {
  SELECT ?river ?item ?lat ?long ?coord2 ?dist WHERE { 
    {
      INCLUDE %rivers .
      ?stat2 ps:P403 ?river.
      ?item p:P403 ?stat2. 
      ?stat2 pqv:P625/wikibase:geoLatitude ?lat. 
      ?stat2 pqv:P625/wikibase:geoLongitude ?long.
      ?stat2 pq:P625 ?coord2 .                          # get coords for all tributary rivers
      BIND(geof:distance(?coord, ?coord2) as ?dist)     # distance between tributary mouth and main river mouth
    }
    UNION
    {
      SELECT ?river (?river AS ?item) (?mouthlat AS ?lat) (?mouthlong AS ?long) (?coord AS ?coord2) (0.0 AS ?dist)
      WHERE
      { INCLUDE %rivers }
    }
  }
} AS %points

WITH {                                                  # work out the sequence of mouths based on distance
  SELECT ?river ?item (COUNT(DISTINCT(?item2)) AS ?seq0) WHERE {
    INCLUDE %points .
 
    {
      SELECT ?river (?item AS ?item2) (?dist AS ?dist2) WHERE {
         INCLUDE %points .
      }
    }
    
    FILTER (?dist2 <= ?dist) .
  } GROUP BY ?river ?item 
} AS %seq

WITH {                                                 # unite the sequence and the coordinate details on a single row
  SELECT ?river ?seq0 ?item ?dist ?coord2 ?lat ?long  WHERE {
       INCLUDE %points .
       INCLUDE %seq .
    }
} AS %points_seq     

WHERE {                                               # construct lines from mouth to mouth
  {
    SELECT ?river (?seq0 AS ?seq) ?item ?elev ?coord2 ?lat ?long  WHERE {
       INCLUDE %points_seq .
    }
  }
  {
    SELECT ?river ((?seq0-1) AS ?seq) (?lat AS ?lat2) (?long AS ?long2) WHERE {
       INCLUDE %points_seq .
    }
  }
  BIND(CONCAT('LINESTRING (', STR(?long), ' ', STR(?lat), ',', STR(?long2), ' ', STR(?lat2), ')') AS ?str) .
  BIND(STRDT(?str, geo:wktLiteral) AS ?line) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?seq
line-map of rivers based on distance to the mouth
I also made a bugfix in subquery %seq changing "<" to "<=" in the filter to include the first point which was missing. --Dipsacus fullonum (talk) 13:55, 2 May 2022 (UTC)
I just had an idea for a possible improvement. You can perhaps get more river point coordinates by looking for bridges or other things that crosses (P177) the rivers. --Dipsacus fullonum (talk) 15:40, 2 May 2022 (UTC)
  • Interesting idea of a query! I've tried to run for Q159 and have very few results (though there are many rivers and tributaries and coordinates in Wikidata)... What has to be improved to get them onto the map? E.g. look how many tributaries in Q78707! --Infovarius (talk) 08:38, 6 May 2022 (UTC)
@Infovarius: The Scottish rivers used mouth of the watercourse (P403) with coordinate location (P625) as qualifer, whereas the Russian rivers seem to use coordinate location (P625) with applies to part (P518) river mouth (Q1233637) as qualifier. I modified the query to use both methods get the coordinates of the river mouth but cannot run it for all Russian rivers due to timeout. Here is for Yenisey (Q78707). The river is too curvy for algorithm for ordering the points to fully work.
#title:line-map of rivers based on distance to the mouth
#defaultView:Map
SELECT ?seq ?item ?itemLabel ?coord2 ?line ?riverLabel (?river AS ?layer)

WITH {
  SELECT DISTINCT ?river ?mouthlat ?mouthlong ?coord WHERE { 
      VALUES ?river { wd:Q78707 } # Yenisey
      ?river wdt:P31 wd:Q4022 .
      ?river wdt:P17 wd:Q159 .
      {
        ?river p:P403 ?stat. 
        ?stat pq:P625 ?coord .                          # get the coords for the main river  
        ?stat pqv:P625/wikibase:geoLatitude ?mouthlat. 
        ?stat pqv:P625/wikibase:geoLongitude ?mouthlong.
      }
      UNION
      {
        ?river p:P625 ?stat.                           # coordinates
        ?stat pq:P518 wd:Q1233637.                     # applies to river mouth
        ?stat ps:P625 ?coord .                         # get the coords for the main river  
        ?stat psv:P625/wikibase:geoLatitude ?mouthlat. 
        ?stat psv:P625/wikibase:geoLongitude ?mouthlong.
      }
  }
} AS %rivers
WITH {
  SELECT ?river ?item ?lat ?long ?coord2 ?dist WHERE { 
    {
      INCLUDE %rivers .
      ?stat2 ps:P403 ?river.
      ?item p:P403 ?stat2. 
      {
        ?stat2 pqv:P625/wikibase:geoLatitude ?lat. 
        ?stat2 pqv:P625/wikibase:geoLongitude ?long.
        ?stat2 pq:P625 ?coord2 .                        # get coords for all tributary rivers
      }
      UNION
      {
        ?item p:P625 ?stat3. 
        ?stat3 pq:P518 wd:Q1233637.                     # applies to river mouth
        ?stat3 psv:P625/wikibase:geoLatitude ?lat. 
        ?stat3 psv:P625/wikibase:geoLongitude ?long.
        ?stat3 ps:P625 ?coord2 .                        # get coords for all tributary rivers
      }
      BIND(geof:distance(?coord, ?coord2) as ?dist)     # distance between tributary mouth and main river mouth
    }
    UNION
    {
      SELECT ?river (?river AS ?item) (?mouthlat AS ?lat) (?mouthlong AS ?long) (?coord AS ?coord2) (0.0 AS ?dist)
      WHERE
      { INCLUDE %rivers }
    }
  }
} AS %points

WITH {                                                  # work out the sequence of mouths based on distance
  SELECT ?river ?item (COUNT(DISTINCT(?item2)) AS ?seq0) WHERE {
    INCLUDE %points .
 
    {
      SELECT ?river (?item AS ?item2) (?dist AS ?dist2) WHERE {
         INCLUDE %points .
      }
    }
    
    FILTER (?dist2 <= ?dist) .
  } GROUP BY ?river ?item 
} AS %seq

WITH {                                                 # unite the sequence and the coordinate details on a single row
  SELECT ?river ?seq0 ?item ?dist ?coord2 ?lat ?long  WHERE {
       INCLUDE %points .
       INCLUDE %seq .
    }
} AS %points_seq     

WHERE {                                               # construct lines from mouth to mouth
  {
    SELECT ?river (?seq0 AS ?seq) ?item ?elev ?coord2 ?lat ?long  WHERE {
       INCLUDE %points_seq .
    }
  }
  {
    SELECT ?river ((?seq0-1) AS ?seq) (?lat AS ?lat2) (?long AS ?long2) WHERE {
       INCLUDE %points_seq .
    }
  }
  BIND(CONCAT('LINESTRING (', STR(?long), ' ', STR(?lat), ',', STR(?long2), ' ', STR(?lat2), ')') AS ?str) .
  BIND(STRDT(?str, geo:wktLiteral) AS ?line) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} ORDER BY ?seq
line-map of rivers based on distance to the mouth
--Dipsacus fullonum (talk) 09:27, 6 May 2022 (UTC)
@Dipsacus fullonum: Yenisey looks perfect now, thank you! But curvy Kama (Q79082) is more difficult to sort. --Infovarius (talk) 11:27, 8 May 2022 (UTC)

@Tagishsimon, Infovarius: I have not been aware of the distance from river mouth (P2148) property until now, but if it were systematically used to indicate the location of tributaries and bridges etc, these maps could be drawn much more accurately.

Family and business relationships of early typographers in ItalyEdit

Hi, I'm looking for a way to represent the relationships between early typographers, to visualize the results I started by maniacally adding the property partner in business or sport (P1327) to typographers and using Wikidata Graph Builder (like this), but this is only a partial solution since some segments of the tree I'm trying to build are connected by family relationships. The ideal would be to use partner in business or sport (P1327), student (P802)/student of (P1066), spouse (P26) and father (P22)/mother (P25)/child (P40) as links. To limit the tree work location (P937) could be used as filter (I'm also adding that like a maniac to every typographer). I use SPARQL a little but this is over my head, would love to learn more. Started looking at this but seems overly complex. Thanks! --divudì 07:14, 4 May 2022 (UTC)

@Divudi85 I came up with the query below. Unfortunately it’s a bit too expensive and my attempts to include the labels of the items have all made the query timeout.
#defaultView:Graph
select ?person ?linkedPerson ?type ?personLabel (?type as ?edgeLabel) {
  ?person wdt:P106 wd:Q1229025 .
  ?person wdt:P937/wdt:P17? wd:Q38 . 
  { 
    ?person wdt:P1327 ?linkedPerson .
    bind("business partner" as ?type)
  } union {
    ?person wdt:P22| wdt:P23 | wdt:P40 ?linkedPerson .
     bind("lineage" as ?type)
  } union {
    ?person wdt:P26 ?linkedPerson .
     bind("partner" as ?type)
  }
} limit 1000
Try it!
author  TomT0m / talk page 10:25, 4 May 2022 (UTC)
@TomT0m: Thanks! I tweaked it a little and filtered for "place of work" and now doesn't time out. Pretty pretty pretty cool. Thanks again :)
#defaultView:Graph
select ?person ?linkedPerson ?type ?personLabel (?type as ?edgeLabel) {
    ?person wdt:P937 wd:Q641. 
  ?person wdt:P2032 ?datafine
  { 
    ?person wdt:P1327 ?linkedPerson .
   bind("business partner" as ?type)
  } union {
    ?person wdt:P22 ?linkedPerson .
     bind("father" as ?type)
    
} union {
?person wdt:P23 ?linkedPerson .
bind("mother" as ?type)

} union {
?person wdt:P40 ?linkedPerson .
bind("son" as ?type)

} union {
?person wdt:P26 ?linkedPerson .
bind("partner" as ?type)
}
FILTER ("1850-01-01"^^xsd:dateTime > ?datafine) 
SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en" . }
}
Try it!

divudì 15:55, 4 May 2022 (UTC)

"this isn't even my final form!" LOL other tweaks. like it very much!
#defaultView:Graph
select ?person ?linkedPerson ?linkedPersonLabel ?type ?personLabel (?type as ?edgeLabel) {
   ?person wdt:P937 wd:Q641. 
{?person wdt:P106 wd:Q175151 } UNION {?person wdt:P106 wd:Q1229025} UNION {?person wdt:P106 wd:Q998550} UNION {?person wdt:P106 wd:Q2516866}UNION {?person wdt:P106 wd:Q40881196}
  { ?person wdt:P2032 ?datafine } UNION { ?person wdt:P570 ?datafine }
{ ?person wdt:P1327 ?linkedPerson .  bind("business partner" as ?type)  } union 
{ ?person wdt:P22 ?linkedPerson .  bind("father" as ?type) } union 
{ ?person wdt:P23 ?linkedPerson .  bind("mother" as ?type)  } union 
{ ?person wdt:P40 ?linkedPerson .  bind("son" as ?type) } union 
{ ?person wdt:P26 ?linkedPerson .  bind("partner" as ?type) }
FILTER ("1850-01-01"^^xsd:dateTime > ?datafine) 
SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en" . }
}
Try it!

divudì 16:22, 4 May 2022 (UTC)

I'm assuming you wanted to include people who weren't really typographers themselves, since you mentioned family relations. If you're okay with following relations from one common source and don't need it to be portable you can use the ALP blazegraph extension which I just so happened to be experimenting with today. Here showing at most six degrees of separation. Infrastruktur (talk) 11:18, 4 May 2022 (UTC)
#defaultView:Graph
SELECT ?item ?itemLabel ?linkTo
WHERE {
  SERVICE bd:alp {
    wd:Q3876594 ?pred ?item.
    hint:Prior hint:alp.pathExpr true .
    FILTER(?pred IN (wdt:P1327, wdt:P802, wdt:P1066, wdt:P26, wdt:P451, wdt:P22, wdt:P25, wdt:P40))
    hint:Group hint:alp.lowerBound 1 .
    hint:Group hint:alp.upperBound 6  .
    hint:Group hint:alp.bidirectional true .
  }
  ?item (wdt:P1327|wdt:P802|wdt:P1066|wdt:P26|wdt:P451|wdt:P22|wdt:P25|wdt:P40) ?linkTo.
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
Try it!
Hmm, I wonder if there is a problem with one of my queries. I tried to make a portable version and it ended up showing a different amount of people. Infrastruktur (talk) 11:54, 4 May 2022 (UTC)
#defaultView:Graph
SELECT ?item ?itemLabel ?linkTo
WHERE {
  { SELECT DISTINCT ?item WHERE {
  VALUES ?source { wd:Q3876594 }
  { ?source (wdt:P1327| wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)? ?item. }
  UNION
  { ?item (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)?/
    (wdt:P1327 | wdt:P802 | wdt:P1066 | wdt:P26 | wdt:P451 | wdt:P22 | wdt:P25 | wdt:P40)? ?source. }
  } }
  ?item (wdt:P1327|wdt:P802|wdt:P1066|wdt:P26|wdt:P451|wdt:P22|wdt:P25|wdt:P40) ?linkTo. hint:Group hint:optimizer "None".
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
Try it!

@Infrastruktur: Thanks! P802 & P1066 unexpectedly brought out interesting connections (but too much of it! eheh so I tried removing them). what's the meaning of "?/" in this syntax? also, could I use as source something else like "people - place of work - venice" (?typographer wdt:P937 wd:Q641) for exemple, or it must be a specific item? Thanks again! --divudì 16:10, 4 May 2022 (UTC)

@Divudi85
The « ? » stands for « maybe » in a « property path », the « / » is the sequence of path, the « | » is an « or ».
For example « wdt:P22 | wdt:P23 » means « the father or the mother ». « wdt:P22/wdt:P22 » means « the father of the father », so the grandfather of the male lineage, « wdt:P22/wdt:P22? » means « the father and possibly its father, so the query results will get both the father and the grandfather on two lines. You can compose everything together to get stuffs like « (wdt:P22 | wdt:P23)/(wdt:P22 | wdt:P23)? » which will return, the father, the mother, both grandfathers and both grandmothers.
I rewrote a bit your proposition above using « values » because it’s easier to extend and to read :
#defaultView:Graph
select ?person ?linkedPerson ?linkedPersonLabel ?type ?personLabel (?type as ?edgeLabel) {
   ?person wdt:P937 wd:Q641;
           wdt:P106 ?activity .
  
  values ?activity { 
    wd:Q175151 wd:Q1229025 wd:Q998550 wd:Q2516866 wd:Q40881196
  }
  
  ?person wdt:P2032|wdt:P570 ?datafine .
  
{ ?person wdt:P1327 ?linkedPerson .  bind("business partner" as ?type)  } union 
{ ?person wdt:P22 ?linkedPerson .  bind("father" as ?type) } union 
{ ?person wdt:P23 ?linkedPerson .  bind("mother" as ?type)  } union 
{ ?person wdt:P40 ?linkedPerson .  bind("son" as ?type) } union 
{ ?person wdt:P26 ?linkedPerson .  bind("partner" as ?type) }
FILTER ("1850-01-01"^^xsd:dateTime > ?datafine) 
SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en" . }
}
Try it!
author  TomT0m / talk page 17:12, 4 May 2022 (UTC)
Unfortunately, my particular approach requires a single source, it can't be changed to work on a group of people. Infrastruktur (talk) 21:03, 4 May 2022 (UTC)

Taming owlsEdit

You know how Wikidata uses owl:sameAs to for redirection? I was trying the same thing except with properties, but I couldn't get it to work.

I have two local datasets, one being an extract from Wikidata, and the other containing among other things:

@prefix prop: <http://example.net/example-ns/> .

prop:start a owl:ObjectProperty;
  owl:equivalentProperty wdt:P729 .
prop:end a owl:ObjectProperty;
  owl:equivalentProperty wdt:P730 .

Except when I try to use prop:start instead of wdt:P729 in a query it doesn't work. What am I doing wrong? Infrastruktur (talk) 13:20, 8 May 2022 (UTC)

I am unsure what you are trying to achieve. Fist, why use owl:ObjectProperty instead of owl:sameAs? https://www.w3.org/TR/owl-ref/#equivalentProperty-def says that "Property equality should be expressed with the owl:sameAs construct". Second, even if two properties are the same, you cannot, as far as I know, use one instead of the other in a query. Redirected items will not appear in a search either unless you explicitly search for them. --Dipsacus fullonum (talk) 14:07, 8 May 2022 (UTC)
Many thanks for the explanation, it really helped clear things up. I was hoping to remap the ontology without changing the dataset, if that makes any sense. Infrastruktur (talk) 15:15, 8 May 2022 (UTC)

Request on women prime ministers.Edit

Hello,

I would like to do a Wikidata search to get data for : Women prime ministers with their length of service in any country (with 2 variables : date_start and date_end of their post-held of prime minister). I managed to get all the elements I needed (people, country, photo, geo-coordinates), but I'm stuck with the dates. My searches return nothing at all each time with P580 & P582. I have some knowledge of code, especially Python. But I am just starting to use wikidata. Thus, Can you help me to get this request? Thank you in advance. Best

List of final searched variables ok : itemLabel, statementLabel, image , bornplaceLabel, nationalityLabel, geocoord

List o variables to add : date_start, date_end

code :

SELECT DISTINCT ?item ?itemLabel ?statement ?statementLabel ?image ?bornplace ?bornplaceLabel ?nationality ?nationalityLabel ?geocoord WHERE {

 ?item wdt:P31 wd:Q5;
       wdt:P21 wd:Q6581072;
       wdt:P39 ?statement.
       
 ?statement wdt:P279 wd:Q14212.
 
 ?item wdt:P19 ?bornplace;
       wdt:P27 ?nationality.
 
 OPTIONAL {?item wdt:P18 ?image.}
 OPTIONAL {?bornplace wdt:P625 ?geocoord.}
 
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr, en". }

}

@Ricolajazz: Your variable ?statement isn't a statement (but a held position). It needs to be a statement in order to get start and end dates as these are encoded as qualifiers to the statement with P39. See the RDF data model for how the data are organized. You may lose some results by requiring that values for P19 and P27 exist.
SELECT DISTINCT ?item ?itemLabel ?position ?positionLabel ?image
  ?bornplace ?bornplaceLabel ?nationality ?nationalityLabel ?geocoord
  ?date_start ?date_end
WHERE
{
 ?item wdt:P31 wd:Q5;
       wdt:P21 wd:Q6581072;
       p:P39 ?statement.

 ?statement ps:P39 ?position;
            pq:P580 ?date_start;
            pq:P582 ?date_end.

 ?position wdt:P279 wd:Q14212.
 
 ?item wdt:P19 ?bornplace;
       wdt:P27 ?nationality.
 
 OPTIONAL {?item wdt:P18 ?image.}
 OPTIONAL {?bornplace wdt:P625 ?geocoord.}
 
 SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr, en". }
}
Try it!
--Dipsacus fullonum (talk) 01:44, 9 May 2022 (UTC)
Good evening again,
Thank you for processing my request and for your recommendations. I will continue to try several methods in order to get the most accurate results
possible. Already, I have added an option to date_end an idea of which ministers are still active (OPTIONAL{?statement pq:P582 ?date_end. })which has increased the results.
I hope to be on the right track. In any case, I am still learning.
regards Ricolajazz (talk) 04:25, 9 May 2022 (UTC)

Query timeout limit reachedEdit

I’m trying to find all subjects and objects with the property “shares border with (P47)” on Wikipedia in Arabic (I need to retrieve the Arabic wikipedia Title), but it times out when I added the : ^schema:about schema:isPartOf Lines..

Is there any possible way to get the results with timing out?


The Query: https://w.wiki/55Un:

SELECT ?sbj_ar ?obj_ar WHERE {

 ?sbj wdt:P47 ?obj .
 
 ?sbj ^schema:about ?sbj_url_ar .
 ?sbj_url_ar schema:isPartOf <https://ar.wikipedia.org/>; schema:name ?sbj_ar .
 
 ?obj ^schema:about ?obj_url_ar .
 ?obj_url_ar schema:isPartOf <https://ar.wikipedia.org/>; schema:name ?obj_ar .

}


Thank you!

I unfortunately don't have other any answer than I had 3 weeks ago. Dipsacus fullonum (talk) 06:58, 9 May 2022 (UTC)
If A shares a border with B, then B also shares a border with A. You're getting duplicated pairs, so you ought to drop the object. Infrastruktur (talk) 07:17, 9 May 2022 (UTC)
Thank you!
Some of the properties I'm looking for does'nt apply for reversed directions. property shares_border_with will always be true if I reverse the direction of the subject and object, but other dosent such as place_of_birth'P19', place_of_death'P20', country_of_citizenship'P27' and many more. HaneenAlhomoud (talk) 08:16, 9 May 2022 (UTC)
can you help me editing my query to get the results without duplicated pairs for properties such as shares_border_with?
Thank you. HaneenAlhomoud (talk) 08:20, 9 May 2022 (UTC)
Thank you for your time!.
I need to get as many results as possible, so Limiting my results did not help much. HaneenAlhomoud (talk) 08:17, 9 May 2022 (UTC)

How to extract list of articles that are present in English wiki and also as well in another language Wikipedia?Edit

HI I want a SPARQL Query for list of wiki articles present in Telugu wikipedia(te.wikipedia.com) and also as will English Example- column1 - Articles_name(English) and column2 - articles_name(Telugu) , column3- link(Telugu Wikipedia article link), column4- link(English Wikipedia article link)

Can any one help with this pleas - Thanks in advance Ramu ummadishetty (talk) 16:35, 11 May 2022 (UTC)

@Ramu ummadishetty: Here is a query. It will not only find articles but also connected pages in other namespaces like categories, templates and modules. I could try to filter these out if that is a problem, but there is not very easy way to do it.
SELECT ?title_en ?title_te ?link_en ?link_te
WHERE
{
  ?link_te schema:isPartOf <https://te.wikipedia.org/> .
  ?link_te schema:name ?title_te .
  ?link_te schema:about ?item .
  ?link_en schema:about ?item .
  ?link_en schema:isPartOf <https://en.wikipedia.org/> .
  ?link_en schema:name ?title_en .
}
Try it!
--Dipsacus fullonum (talk) 17:04, 11 May 2022 (UTC)
Thank you so much Dipsacus fullonum this works i guess if possible try to filter, that helps a lot Ramu ummadishetty (talk) 15:06, 12 May 2022 (UTC)
@Ramu ummadishetty: Try to add MINUS { ?item wdt:P31/wdt:P279* wd:Q17379835. }. Credit for the MINUS clause goes to Infrastruktur who suggested it in the section #Filter out all "Wiki" entries below. I wasn't aware of the item Wikimedia page outside the main knowledge tree (Q17379835) before then.
SELECT ?title_en ?title_te ?link_en ?link_te
WHERE
{
  ?link_te schema:isPartOf <https://te.wikipedia.org/> .
  ?link_te schema:name ?title_te .
  ?link_te schema:about ?item .
  MINUS { ?item wdt:P31/wdt:P279* wd:Q17379835. }
  ?link_en schema:about ?item .
  ?link_en schema:isPartOf <https://en.wikipedia.org/> .
  ?link_en schema:name ?title_en .
}
Try it!
--Dipsacus fullonum (talk) 07:00, 14 May 2022 (UTC)

List of locations around a certain point without a certain propertyEdit

I want to modify this query:

# Select the ItemId, label and coordinate location
SELECT ?place ?placeLabel ?location WHERE {
 # Use the around service
 SERVICE wikibase:around { 
 # Looking for items with coordinate locations(P625)
 ?place wdt:P625 ?location . 
 # That are in a circle with a centre at this point
 bd:serviceParam wikibase:center "Point(-52.8118056 47.5195306)"^^geo:wktLiteral . 
 # Where the circle has a specified radius in km
 bd:serviceParam wikibase:radius "10" . 
 }
 # Use the label service to get the English label
 SERVICE wikibase:label {
 bd:serviceParam wikibase:language "en" . 
 }
}

What I want it to do is only return items that do not have a specific property, in my case, P821, set. I have tried adding this code to the query:

SELECT DISTINCT ?item WHERE {
 MINUS { 
   ?item p:P821 ?statement0.
   ?statement0 (ps:P821) _:anyValueP821.
 }
}

But it does not give me my desired result. -- Denelson83 (talk) 01:49, 12 May 2022 (UTC)

@Denelson83: Try adding FILTER NOT EXISTS {?place wdt:P821 []} to you query, like this:
# Select the ItemId, label and coordinate location
SELECT ?place ?placeLabel ?location ?p821 WHERE {
 # Use the around service
 SERVICE wikibase:around { 
 # Looking for items with coordinate locations(P625)
 ?place wdt:P625 ?location . 
 # That are in a circle with a centre at this point
 bd:serviceParam wikibase:center "Point(-52.8118056 47.5195306)"^^geo:wktLiteral . 
 # Where the circle has a specified radius in km
 bd:serviceParam wikibase:radius "10" . 
 }
 # Use the label service to get the English label
 SERVICE wikibase:label {
 bd:serviceParam wikibase:language "en" . 
 }
 FILTER NOT EXISTS {?place wdt:P821 []}
}
Try it!
--Larske (talk) 03:46, 12 May 2022 (UTC)
Looks like that works. Thank you. -- Denelson83 (talk) 03:48, 12 May 2022 (UTC)

Filter out all "Wiki" entriesEdit

Hi!

I have been trying to create a webpage that can fetch a random (popular) entry from wikimedia.

However, I'm having issues with the "Wikidata" stuff that appears/is returned from time to time.

To consider an entry popular, it needs to have mode than 40 sitelinks. And many of the wikimedia pages do! So how can I filter them out?

I have been manually adding filters one by one every time an undesired page appears, but I can't be sure that I added them all.

For example, this one did appear recently. I filtered it out by removing all entries that are "instance of Wikimedia project page (Q14204246)", but many types and stuff exists and I can't find anywhere a list of all of them.

Is there any way to filter out ALL wikimedia pages and stuff?

You can check my query here.  – The preceding unsigned comment was added by Guplem (talk • contribs) at 2022-05-13 09:22 (UTC).

I guess. MINUS { ?item wdt:P31/wdt:P279* wd:Q17379835. } seems to do the trick. Infrastruktur (talk) 09:14, 13 May 2022 (UTC)
It seems to work, but I have a couple of questions...
It looks like it is removing from the results those entries that are an instance of something that is a subclass of the "Wikimedia page outside the main knowledge tree (Q17379835)". Am I right?
What happens if the thing is not directly a subclass of that? For example: Q18711811, Q36330215, Q107344376, Q19887878 ... Guplem (talk) 12:22, 13 May 2022 (UTC)
Yup, it removes results that is an instance of a subclass of "Wikimedia page outside the main knowledge tree". Since the star (*) is used it will also include things that is an instance of a subclass of a subclass of Q17379835 and that includes the four items you listed on this page. If the item is an instance of any one of the 297 subclasses, it will remove it from the results.
The categorization of items on Wikidata can be a bit messy in places so don't be surprised if you find a few issues here and there. Infrastruktur (talk) 12:50, 13 May 2022 (UTC)
Thank you very, very much for your help and your time! Guplem (talk) 13:18, 13 May 2022 (UTC)

instance of (P31) scholarly article (Q13442814) released on a specific dateEdit

Hi, I'm looking for a query for a list of items with instance of (P31) scholarly article (Q13442814) released on a specific date and its description. I tried to use the following query but it doesn't seem to work.

SELECT DISTINCT ?item ?itemLabel ?itemDescription WHERE {
  ?item wdt:P31 wd:Q13442814.
  ?item wdt:P577 ?pubdate.
  FILTER((?pubdate >= "2021-01-01T00:00:00Z"^^xsd:dateTime) && (?pubdate <= "2021-12-31T00:00:00Z"^^xsd:dateTime))
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!

--EN-Jungwon 18:39, 13 May 2022 (UTC)

@EN-Jungwon: You wrote "released on a specific date". Use the specific date in a triple pattern instead of using a filter. e.g.:
?item wdt:P577 "2021-05-01T00:00:00Z"^^xsd:dateTime.
If you do mean a period with a from and to date, then use a rangesafe hint:
?item wdt:P577 ?pubdate. hint:Prior hint:rangeSafe true.
--Dipsacus fullonum (talk) 06:47, 14 May 2022 (UTC)

Optimize film query to return full-length videos regardless of the presence of a trailer videoEdit

Hi! For a hobby project prototyping a public domain and CC-licensed film browser, I have constructed a very simple query that retrieves films from Wikidata which have a full length video (bare video (P10) statement) but not a trailer video (video (P10) statement with object has role (P3831) film trailer (Q622550) qualifier). Example retrieving films with full-length videos (but not having any trailer videos) from Japan:

SELECT ?item ?full_length_video WHERE {
?item (wdt:P31/(wdt:P279*)) wd:Q11424.
?item wdt:P10 ?full_length_video.
?item wdt:P495 wd:Q17.
MINUS { ?item p:P10 [pq:P3831 wd:Q622550]. } 
}
Try it!

However, this query fails to return those films that have both a full-length video and a trailer video, and I would like to catch these too, preferably returning both the variables ?full_length_video and ?trailer_video (if the latter exists). This is beyond my querying skills, and I wonder if it's even possible. Would someone be interested in helping me solve this puzzle? Thanks so much in advance :D Spinster 💬 07:32, 15 May 2022 (UTC)

SELECT DISTINCT ?item ?full_length_video ?t WHERE {
  ?item (wdt:P31/(wdt:P279*)) wd:Q11424.
  ?item wdt:P10 ?full_length_video.
  ?item wdt:P495 wd:Q17.
  OPTIONAL { ?item p:P10 [pq:P3831 ?type]. }
#   BIND(IF(BOUND(?type), 'trailer (or sth)', 'full (so undefined)') AS ?t )
  BIND(IF(BOUND(?type), FALSE, TRUE) AS ?t )
  FILTER(?t)
}
Try it!
Piastu (talk) 09:35, 15 May 2022 (UTC)
Thanks for this idea, it's interesting! I would really like to be able to return full length videos and trailer videos as separate variables, if that is possible at all. The difficulty is that full length video's in most cases don't have any qualifier. Your query, however, made me discover that some video (P10) statements for full length videos have object has role (P3831) full movie available on Wikimedia Commons (Q89347362) as qualifier, and maybe a batch edit is in order to more easily distinguish between both (I can do that batch edit). If anyone has other ideas, I'd love to hear them! Spinster 💬 15:28, 15 May 2022 (UTC)
Another variant. Infrastruktur (talk) 21:03, 15 May 2022 (UTC)
Thank you! :D Spinster 💬 13:44, 16 May 2022 (UTC)

Query for British LibrariesEdit

Hi all, I was wondering if a query could be run outlining all the libraries in the UK? Thanks Jamzze (talk) 19:51, 15 May 2022 (UTC)

@Jamzze: See various queries on the homepage of User:Sic19, who's done some of the work in this area. He has various queries for libraries there, both under the section "SPARQL queries" and under "SPARQL queries for ongoing tasks", and I am sure could help you if there were any tweaks or adjustments you wanted to them. Jheald (talk) 20:51, 15 May 2022 (UTC)