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

Data matching and superset of WikiBio data set[edit]

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 collection[edit]

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 !

Please to make the river reach the sea[edit]

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)[reply]

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)[reply]
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)[reply]
Thank you, Dipsacus fullonum; your solution exactly what I was after. P403's P625 noted & agreed. --Tagishsimon (talk) 12:49, 2 May 2022 (UTC)[reply]
@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)[reply]
@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)[reply]
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)[reply]
  • 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)[reply]
@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)[reply]
@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)[reply]

@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.

Taming owls[edit]

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)[reply]

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)[reply]
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)[reply]

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)[reply]
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)[reply]

Query timeout limit reached[edit]

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)[reply]
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)[reply]
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)[reply]
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)[reply]
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)[reply]

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)[reply]

@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)[reply]
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)[reply]
@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)[reply]
Thank you so much @Dipsacus fullonum Ramu ummadishetty (talk) 12:03, 19 May 2022 (UTC)[reply]

List of locations around a certain point without a certain property[edit]

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)[reply]

@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)[reply]
Looks like that works. Thank you. -- Denelson83 (talk) 03:48, 12 May 2022 (UTC)[reply]

Filter out all "Wiki" entries[edit]

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)[reply]
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)[reply]
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)[reply]
Thank you very, very much for your help and your time! Guplem (talk) 13:18, 13 May 2022 (UTC)[reply]

instance of (P31) scholarly article (Q13442814) released on a specific date[edit]

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)[reply]

@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)[reply]

Optimize film query to return full-length videos regardless of the presence of a trailer video[edit]

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)[reply]

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)[reply]
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)[reply]
Another variant. Infrastruktur (talk) 21:03, 15 May 2022 (UTC)[reply]
Thank you! :D Spinster 💬 13:44, 16 May 2022 (UTC)[reply]

Query for British Libraries[edit]

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)[reply]

@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)[reply]

Query items with specific reference[edit]

Hello! How can I query all items that has a property that uses a specific reference? For example all items with at least one property with the reference stated in (P248) and Biografiskt lexikon för Finland (Q10429758)? Thank you! Robertsilen (talk) 12:59, 17 May 2022 (UTC)[reply]

@Robertsilen Something like this query : 
#TEMPLATE={ "template": { "en": "all items with statements using some ?ref as « stated in » value" }, "variables": { "?ref": { "query": "" } } }
select distinct ?item ?itemLabel {
  ?item wikibase:sitelinks ?links;
        ?p ?st .
   ?st prov:wasDerivedFrom/ pr:P248 ?ref .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  bind(wd:Q10429758 as ?ref)
}
:
Try it!
You can use the following link https://w.wiki/5B4F , that allows to interactively chose a reference using the query service « #TEMPLATE » feature. author  TomT0m / talk page 13:12, 17 May 2022 (UTC)[reply]
@Robertsilen:
SELECT ?item ?itemLabel ?property ?propertyLabel ?statement
WHERE
{
  ?reference pr:P248 wd:Q10429758 .
  ?statement prov:wasDerivedFrom ?reference .
  ?item ?claim ?statement .
  ?property wikibase:claim ?claim .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],sv,fi,en". }
}
Try it!
--Dipsacus fullonum (talk) 13:14, 17 May 2022 (UTC)[reply]

i'm interested in getting voting mechanisms and systems extracted from wikidata[edit]

hi - i work for an open source company aragon.org and we provide voting solutions for blockchain-based companies to help manage their business decisions and processes. we are hoping to do a more exhaustive search of voting mechanisms to begin sorting our current software offerings and work towards extending

some current voting mechanisms we use

simple majority super majority delegated weighted majority anonymized voting

and then there some more complex = i'm not sure whether they've been added in to wikidata yet, and we would like some advice adding content to make sure we meet expectations and standards

examples include

quadratic voting conviction voting etc

so finding all the voting systems as understood in wikidata would be a great starting point? maybe voting mechanisms by region?

thanks for engaging in discussion, we're new in the space and have a lot to learn = looking forward to it :)

This seems to be quite a mess in Wikidata right now as « voting system » are mixed up with electoral division.
So to find the « electoral systems » as currently known by Wikidata I had to exclude electoral divisions. It also seem to be mixed up with voting system even if we may vote for something else than elections).
The following query uses these:
  • Properties: instance of (P31)  View with Reasonator View with SQID, subclass of (P279)  View with Reasonator View with SQID
    select ?system ?type {
      ?system wdt:P31/wdt:P279* wd:Q182985 ;
              wdt:P31 ?type .
      filter not exists {
        ?system wdt:P31/wdt:P279* ?circ  .
                values ?circ { <http://www.wikidata.org/entity/Q17166756>  wd:Q192611	wd:Q27971968 }
                
      }
    }
    
The datas and the datamodel seem to be quite messy at this point. author  TomT0m / talk page 18:23, 19 May 2022 (UTC)[reply]

Query to count enwiki talk pages which have... and do not have...[edit]

I would like to query taxa having both an enwiki page and an AFD-id in wikidatain order to count the talk pages which indicate that the taxon is considered part of the Australian Biota project, and those that have not. An example of a Qitem having an enwiki page, an afd-id and which is part of the Australian biota project is: Enteles vigorsii (Q107221668) with talk page. MargaretRDonald (talk) 01:57, 20 May 2022 (UTC)[reply]

An additional query which just finds the taxa with an enwiki page and AFD-id and which have talk pages which do not assign the page to the Australian biota project would also be useful to allow us to remedy the problem... MargaretRDonald (talk) 02:27, 20 May 2022 (UTC)[reply]

@MargaretRDonald: I don't think that is possible using the Wikidata Query Service. Information about talk pages in the English Wikipedia would have to come from an MWAPI call to the Wikipedia, and MWAPI have a hard limit for max 5000 results. but there are 20000+ article talk pages in category en:Category:WikiProject Australian biota articles. --Dipsacus fullonum (talk) 03:23, 20 May 2022 (UTC)[reply]
Thanks, @Dipsacus fullonum: Good to ask and great to get a response. MargaretRDonald (talk) 05:47, 20 May 2022 (UTC)[reply]

Query to list English, French and German official and common names of a given species[edit]

I would like to produce a list that contains the English, French and German official AND common names of species (plant, animal ...) when the species' latin name are typed in. Is that possible?

Like:

Jaculus orientalis [Latin] > Ägyptische Springmaus [German official name - that is the wikipedia name of the article] > Große Wüstenspringmaus [German common name] > greater Egyptian jerboa [English official, i.e. wikipedia name of the article] > jerboa [English common] > Grande gerboise d'Égypte [French official, i.e. wikipedia name of the article] > gerboise [French common]

It must be some code (but unfortunately I'm not a coder ...) like this:

SELECT DISTINCT ?item ?itemLabel WHERE {

 SERVICE wikibase:label { bd:serviceParam wikibase:language "fr". }
 {
   SELECT DISTINCT ?item WHERE {
     ?item p:P225 ?statement0.
     ?statement0 (ps:P225) "Jaculus orientalis".
   }
   LIMIT 100
 }

}

but instead of just the French official name the other variants should be added. Also I don't know how to query for more than one latin name.

I'm really thankful for any help!

Filter objects that are "on" a beach/close to a beach/a see?[edit]

For instance, how to find objects such as Hoek van Holland Strand metro station (Q29058738) ? Bouzinac💬✒️💛 08:49, 20 May 2022 (UTC)[reply]

Here's an example comparing the distance between any beach and metro in France. Note that the working set get big fast, in this case it is 400 * 500 ~= 200 000 before filtering, this gets reduced to 7 that is within walking distance. Had to compare against beaches since seas can be too be to big to compare coordinates against. Infrastruktur (talk) 13:02, 20 May 2022 (UTC)[reply]
Nice ! Would such a statement https://www.wikidata.org/wiki/Q3183137#P206 be correct ? Bouzinac💬✒️💛 13:20, 20 May 2022 (UTC)[reply]
I found one metro in France and two in the Netherlands that used located in or next to body of water (P206). I don't know how close they will have to be to the water. Infrastruktur (talk) 13:42, 20 May 2022 (UTC)[reply]
It is obviously subjective but a reasonable walking distance should be fine. Trying to fix that, so that a quick query can be feasible using P206 Bouzinac💬✒️💛 14:01, 20 May 2022 (UTC)[reply]
SELECT ?beach ?beachLabel ?metro ?metroLabel ?dist
WHERE {
  {
    SELECT ?beach ?metro ?dist
    WHERE {
      ?beach wdt:P31/wdt:P279* wd:Q40080;
        wdt:P17 wd:Q142;
        wdt:P625 ?beachLoc .
      ?metro wdt:P31/wdt:P279* wd:Q928830;
        wdt:P17 wd:Q142;
        wdt:P625 ?metroLoc .
      BIND(geof:distance(?beachLoc, ?metroLoc) * 1000 as ?dist)
      FILTER(?dist <= 2000)
    }
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "fr, en" . }
}
ORDER BY ASC(?dist)
Try it!
@Bouzinac, Infrastruktur: I had expected that you could speed up the query by assuming that beach and metro are in the same administrative entity (P:P131), but adding
     ?beach wdt:P131 ?admEnt.
     ?metro wdt:P131 ?admEnt.
reduced the number of results from 7 to 1 because the beach and metro items have values for P131 at different administrative levels. --Dipsacus fullonum (talk) 15:35, 20 May 2022 (UTC)[reply]
I am quite interested in optimization. Not sure there's any clever tricks to reduce the algorithmic complexity from O(m*n), but a script could sort by longitude and latitude and eliminate entire ranges that way, that would certainly help if there was a lot of data. Infrastruktur (talk) 16:03, 20 May 2022 (UTC)[reply]