User:Ijon/Queries

From Wikidata
Jump to navigation Jump to search

Queries tutorials[edit]

Asaf's Query examples[edit]

Breakdown of item types (instance of) for a given country (Israel, here)[edit]

The following query uses these:

  • Properties: country (P17)  View with Reasonator View with SQID, instance of (P31)  View with Reasonator View with SQID
    # breakdown of item types (instance of) for a given country (Israel, here)
    SELECT ?instanceLabel (COUNT(?item) as ?count) 
    WHERE { 
      ?item wdt:P17 wd:Q801 . 
      ?item wdt:P31 ?instance . 
      ?instance rdfs:label ?instanceLabel  
      FILTER(LANG(?instanceLabel) = "he")
    }
    GROUP BY ?instanceLabel
    ORDER BY DESC(?count)
    LIMIT 30
    

Albums known to be from a certain country (Israel, here)[edit]

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID, subclass of (P279)  View with Reasonator View with SQID, country of origin (P495)  View with Reasonator View with SQID, performer (P175)  View with Reasonator View with SQID, genre (P136)  View with Reasonator View with SQID
    # Albums known to be from Israel
    SELECT ?item ?itemLabel ?performerLabel ?genreLabel ?depth
    WHERE {
        ?item (wdt:P31/wdt:P279*) wd:Q482994;
              wdt:P495 wd:Q801;
              wikibase:statements ?depth.
      OPTIONAL {
        ?item wdt:P175 ?performer.
      }
      OPTIONAL {
        ?item wdt:P136 ?genre.
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],he". }
      }
    

Ukrainian politicians by birth oblast[edit]

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID, occupation (P106)  View with Reasonator View with SQID, country of citizenship (P27)  View with Reasonator View with SQID, place of birth (P19)  View with Reasonator View with SQID, located in the administrative territorial entity (P131)  View with Reasonator View with SQID, country (P17)  View with Reasonator View with SQID
    # Ukrainian politicians by birth oblast
    SELECT ?oblastLabel (COUNT(?thing) as ?total)
    WHERE 
    {
      ?thing wdt:P31 wd:Q5 . # human
      ?thing wdt:P106 wd:Q82955 . # politician
      ?thing wdt:P27 wd:Q212 . # Ukrainian citizen
      ?thing wdt:P19 ?place . # who has *some* place of birth
      ?place wdt:P131 ?oblast . # where the place belongs to an administrative entity
      ?oblast wdt:P17 wd:Q212 . # which is in Ukraine
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],uk,ru,pl,be,cs,bg,sr,en". }
    }
    GROUP BY ?oblastLabel
    ORDER BY DESC(?total)
    

People born somewhere in Zhytomyr oblast, with fallback languages marked[edit]

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID, place of birth (P19)  View with Reasonator View with SQID, located in the administrative territorial entity (P131)  View with Reasonator View with SQID
    # People born somewhere in Zhytomyr oblast, with fallback languages marked
    SELECT ?shtuka ?shtukaLabel (LANG(?shtukaLabel) as ?lang) 
    WHERE 
    {
      { ?shtuka wdt:P31 wd:Q5 . # all the humans
      ?shtuka wdt:P19 ?place . # who have a place of birth
      
      ?place wdt:P131 wd:Q40637 .} # which is in Zhytomyr oblast
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],ru,pl,be,cs,bg,sr,en". }
    }
    

Editors-in-chief and their employers[edit]

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID, position held (P39)  View with Reasonator View with SQID, employer (P108)  View with Reasonator View with SQID
    # Editors-in-chief and their employers
    SELECT ?editor ?editorLabel ?empLabel
    WHERE 
    {
      ?editor wdt:P31 wd:Q5.
      ?editor wdt:P39 wd:Q589298 . # holding editor-in-chief positions
      OPTIONAL {
                ?editor p:P39 ?prop .
               ?prop ps:P39 wd:Q589298 .
               ?prop pq:P108 ?emp . }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    

Best-described items in Te Papa museum[edit]

The following query uses these:

  • Properties: collection (P195)  View with Reasonator View with SQID, location (P276)  View with Reasonator View with SQID, instance of (P31)  View with Reasonator View with SQID
    # best-described items in Te Papa museum
    SELECT DISTINCT ?q ?stcount WHERE {
      { ?q wdt:P195 wd:Q915603 .} # in Te Papa's collection 
      UNION 
      { ?q wdt:P276 wd:Q915603 .} # or "located in" Te Papa }
      # ?q wdt:P31 wd:Q3305213 . # limit to paintings (to not timeout)
      ?q wikibase:statements ?stcount .
    } GROUP BY ?q ?stcount
    ORDER BY DESC(?stcount)
    

Most-used properties to describe poems[edit]

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID
    SELECT ?prop ?propLabel ?count 
    WHERE 
    {
        {
            SELECT ?prop (COUNT(DISTINCT ?item) AS ?count) WHERE {
               
               hint:Query hint:optimizer "None" .
               ?item wdt:P31 wd:Q5185279 .
               ?item ?p ?id .
               ?prop wikibase:directClaim ?p .
            }  GROUP BY ?prop
        }
      
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "en" .
        }
    } ORDER BY DESC (?count)
    

Australian women covered in other Wikipedias but not in English[edit]

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID, sex or gender (P21)  View with Reasonator View with SQID, country of citizenship (P27)  View with Reasonator View with SQID
    # Australian women covered in other Wikipedias but not in English
    SELECT ?person ?personLabel ?personDescription (COUNT(DISTINCT ?sitelink) as ?linkcount) WHERE {
      ?person wdt:P31 wd:Q5 . # human
      ?person wdt:P21 wd:Q6581072 . # woman
      ?person wdt:P27 wd:Q408 . # Australian
      ?sitelink schema:about ?person . # get Wikimedia pages about the person
      FILTER NOT EXISTS {
        ?links schema:about ?person . # get Wikimedia pages about the person
        ?links schema:isPartOf <https://en.wikipedia.org/> . # but exclude English Wikipedia.
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    } GROUP BY ?person ?personLabel ?personDescription
    ORDER BY DESC(?linkcount) 
    LIMIT 20
    

People who won the Nobel Prize more than once[edit]

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID, award received (P166)  View with Reasonator View with SQID
    # People who won the Nobel Prize more than once
    SELECT ?winnerLabel ?wins
    {
      {
        SELECT ?winner (COUNT(?prize) AS ?wins)
        WHERE  
        { 
          ?winner wdt:P31 wd:Q5 . # human
          ?winner wdt:P166 ?prize . # who has won some prize
          ?prize wdt:P31 wd:Q7191 . # and where that prize is a kind of Nobel prize
        }
        GROUP BY ?winner
        ORDER BY DESC(?wins)
      }
      FILTER( ?wins > 1)
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    

Population timeline of a city[edit]

The following query uses these:

  • Properties: population (P1082)  View with Reasonator View with SQID, point in time (P585)  View with Reasonator View with SQID
    #defaultView:Timeline
    #title: Timeline of population size of Jerusalem, Israel
    SELECT ?pop ?time WHERE {
      wd:Q1218 p:P1082 [ ps:P1082 ?pop ; pq:P585 ?time ] .
    }
    

Model items by type[edit]

The following query uses these:

  • Properties: model item (P5869)  View with Reasonator View with SQID
    # model items
    SELECT ?conceptLabel ?item ?itemLabel 
    WHERE 
    {
      ?concept wdt:P5869 ?item.
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    } ORDER BY ?conceptLabel
    

Most important missing item labels in a language[edit]

(The Twi language in this example)

The following query uses these:

  • Properties: instance of (P31)  View with Reasonator View with SQID, subclass of (P279)  View with Reasonator View with SQID
    # 100 most significant items missing labels in Twi 
    # (change the "tw" below to another language code to get the same query for another language)
    
    SELECT ?item ?sitelinks WHERE {
      ?item wikibase:sitelinks ?sitelinks. hint:Prior hint:rangeSafe true.
      FILTER(?sitelinks >= 100) # use at-least-100-sitelinks as an indicator of significance
    
      MINUS { ?item wdt:P31/wdt:P279* wd:Q58494026} # exclude Wikimedia internal items (categories, templates, etc.)
    
      MINUS { ?item rdfs:label ?label FILTER(LANG(?label) = "tw") } # <-- change language here
    
    } ORDER BY DESC(?sitelinks)
    LIMIT 100
    

Queries Asaf is too lazy to paste here[edit]

Other people's queries to stealadapt[edit]