Open main menu

User:Charles Matthews/Queries

Vanilla SPARQLEdit

  • #Prime ministers of the UK
      SELECT ?item WHERE {
      ?item wdt:P39 wd:Q14211 .
    }
    
    Try it!
  • #Women educated at Girton College
    SELECT ?item WHERE {
      ?item wdt:P31 wd:Q5 .
      ?item wdt:P21 wd:Q6581072 .
      ?item wdt:P69 wd:Q797846
      }
    
    Try it!
  • #Family name and disambiguation page
    SELECT DISTINCT ?item ?itemLabel
      WHERE {?item wdt:P31 wd:Q101352 .
            ?item wdt:P31 wd:Q4167410 .
          SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
          }
    
    Try it!
  • #Instances of list
    SELECT DISTINCT ?item ?itemLabel ?thing1 ?thing1Label
      WHERE 
       { ?item wdt:P31 ?thing.
         ?thing wdt:P31 wd:Q13406463.
         ?thing wdt:P360 ?thing1.
          SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
        }
    
    Try it!
  • #Art museums without country, labels
    SELECT ?item ?itemLabel WHERE {
      ?item wdt:P31 wd:Q207694 .
      OPTIONAL { ?item wdt:P17 ?dummy0 }
      FILTER(!bound(?dummy0))
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • #Disambiguation page where there should be a family name
      SELECT DISTINCT ?item ?surnameLabel
      WHERE {?item wdt:P31 wd:Q5 .
            ?item wdt:P734 ?surname .
            ?surname wdt:P31 wd:Q4167410 .
          SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
          }
    
    Try it!
  • #Software on the Internet Archive
    SELECT ?item ?itemLabel ?archiveID ?image
    {
    	?item wdt:P724 ?archiveID .
        ?item wdt:P31/wdt:P279* wd:Q7397.
        OPTIONAL {?item wdt:P18 ?image}.
    	SERVICE wikibase:label { bd:serviceParam wikibase:language "en,en"  }    
    }
    
    Try it!
  • #Software reading STL files
    SELECT DISTINCT ?app ?appLabel ?licLabel ?logo WHERE {
      ?app (wdt:P31/wdt:P279*) wd:Q7397.
      ?app wdt:P1072 wd:Q1238229.
      OPTIONAL {?app wdt:P275 ?lic.}
      OPTIONAL {?app wdt:P154 ?logo.}
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!
  • #Families with OBIN, no "has part", labels
    SELECT ?item ?itemLabel
    WHERE {
      ?item wdt:P31 wd:Q8436 .
      ?item wdt:P1415 ?dummy0 .
      OPTIONAL { ?item wdt:P527 ?dummy1 }
      FILTER(!bound(?dummy1))
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • #Finding items without "instance of", per property, accepting some substitutes
    SELECT DISTINCT ?item ?itemLabel
      WHERE 
          {?item wdt:P1343 wd:Q2657718 .
            FILTER NOT EXISTS {?item wdt:P31 [ ]}
            FILTER NOT EXISTS {?item wdt:P279 [ ]}
            FILTER NOT EXISTS {?item wdt:P361 [ ]}
            FILTER NOT EXISTS {?item wdt:P2445 [ ]}
           SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
           }
    
    Try it!
  • #Persons in the History of Parliament, Legacies of Slavery and Kindred Britain
    SELECT DISTINCT ?item ?itemLabel ?dummy
      WHERE {?item wdt:P1614 [ ].
             ?item wdt:P3023 [ ].
             ?item wdt:P3051 ?dummy.
            
             SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
            }
    
    Try it!
  • Try it!
  • #Official languages (Popov)
    SELECT ?countryLabel ?country2 ?country3 ?wikilang ?languageLabel ?language2 ?language3
    WHERE 
    {
      ?country wdt:P31 wd:Q6256.
      ?country wdt:P37 ?language.
      OPTIONAL {
        # ISO 3166-1 alpha-2 code
        ?country wdt:P297 ?country2.
      }
      OPTIONAL {
        # ISO 3166-1 alpha-3 code
        ?country wdt:P298 ?country3.
      }
      OPTIONAL {
        # ISO 639-1 code
        ?language wdt:P218 ?language2.
      }
      OPTIONAL {
        # ISO 639-3 code
        ?language wdt:P220 ?language3.
      }
      OPTIONAL {
        # Wikimedia language code
        ?language wdt:P424 ?wikilang.
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    
    Try it!

Seeing round the cornerEdit

  • Described by source that is a part of the Victoria County History
SELECT ?item ?itemLabel ?ref ?refLabel WHERE {

  ?item wdt:P1343 ?ref .
  ?ref wdt:P361? wd:Q7926668
           
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}

Try it!

  • #Main subjects of scientific papers
      #Now has performance problems
      SELECT ?item ?itemLabel ?subject ?subjectLabel WHERE {
      ?item wdt:P31 wd:Q13442814 .
      ?item wdt:P921 ?subject .
       SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • #Members of institutions having legislative terms
    SELECT ?person ?personLabel
    WHERE { ?person wdt:P463 ?inst.
           ?inst wdt:P31 ?term.
           ?term wdt:P279* wd:Q15238777 .
    
     SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

SPARQL featuresEdit

Handling subclassesEdit

  • Subclasses of "literary work"
SELECT ?s ?sLabel
WHERE
{
  ?s wdt:P279 wd:Q7725634 .
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
 }

Try it!

SELECT ?item WHERE {
    ?item p:P31/ps:P31/wdt:P279* wd:Q12280.  # Item's type is: bridge or sub-type or sub-sub-type/etc
}

Try it!

  • Units of measurement
SELECT ?q ?qLabel {
  ?q wdt:P31|wdt:P279* wd:Q47574 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  }

Try it!

  • Libraries and subclasses, map
#defaultView:Map
SELECT ?library ?coord
WHERE {
  ?library p:P31/ps:P31/wdt:P279* wd:Q12280 .
  
  OPTIONAL { ?library wdt:P625 ?coord }
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

  • Software titles by license name and license type:
SELECT DISTINCT ?app ?appLabel ?licenseLabel ?classLabel
WHERE {
  ?app (p:P31/ps:P31/wdt:P279*) wd:Q7397.
  ?app wdt:P275 ?license.
  ?license wdt:P279 ?class

  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}

Try it!

  • Comparison of two modes of "all subclasses"
SELECT ?item ?itemLabel WHERE {
  ?item p:P31/ps:P31/wdt:P279* wd:Q12280.
  FILTER NOT EXISTS { ?item wdt:P31/wdt:P279* wd:Q12280. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

Regex etc. string operationsEdit

  • UK placenames beginning in Llan-
#defaultView:Map
SELECT ?item ?itemLabel ?coord
WHERE 
{
	?item wdt:P31/wdt:P279* wd:Q486972;
    	      wdt:P17 wd:Q145;
    	      rdfs:label ?itemLabel;
    	      wdt:P625 ?coord;
    	 FILTER (lang(?itemLabel) = "en") . 
         FILTER(STRSTARTS(?itemLabel, 'Llan')).
}

Try it!

  • UK placenames ending in -by
#defaultView:Map
SELECT ?item ?itemLabel ?coord
WHERE 
{
	?item wdt:P31/wdt:P279* wd:Q486972;
    	      wdt:P17 wd:Q145;
    	      rdfs:label ?itemLabel;
    	      wdt:P625 ?coord;
    	 FILTER (lang(?itemLabel) = "en") . 
    	 FILTER regex (?itemLabel, "(by)$").
}

Try it!

  • SUBSTR with schema.org
SELECT ?item WHERE {
  ?item wdt:P1415 ?dummy0 .
  OPTIONAL {
    ?wiki0 <http://schema.org/about> ?item .
    FILTER(SUBSTR(STR(?wiki0),1,24) = 'https://en.wikipedia.org') .
  }
  FILTER(!bound(?wiki0))
  ?item wdt:P106 wd:Q6625963 .
  ?item wdt:P21 wd:Q6581072 .
}

Try it!

  • Abbreviated systematic name by regex
SELECT ?q ?name { ?q wdt:P225 ?name FILTER REGEX(?name,'^O\\S+ bicolor$') }

Try it!

  • Airport codes with regex check for three letters
SELECT ?item ?itemLabel ?dummy0Label
WHERE {
  ?item wdt:P238 ?dummy0 .
  FILTER regex (?dummy0, "[A-Z]{3}")
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

  • Given names ending in "ko", English label
SELECT ?item ?itemLabel 
WHERE {?item wdt:P31 wd:Q202444; wdt:P1705 ?itemLabel .

#FILTER (lang(?itemLabel) = "en") .
FILTER regex (?itemLabel, "ko$") .
       }

Try it!

  • Female given names ending in "o", label in any language
SELECT ?item ?itemLabel 
WHERE {?item wdt:P31 wd:Q11879590; wdt:P1705 ?itemLabel .

#FILTER (lang(?itemLabel) = "mul") .
FILTER regex (?itemLabel, "o$") .
       }

Try it!

  • Scientific paper titles ending in "virus"
SELECT ?item ?itemLabel ?subject ?subjectLabel ?title 
WHERE {
  ?item wdt:P31 wd:Q13442814 .
  ?item wdt:P921 ?subject .
  ?item wdt:P1476 ?title .
  FILTER regex (?title, "(virus)$").
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

  • Scientific paper titles including "virus"
SELECT ?item ?itemLabel ?title 
WHERE {
  ?item wdt:P31 wd:Q13442814 .
  ?item wdt:P1476 ?title .
  FILTER regex(str(?title), "virus")
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

  • #ICD9 handling
    SELECT ?item ?itemLabel ?icd ?icd2 ?icd3
    WHERE {
     ?item wdt:P31 wd:Q12136 .
      ?item wdt:P493 ?icd  .
      BIND(SUBSTR(?icd,1,3) AS ?icd1)
      BIND(CONCAT("1",?icd1) AS ?icd2)
      BIND(xsd:integer(?icd2) AS ?icd3)
      FILTER(1000 < ?icd3 && ?icd3 <1139)
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
          }
    
    Try it!

MapsEdit

  • #defaultView:Map
    #Libraries of the world
    SELECT ?library ?coord
    WHERE {
      ?library wdt:P31 wd:Q7075 .
      
      OPTIONAL { ?library wdt:P625 ?coord }
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • #defaultView:Map
    #Rembrandts
    SELECT ?painting ?paintingLabel ?collection ?collectionLabel ?image ?coord
    WHERE {
      ?painting wdt:P31 wd:Q3305213 .
      ?painting wdt:P170 wd:Q5598 .
      ?painting wdt:P195 ?collection .
      
      OPTIONAL { ?collection wdt:P625 ?coord }
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • Jackson Pollocks
#defaultView:Map
SELECT ?painting ?paintingLabel ?collection ?collectionLabel ?image ?coord
WHERE {
  ?painting wdt:P31 wd:Q3305213 .
  ?painting wdt:P170 wd:Q37571 .
  ?painting wdt:P195 ?collection .
  
  OPTIONAL { ?collection wdt:P625 ?coord }
  
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

  • #Locations of paintings by RAs
    #defaultView:Map
    SELECT DISTINCT ?painter ?painterLabel ?item ?itemLabel ?location ?locationLabel ?coords WHERE {
        ?item wdt:P31/wdt:P279* wd:Q3305213 .
        ?item wdt:P170 ?painter .
        ?painter wdt:P463 wd:Q270920 .
        ?item wdt:P276 ?location .
        ?location wdt:P625 ?coords .
    #    ?location wdt:P131* wd:Q145 .
        OPTIONAL {?item wdt:P18 ?image} .
        SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    
    Try it!
  • Birthplaces of SMPs
SELECT DISTINCT ?person ?personLabel ?born ?bornLabel ?borncountryLabel ?coord {
  ?position wdt:P279* wd:Q1711695 .
  ?person wdt:P39 ?position .
  ?person wdt:P19 ?born .
  ?born wdt:P17 ?borncountry .
  ?born wdt:P625 ?coord .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
#defaultView:Map

Try it!

  • #Airport names
    #defaultView:Map{"layer":"?gender"}
    SELECT ?airport ?airportLabel ?location ?person ?personLabel ?gender WHERE {
      ?airport wdt:P31/wdt:P279* wd:Q1248784;
                wdt:P625 ?location;
                wdt:P138 ?person.
      ?person wdt:P31 wd:Q5.
      OPTIONAL {
        ?person wdt:P21/wdt:P279* wd:Q6581072.
        BIND("female"@en AS ?female)
      }
      OPTIONAL {
        ?person wdt:P21/wdt:P279* wd:Q6581097.
        BIND("male"@en AS ?male)
      }
      OPTIONAL {
        ?person wdt:P21 ?gender.
        MINUS { ?gender wdt:P279* wd:Q6581097. }
        MINUS { ?gender wdt:P279* wd:Q6581072. }
        FILTER(!ISBLANK(?gender))
        BIND("other"@en AS ?other)
      }
      BIND(COALESCE(?female, ?male, ?other, "unknown"@en) AS ?gender)
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    
    Try it!
  • Latin American women missing on esWP
  • #defaultView:Map
    #Newspapers, USA
    SELECT DISTINCT ?item ?itemLabel ?place ?placeLabel ?id ?coords ?article ?mapflags ?rgb WHERE {
     ?c wdt:P279* wd:Q11032 .
      ?item wdt:P31 ?c .
     ?item wdt:P17
    
    Try it!
  • #Jason Evans, DWB articles birthplaces
    #defaultView:Map{"layer":"?typeLabel"}
    
    SELECT  ?item ?itemLabel ?coords ?pic ?genderLabel ?type ?typeLabel
    where {
      ?item wdt:P1648 ?biog .
      ?item wdt:P19 ?location .
    ?location wdt:P625 ?coords . 
      ?item wdt:P21 ?gender .
      ?item wdt:P106 ?type .
      OPTIONAL { ?item wdt:P18 ?pic }
    
     
      
     
     SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
    } 
    *[https://query.wikidata.org/embed.html#%23defaultView%3AMap%7B%22hide%22%3A%5B%22%3Fcoordinates%22%2C%20%22%3Fline%22%2C%20%22%3Frgb%22%5D%7D%0ASELECT%20DISTINCT%20%3Fcoordinates%20%3Fline%20%3Fitem%20%3FitemLabel%20%3FconnectLabel%20%3Fimage%20%3Fopening%20%3Frgb%0A%0AWITH%20%7B%0A%20%20SELECT%20%3Fitem%20(SAMPLE(%3Fcoordinates)%20AS%20%3Fcoordinates)%20(SAMPLE(%3Fimage)%20AS%20%3Fimage)%20(sample(%3Flat1)%20as%20%3Flat1)%20(sample(%3Flon1)%20as%20%3Flon1)%20(sample(%3Fopening)%20as%20%3Fopening)%20WHERE%20%7B%0A%20%20%20%20%3Fitem%20wdt%3AP31%20wd%3AQ14562709%20.%0A%20%20%20%20%3Fitem%20wdt%3AP625%20%3Fcoordinates%20%20.%0A%20%20%20%20%3Fitem%20p%3AP625%20%2F%20psv%3AP625%20%2F%20wikibase%3AgeoLatitude%20%3Flat1%20.%0A%20%20%20%20%3Fitem%20p%3AP625%20%2F%20psv%3AP625%20%2F%20wikibase%3AgeoLongitude%20%3Flon1%20.%0A%20%20%20%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP18%20%3Fimage%20%7D.%0A%20%20%20%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP1619%20%3Fopening%20%7D.%0A%20%20%7D%20GROUP%20BY%20%3Fitem%0A%7D%20AS%20%25stations%0A%0AWITH%20%7B%0A%20%20SELECT%20%3Fnextstation%20(sample(%3Flat2)%20as%20%3Flat2)%20(sample(%3Flon2)%20as%20%3Flon2)%20WHERE%20%7B%0A%20%20%20%20%3Fnextstation%20wdt%3AP31%20wd%3AQ14562709%20.%0A%20%20%20%20%3Fnextstation%20p%3AP625%20%2F%20psv%3AP625%20%2F%20wikibase%3AgeoLatitude%20%3Flat2%20.%0A%20%20%20%20%3Fnextstation%20p%3AP625%20%2F%20psv%3AP625%20%2F%20wikibase%3AgeoLongitude%20%3Flon2%20.%0A%20%20%7D%20GROUP%20BY%20%3Fnextstation%0A%7D%20AS%20%25nextstations%0A%0AWITH%20%7B%0A%20%20SELECT%20%3Fline%20%3Fconnect%20%3Frgb%20WHERE%20%7B%0A%20%20%20%20INCLUDE%20%25stations%20.%0A%20%20%20%20INCLUDE%20%25nextstations%20.%0A%20%20%20%20%3Fitem%20p%3AP197%20%3Fnextstationstatement%20.%0A%20%20%20%20%3Fnextstationstatement%20ps%3AP197%20%3Fnextstation%20.%0A%20%20%20%20%3Fnextstationstatement%20pq%3AP81%20%3Fconnect%20.%20%3Fconnect%20wdt%3AP361%20wd%3AQ20075%20%20.%20%0A%20%20%20%20%3Fconnect%20wdt%3AP465%20%3Frgb%20.%0A%20%20%20%20FILTER(STR(%3Fitem)%20%3C%20STR(%3Fnextstation))%20.%20%20%20%20%0A%20%20%20%20BIND(CONCAT('LINESTRING%20('%2C%20STR(%3Flon1)%2C%20'%20'%2C%20STR(%3Flat1)%2C%20'%2C'%2C%20STR(%3Flon2)%2C%20'%20'%2C%20STR(%3Flat2)%2C%20')')%20AS%20%3Fstr)%20.%0A%20%20%20%20BIND(STRDT(%3Fstr%2C%20geo%3AwktLiteral)%20AS%20%3Fline)%20%0A%20%20%7D%0A%7D%20AS%20%25lines%20%0A%20%0AWHERE%20%7B%0A%20%20%7B%20INCLUDE%20%25stations%20%7D%20%20UNION%20%7B%20INCLUDE%20%25lines%20%20%7D%20.%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D 
    GROUP BY ?item ?itemLabel ?coords ?pic ?genderLabel ?type ?typeLabel
    
    Try it! Variant map default view, London Underground IRL map]

Graph viewEdit

  • #defaultView:Graph
    #Subclasses of operating system
    SELECT DISTINCT ?os ?osLabel ?subclassOf WHERE {
      ?os wdt:P279* wd:Q9135 .
      OPTIONAL { ?os wdt:P279 ?subclassOf } .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    ORDER BY ?osLabel
    
    Try it!
  • #Lexemes connected
    #defaultView:Graph
    SELECT ?lexeme ?lexemeLabel ?target ?targetLabel WHERE {
      ?lexeme wdt:P5191 ?target; wikibase:lemma ?lexemeLabel.
      ?target wdt:P5191* wd:L2087; wikibase:lemma ?targetLabel.
    
    Try it!

Graph patternsEdit

  • # Presidents with identified mythical ancestors
    
    SELECT distinct ?person ?personLabel ?ancestor ?ancestorLabel ?born ?died ?typeLabel WHERE {
     
      ?person p:P39 ?positionStatement .
      ?positionStatement ps:P39 wd:Q11696 .  # this person was President of the USA
      { ?person (wdt:P25|wdt:P22)* ?ancestor . } # their mother or father, or their mother or father, and so on
      filter not exists { ?ancestor wdt:P31 wd:Q5 } . # the ancestor is not "a human"
      ?ancestor wdt:P31 ?type .
      ?person wdt:P31 wd:Q5 . # the original person must be a human, ie not fictional
      OPTIONAL { ?ancestor wdt:P569 ?born } .
      OPTIONAL { ?ancestor wdt:P570 ?died } .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

Bar chart viewEdit

  • #defaultView:BarChart
    #From https://scientometrics2017.ai.wu.ac.at/wp-content/uploads/2017/06/Nielsen2017Scholia_slides
    # Inspired from LEGOLAS - http://abel.lis.illinois.edu/legolas/
    # Shubhanshu Mishra, Vetle Torvik
    select ?year (count(?work) as ?number_of_publications) ?role where {
      {
        select (str(?year_) as ?year) (0 as ?pages) ("_" as ?role) where {
          # default values = 0
          ?year_item wdt:P31 wd:Q577 . 
          ?year_item wdt:P585 ?date .
          bind(year(?date) as ?year_)
          {
            select (min(?year_) as ?earliest_year)  (max(?year_) as ?latest_year) where {
              ?work wdt:P50 wd:Q20980928 .
              ?work wdt:P577 ?publication_date . 
              bind(year(?publication_date) as ?year_)
            }
          }
          bind(year(now())+1 as ?next_year)
          filter (?year_ >= ?earliest_year && ?year_ <= ?latest_year)
        }
      }
      union {
      {
        select ?work (min(?years) as ?year) (count(?coauthors) as ?number_of_authors) ?author_number where {
          ?work (p:P50|p:P2093) ?author_statement . 
          ?author_statement ps:P50 wd:Q20980928 .
          optional { ?author_statement pq:P1545 ?author_number . }
          ?work (wdt:P50
    
    Try it!

Timeline viewEdit

  • #defaultView:Timeline
    select ?species ?speciesLabel ?date ?image {
      ?species wdt:P171* wd:Q9482 .
      ?species wdt:P105 wd:Q7432 .
      OPTIONAL { ?species wdt:P18 ?image }
      ?species p:P225 ?td .
      ?td pq:P574 ?date .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
    }
    
    Try it!
  • #Timeline of theologians active in Berlin
    #defaultView:Timeline
    SELECT ?theologian ?theologianLabel ?date_of_birth ?image WHERE {
      ?theologian wdt:P106 wd:Q1234713.
      ?theologian wdt:P937 wd:Q64.
      OPTIONAL { ?theologian wdt:P569 ?date_of_birth. }
      OPTIONAL { ?theologian wdt:P18 ?image. }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    
    Try it!
  • #Balzac timeline
    #defaultView:Timeline
    SELECT DISTINCT ?t ?ppLabel ?valueLabel ?rrLabel  ?image
    WHERE
    {
      {wd:Q9711 ?prop ?t FILTER (datatype(?t)=xsd:dateTime) .
       ?value wikibase:directClaim ?prop. }
      UNION
      {
       wd:Q9711 ?q ?statement  FILTER regex (STR(?q),"prop/P") .
       ?statement ?p ?t FILTER regex (STR(?p), "prop/qualifier/P") FILTER (datatype(?t)=xsd:dateTime) .
       ?statement ?r ?value FILTER regex (STR(?r),"prop/statement/") .
       BIND(IRI(CONCAT(CONCAT(SUBSTR(STR(?r),1,29),"direct/"),SUBSTR(STR(?r),40))) as ?rprop)  
       ?rr wikibase:directClaim ?rprop .  
       BIND(IRI(CONCAT(CONCAT(SUBSTR(STR(?p),1,29),"direct/"),SUBSTR(STR(?p),40))) as ?pprop)  
        ?pp  wikibase:directClaim ?pprop .
        OPTIONAL {?value wdt:P18 ?image}
      }
      UNION
      {
       wd:Q9711 wdt:P800 ?value .
       ?value ?prop ?t FILTER (datatype(?t)=xsd:dateTime) .
       ?rr wikibase:directClaim wdt:P800 .
       ?pp wikibase:directClaim ?prop.
        OPTIONAL {?value wdt:P18 ?image}
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    ORDER BY ?t
    
    Try it!
  • #Riemann
    #defaultView:Timeline
    SELECT DISTINCT ?t ?value ?rrLabel ?valueLabel ?image
    WHERE
    {
      {wd:Q42299 ?prop ?t FILTER (datatype(?t)=xsd:dateTime) .
       ?value wikibase:directClaim ?prop. }
      UNION
      {
       wd:Q42299 ?q ?statement  FILTER regex (STR(?q),"prop/P") . #exclude P:describedBy
       wd:Q42299 wdt:P570 ?death . 
       ?statement ?p ?t FILTER regex (STR(?p), "prop/qualifier/P") FILTER (datatype(?t)=xsd:dateTime) FILTER (?t < ?death).
       ?statement ?r ?value FILTER regex (STR(?r),"prop/statement/") .
       BIND(IRI(CONCAT(CONCAT(SUBSTR(STR(?r),1,29),"direct/"),SUBSTR(STR(?r),40))) as ?rprop) 
       ?rr wikibase:directClaim ?rprop .
       OPTIONAL {?value wdt:P18 ?image}
      }
      UNION {
       ?value wdt:P50 wd:Q42299 .
    #   wd:Q309765 wdt:P570 ?death .
       ?value wdt:P577 ?t . 
    #          FILTER (?t < ?death) 
    #    FILTER NOT EXISTS {?value wdt:P629 ?x .} .
    #   ?value wdt:P1433 ?rr .
       OPTIONAL {?value wdt:P18 ?image}
      }
      UNION
      {
       wd:Q42299 wdt:P800 ?rr .
       ?rr  wdt:P577 ?t .
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de, fr". }
    }
    
    Try it!
  • #Paul Erdös timeline
    #defaultView:Timeline
    SELECT DISTINCT ?t ?value ?rrLabel ?valueLabel (SAMPLE(?image) as ?img)
    WHERE
    {
      {wd:Q173746 ?prop ?t FILTER (datatype(?t)=xsd:dateTime) .
       ?value wikibase:directClaim ?prop. }
      UNION
      {
       wd:Q173746 ?q ?statement  FILTER regex (STR(?q),"prop/P") . #exclude P:describedBy
       wd:Q173746 wdt:P570 ?death . 
       ?statement ?p ?t FILTER regex (STR(?p), "prop/qualifier/P") FILTER (datatype(?t)=xsd:dateTime) FILTER (?t < ?death).
       ?statement ?r ?value FILTER regex (STR(?r),"prop/statement/") .
       BIND(IRI(CONCAT(CONCAT(SUBSTR(STR(?r),1,29),"direct/"),SUBSTR(STR(?r),40))) as ?rprop) 
       ?rr wikibase:directClaim ?rprop .
       OPTIONAL {?value wdt:P18 ?image}
      }
      UNION {
       ?value wdt:P50 wd:Q173746 .
    #   wd:Q309765 wdt:P570 ?death .
       ?value wdt:P577 ?t . 
    #          FILTER (?t < ?death) 
    #    FILTER NOT EXISTS {?value wdt:P629 ?x .} .
    #   ?value wdt:P1433 ?rr .
       OPTIONAL {?value wdt:P18 ?image}
      }
      UNION
      {
       wd:Q173746 wdt:P800 ?rr .
       ?rr  wdt:P577 ?t .
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en,de, fr". }
    } GROUP BY ?t ?value ?rrLabel ?valueLabel
    
    Try it!
  • Neptune's moons [1]
  • Cranach paintings[2]
  • Hermann Weyl[3]
  • Beltrami[4]
  • Lord Byron [5]
  • Timeline of discoveries of natural satellites in the solar system [6]

BubbleChart viewEdit

  • Surname packed bubble chart
#defaultView:BubbleChart
SELECT ?surname ?surnameLabel ?count WHERE {
  {
    SELECT ?surname (COUNT(?person) AS ?count) WHERE {
      ?person wdt:P31 wd:Q5.
      ?person wdt:P734 ?surname.
    }
    GROUP BY ?surname
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?count)
LIMIT 100

Try it!

  • Fictional character packed bubble chart
#defaultView:BubbleChart
SELECT ?surname ?surnameLabel ?count WHERE {
  {
    SELECT ?surname (COUNT(?person) AS ?count) WHERE {
      ?person (wdt:P31/wdt:P279*) wd:Q95074.
      ?person wdt:P734 ?surname.
    }
    GROUP BY ?surname
  }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?count)
LIMIT 100

Try it!

  • NGO packed bubble chart draft
#defaultView:BubbleChart
SELECT ?item ?headq ?country ?countryLabel ?count WHERE {
  {
  SELECT ?country (COUNT(?item) AS ?count) WHERE {
  ?item wdt:P31 wd:Q79913 .
  ?item wdt:P159 ?headq .
  ?headq wdt:P17 ?country .
 }
  GROUP BY ?country
 }          
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
 ORDER BY DESC(?count)
 LIMIT 100

Try it!

  • Scientific paper main subjects by general area
#defaultView:BubbleChart
SELECT ?area ?areaLabel ?count
WHERE 
{
  {
    SELECT ?area (COUNT(?item) AS ?count)    
WHERE {
        ?item wdt:P31 wd:Q13442814 .
        ?item wdt:P921 ?subject .
        ?subject wdt:P31 ?area .
        
       }
  GROUP BY ?area 
  }
   SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
ORDER BY DESC(?count)
LIMIT 100

Try it! }

  • #For book properties
    #defaultView:BubbleChart
    SELECT ?property ?propertyLabel ?propertyDescription ?count WHERE { 
    	{
    		select ?propertyclaim (COUNT(*) AS ?count) where {
    			?item wdt:P31 wd:Q571 .
    			?item ?propertyclaim [] .
    		} group by ?propertyclaim 
    	}
    	?property wikibase:claim ?propertyclaim .
    	SERVICE wikibase:label { bd:serviceParam wikibase:language "fr,en" . }
    } ORDER BY DESC (?count)
    
    Try it!
  • #https://blog.k-nut.eu/wikidata-parliament-svg, results to be passed to parliament-svg program
    #defaultView:BubbleChart
    SELECT ?partyLabel ?rgb ?party (COUNT(*) as ?count)
    WHERE
    {
        ?politician wdt:P39 wd:Q17781726 .
        ?politician p:P39 ?membership .
        ?membership pq:P2937 wd:Q30544760 .
        ?politician wdt:P102 ?party .
        ?party wdt:P462 ?color .
        ?color wdt:P465 ?rgb .
    
        SERVICE wikibase:label { bd:serviceParam wikibase:language "de" }
    }
    GROUP BY ?party ?partyLabel ?rgb
    
    Try it!
  • ISBN-13 bubble chart [7]
  • World Cup scorers bubble chart [8]
  • #"Stated in" query by Daniel Mietchen
    ##defaultView:BubbleChart
    #Sources used as references on Wikidata
    SELECT ?source ?sourceLabel (COUNT(?source) AS ?count)
    WHERE
    {
        ?item wdt:P248 ?source .
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "en" .
        }
    }
    GROUP BY ?source ?sourceLabel
    ORDER BY DESC(?count)
    
    Try it!

LineChart viewEdit

  • #defaultView:LineChart
    # number of persons per QID (using P735=Q4925477)
    # by Jura1, 2017-08-27
    SELECT ?n (COUNT(?item)*72 as ?ct)
    {
        ?item wdt:P735 wd:Q4925477 ; wdt:P31 wd:Q5 .
        BIND(ROUND(xsd:integer(strafter(str(?item),"y/Q"))/1000000) as ?n)
    }
    GROUP BY ?n
    
    Try it!
  • #Bechdel test, year by year
    #defaultView:LineChart
    SELECT ?year (COUNT(?film) AS ?nb_films) ?statusLabel WHERE {
      ?film wdt:P31/wdt:P279* wd:Q11424 ;
                wdt:P577 ?date ;
                wdt:P1552 ?status.
      BIND(STR(YEAR(?date)) AS ?year)
      VALUES ?status { wd:Q45171911 wd:Q45172088   }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    GROUP BY ?year ?statusLabel
    
    Try it!

ScatterChart viewEdit

ImageGrid viewEdit

  • Flags with Union Flag canton
# flags featuring the Union Jack as a canton (the top inner corner of a flag)
#defaultView:ImageGrid
SELECT ?flag ?flagLabel ?image WHERE {
  ?flag wdt:P31/wdt:P279* wd:Q186516;
        p:P180 [
          ps:P180 wd:Q3173323;
          pq:P518|pq:P1354 wd:Q1559605
        ].
  OPTIONAL { ?flag wdt:P18 ?image. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Try it!

  • Musical instruments and Spain
#Paintings depicting musical instruments with some connection to Hamburg
# added in 2017-06
#defaultView:ImageGrid
SELECT DISTINCT ?item ?image
WHERE
{
  ?item wdt:P31/wdt:P279* wd:Q3305213 .   
  ?item wdt:P180 ?object .
  ?item ?prop ?hhlink .
  ?hhlink ?prop2 wd:Q29 .
  ?object wdt:P279* wd:Q34379 .  
  ?item wdt:P18 ?image .
}

Try it!

  • Astrolabes (Poulter)[9]

SPARQL KeywordsEdit

Use of BINDEdit

  • # this query identifies the date-weekday combinations for when disasters happened
    # eg "Tuesday 12th"
    # original by Matěj Suchánek & Larske - https://www.wikidata.org/wiki/Wikidata:Request_a_query#Disasters_starting_on_Friday_13
    
    SELECT (?q AS ?Date) (SUM(?mon) AS ?Mon) (SUM(?tue) AS ?Tue) (SUM(?wed) AS ?Wed) (SUM(?thu) AS ?Thu) (SUM(?fri) AS ?Fri) (SUM(?sat) AS ?Sat) (SUM(?sun) AS ?Sun)  (COUNT(?start_time) AS ?Tot)  WHERE {
     { 
      ?disaster wdt:P31/wdt:P279* wd:Q3839081 ;
                p:P580 ?start_time_stm .          # find the date multi-day disasters started
      ?start_time_stm ps:P580 ?start_time .
      ?start_time_stm psv:P580 ?start_time_node .
      ?start_time_node wikibase:timePrecision ?tp . FILTER(?tp=11) 
     }
     UNION 
     {
      ?disaster wdt:P31/wdt:P279* wd:Q3839081 ;
                p:P585 ?start_time_stm .          # find all those which have "point in time" (ie one-day events)
      ?start_time_stm ps:P585 ?start_time .
      ?start_time_stm psv:P585 ?start_time_node .
      ?start_time_node wikibase:timePrecision ?tp . FILTER(?tp=11) 
     }
      # this beautiful bit of code finds all the relevant days of the week
      # using https://cs.wikipedia.org/wiki/Algoritmus_pro_v%C3%BDpo%C4%8Det_dne_v_t%C3%BDdnu#Zeller%C5%AFv_algoritmus
      BIND( DAY( ?start_time ) AS ?q ) .
      BIND( MONTH( ?start_time ) + IF( MONTH( ?start_time ) < 3, 12, 0 ) AS ?m ) .
      BIND( YEAR( ?start_time ) - IF( MONTH( ?start_time ) < 3, 1, 0 ) AS ?_year ) .
      BIND( ?_year - FLOOR( ?_year / 100 ) * 100 AS ?K ) . # modulo
      BIND( FLOOR( YEAR( ?start_time ) / 100 ) AS ?J ) .
      BIND( ?q + FLOOR( 13 * ( ?m + 1 ) / 5 ) + ?K + FLOOR( ?K / 4 ) + FLOOR( ?J / 4 ) - 2 * ?J AS ?_h ) .
      BIND( ?_h - FLOOR( ?_h / 7 ) * 7 AS ?weekday) . # modulo, 0 is Saturday
      BIND(IF(?weekday=2,1,0) AS ?mon)
      BIND(IF(?weekday=3,1,0) AS ?tue)
      BIND(IF(?weekday=4,1,0) AS ?wed)
      BIND(IF(?weekday=5,1,0) AS ?thu)
      BIND(IF(?weekday=6,1,0) AS ?fri)
      BIND(IF(?weekday=0,1,0) AS ?sat)
      BIND(IF(?weekday=1,1,0) AS ?sun)
    }
    GROUP BY ?q
    ORDER BY ?Date
    
    Try it!
  • Auxiliary
# disasters without a date
SELECT ?disaster ?disasterLabel ?typeLabel WHERE {
  ?disaster wdt:P31/wdt:P279* wd:Q3839081 .
  ?disaster wdt:P31 ?type .
  filter not exists { ?disaster p:P580 ?start_time } .
  filter not exists { ?disaster p:P585 ?point_in_time } .
  filter not exists { ?disaster wdt:P31 wd:Q13406463 } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
}

Try it!

  • #Royal Society obituaries (Andrew Gray)
    SELECT ?item ?itemLabel ?year ?journalLabel ?volume ?pages ?doi ?subject ?subjectLabel
    WHERE 
    {
      { ?item wdt:P1433 wd:Q4914871 } UNION { ?item wdt:P1433 wd:Q29043655 } . # In Bio Memoirs RS or Obit Notices RS
      ?item wdt:P921 ?subject . 
      ?item wdt:P356 ?doi . 
      ?item wdt:P577 ?date .
      bind (year(?date) as ?year).
      ?item wdt:P478 ?volume .
      ?item wdt:P304 ?pages .
      ?item wdt:P1433 ?journal .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    order by ?doi
    
    Try it!
  • #Andrew Gray
    # ambassadors to Argentina
    # who were also MPs
    SELECT DISTINCT ?item ?itemLabel ?startyear ?endyear {
     ?item p:P39 ?positionStatement . ?positionStatement ps:P39 wd:Q29898729 .  # ambassador to Argentina
     OPTIONAL { ?positionStatement pq:P580 ?start . bind(year(?start) as ?startyear) . }
     OPTIONAL { ?positionStatement pq:P582 ?end . bind(year(?end) as ?endyear) . }
      ?item p:P39 ?positionStatement2 . ?positionStatement2 ps:P39 [wdt:P279* wd:Q16707842] . # also an MP
     SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
    }
    ORDER BY desc(?startyear)
    
    Try it!

Use of CONTAINSEdit

  • # taxa that look like they’re named after someone (going by their English label or common name), but don’t have a “named after” statement
    SELECT ?taxon ?label WHERE {
      ?taxon wdt:P31 wd:Q16521;
             wdt:P1843|| CONTAINS(?label, "’s ") || CONTAINS(?label, "s' ") |
    
    Try it!

Use of COUNTEdit

  • # number of author (P50) statements
    # runtime: instantaneous
    SELECT (COUNT(*) AS ?count) WHERE { [] p:P50 []. }
    
    Try it!

Use of FILTEREdit

  • Demonyms
SELECT ?country ?name {
  ?country wdt:P31 wd:Q6256 .
  ?country wdt:P1549 ?name .
  FILTER ( lang(?name)='en')
}

Try it!

  • Time intervals
SELECT ?item ?itemLabel
WHERE {
  ?item wdt:P31 wd:Q5 .
  ?item wdt:P569 ?time0 .
  ?item wdt:P1343 ?dummy
  FILTER ( ?time0 >= "1610-01-01T00:00:00Z"^^xsd:dateTime && ?time0 <= "1620-01-01T00:00:00Z"^^xsd:dateTime )
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" 
}
 }

Try it!

  • MP couples:
SELECT DISTINCT ?item ?person2 WHERE { ?item wdt:P31 wd:Q5 ; wdt:P39 wd:Q16707842. ?person2 wdt:P31 wd:Q5 ; wdt:P39 wd:Q16707842 ; wdt:P26 ?item . FILTER(STR(?person2) > STR(?item)) }

Try it!

  • Filter by inequality: nearly antipodal capitals
SELECT DISTINCT ?country1Label ?country2Label ?capital1Label ?capital2Label ?distance WHERE {
  ?country1 wdt:P31 wd:Q6256 ;
            wdt:P36 ?capital1 .

  ?country2 wdt:P31 wd:Q6256 ;
            wdt:P36 ?capital2 .
  
  ?capital1 wdt:P625 ?coords1 .
  ?capital2 wdt:P625 ?coords2 .
  
  BIND(ROUND(geof:distance(?coords1, ?coords2)) AS ?distance) .
  FILTER (?distance>19000)
  
  FILTER(xsd:string(?capital1) < xsd:string(?capital2)) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY DESC(?distance)

Try it!

  • Comparison with todays's date: select a reviewer
SELECT DISTINCT ?reviewer ?reviewerLabel (SAMPLE(?website) AS ?website) (SAMPLE(?orcid) AS ?orcid) (SAMPLE(?twitter) AS ?twitter) WHERE {
  BIND(wd:Q21198 AS ?field).
  ?subField wdt:P279* ?field.
  ?reviewer wdt:P31 wd:Q5.
  MINUS { ?reviewer wdt:P570 []. }
  ?work wdt:P50 ?reviewer;
        wdt:P577 ?workDate.
  FILTER(?workDate > (NOW() - "P5Y"^^xsd:duration)).
  { ?work wdt:P921 ?subField. } UNION 
  { ?reviewer wdt:P101 ?subField. }
  OPTIONAL { ?reviewer wdt:P856 ?website. }
  OPTIONAL { ?reviewer wdt:P496 ?orcid_. BIND(IRI(CONCAT("https://orcid.org/", ?orcid_)) AS ?orcid). }
  OPTIONAL { ?reviewer wdt:P2002 ?twitter_. BIND(IRI(CONCAT("https://twitter.com/", ?twitter_)) AS ?twitter). }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?reviewer ?reviewerLabel
ORDER BY RAND()

Try it!

Use of FILTER NOT EXISTSEdit

  • #Non-existent labels
    SELECT ?item ?itemLabel
    WHERE {
    	?item	wdt:P1435/wdt:P279* wd:Q14469659 .
    	FILTER NOT EXISTS { 
          ?item rdfs:label ?itemLabel . 
          FILTER (lang(?itemLabel) = "en") 
        }
    }
    
    Try it!
  • # This query identifies MPs and the final ends to their careers (Andrew Gray)
    # It looks for 
    # - the last time someone left office as an MP
    # - what the cause of their leaving office was
    #defaultView:Timeline
    
    SELECT DISTINCT ?item ?itemLabel ?constituencyLabel ?causeLabel ?end {
     
     # find all MP positions
     
     ?membership wdt:P279 wd:Q16707842 . 
     
     # and people who held such a position
     
     ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . 
     
     OPTIONAL { ?positionStatement pq:P768 ?constituency . } # constituency if known
     OPTIONAL { ?positionStatement pq:P4100 ?party . } # party if known
     ?positionStatement pq:P580 ?start . # all members who have a start date (need to block out any without full dates)
     ?positionStatement pq:P582 ?end . # all members who have an end date 
     ?positionStatement pq:P1534 ?cause . # cause of leaving office
      
     FILTER (?end >= "1950-02-23T00:00:00Z"^^xsd:dateTime) .  # everyone whose term ended after the 1950 general election
      
     {?positionStatement pq:P1534 wd:Q52112831 } union  #
     {?positionStatement pq:P1534 wd:Q46993416 } union  #
     {?positionStatement pq:P1534 wd:Q46992747 }        # filter to three special cases 
      
     FILTER NOT EXISTS {
     ?membership2 wdt:P279 wd:Q16707842 .
     ?item p:P39 ?positionStatement2 . 
     ?positionStatement2 ps:P39 ?membership2 . 
     ?positionStatement2 pq:P580 ?start2 .
     FILTER (?start2 >= ?end) . 
     }
     # filter out any where they came back to office at a later date
     
     SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
    }
    order by ?end
    
    Try it!

Use of FILTER isBLANKEdit

# All former MPs alive on election day (Andrew Gray)
# note that election is explicitly hardcoded in three places because MINUS clauses are odd

SELECT distinct ?item ?itemLabel ?died ?born
{
  ?item p:P39 ?positionStatement . ?positionStatement ps:P39 ?membership . ?membership wdt:P279 wd:Q16707842 .
  ?positionStatement pq:P582 ?end . FILTER (?end < ?electionday) . wd:Q3586935 wdt:P585 ?electionday .
  # find all MPs who served any terms in office before this date (nb will include any unknown end dates)
  MINUS { ?item wdt:P570 ?died . FILTER (?died < ?electionday) . wd:Q3586935 wdt:P585 ?electionday }
  MINUS { ?item wdt:P570 ?died . FILTER isBLANK(?died) . }
  # remove all who died before this date, or have a date of death unknown
  MINUS { ?item p:P39 ?positionStatement2 . ?positionStatement2 ps:P39 ?membership2 .
         ?membership2 wdt:P279 wd:Q16707842 . ?positionStatement2 pq:P2715 wd:Q3586935 . }
  # remove all those who were elected at this specific election and thus were an MP on that day
  optional { ?item wdt:P569 ?born } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language 'en' }
}

Try it!

Use of GROUP BYEdit

#Church image duplicates (culled from Magnus Manske, Jheald)
SELECT ?church ?churchLabel ?img
WITH {
  SELECT ?church ?img WHERE {
    ?church wdt:P31 wd:Q16970 . 
    ?church wdt:P18 ?img 
  }
} AS %church_images
WHERE {
  {     
     SELECT ?img (count(?church) AS ?cnt)  {
       INCLUDE %church_images
     } GROUP BY ?img 
     HAVING (?cnt>1)
  }
  INCLUDE %church_images .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY desc(?cnt) ?img ?churchLabel

Try it!

  • #ORCID duplicates (Magnus Manske)
    SELECT ?orcid (count(DISTINCT ?q) AS ?cnt) (group_concat(?q) AS ?authors) { ?q wdt:P496 ?orcid } GROUP BY ?orcid HAVING (?cnt>1)
    
    Try it! }
  • # Find all mineral type localities and sort them by most minerals defined (chris.urs-o)
    SELECT ?locality ?name (COUNT(DISTINCT ?mineral) AS ?count)
    WHERE
    {
        ?mineral wdt:P279* wd:Q7946 .           # Find all instances and subclasses of mineral (traverse the tree)
        ?mineral wdt:P2695 ?locality .          # Where a locality statement exists
       
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "en" .
            
        }
    }
    GROUP BY ?locality ?name
    ORDER BY DESC(?count)
    
    Try it!
  • # properties with most “statement disputed by” qualifiers on statements
    SELECT ?property ?propertyLabel (COUNT(?statement) AS ?count) WHERE {
      ?property a wikibase:Property;
                wikibase:claim ?p.
      ?entity ?p ?statement.
      ?statement pq:P1310 [].
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    GROUP BY ?property ?propertyLabel
    ORDER BY DESC(?count)
    
    Try it!
  • <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-0">Lua error in Module:ConcatArgs at line 36: invalid value (nil) at index 1 in table for 'concat'.</span></strong>
    
    Try it!
  • [10]
    <strong class="error"><span class="scribunto-error" id="mw-scribunto-error-2">Lua error in Module:ConcatArgs at line 36: invalid value (nil) at index 1 in table for 'concat'.</span></strong>
    
    Try it!
  • # ratio of female characters per fictional universe
    # (assuming that any item with “from fictional universe” and “sex or gender” is a fictional character)
    SELECT ?universe ?universeLabel ?percentString ?females ?total WHERE { # add ?percent if you want to sort in the table
      {
        SELECT ?universe (SUM(?female) AS ?females) (COUNT(*) AS ?total) WHERE {
          ?character wdt:P1080 ?universe;
                     wdt:P21 ?gender.
          BIND(IF(?gender IN (wd:Q6581072, wd:Q1052281), 1, 0) AS ?female)
        }
        GROUP BY ?universe
      }
      BIND(?females/?total AS ?ratio)
      BIND(100*?ratio AS ?percent)
      BIND(CONCAT(SUBSTR(STR(?percent), 1, 5), "%") AS ?percentString)
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    ORDER BY DESC(?total)
    
    Try it!

Use of GROUP CONCATEdit

  • # Canadian actors who twice played a US president
    SELECT
    ?actor ?actorLabel
    (GROUP_CONCAT(?movieLabel; separator = "; ") AS ?movies)
    (GROUP_CONCAT(?presidentLabel; separator = "; ") AS ?presidents)
    WHERE {
      ?actor wdt:P31 wd:Q5;
             wdt:P27 wd:Q16;
             p:P106/ps:P106 wd:Q33999.
      ?movie p:P161 [
        ps:P161 ?actor;
        pq:P453 ?president
      ].
      ?president p:P39/ps:P39 wd:Q11696.
      SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
        ?actor rdfs:label ?actorLabel.
        ?movie rdfs:label ?movieLabel.
        ?president rdfs:label ?presidentLabel.
      }
    }
    GROUP BY ?actor ?actorLabel
    HAVING(COUNT(DISTINCT ?movie) = 2)
    
    Try it!
  • #Film adaptations of books
    SELECT DISTINCT ?film ?IMDb (GROUP_CONCAT(DISTINCT  ?IDBnF; separator=";") as ?IDsBnF)
    WHERE {
      ?film wdt:P31/wdt:P279* wd:Q11424.
      # on recherche des éléments ayant nature film/Q11424 ou une sous-classe
      ?film wdt:P144 ?oeuvre.
      # "basé sur"/p144 une oeuvre, propriété 
      {?oeuvre wdt:P31/wdt:P279* wd:Q571} UNION {?oeuvre wdt:P31/wdt:P279* wd:Q7725634}
      # l'oeuvre a en nature livre/Q571 ou oeuvre littéraire/Q7725634 (ou sous-classe)
      ?oeuvre wdt:P268 ?IDBnF.  # l'oeuvre a un idenfiant BnF/propriété P268
      ?film wdt:P345 ?IMDb # le film a un identfiant IMDb
    }
    GROUP BY ?film ?IMDb
    
    Try it!
  • #Languages with cases
    SELECT ?language ?languageLabel (GROUP_CONCAT(?caseLabel; separator = ", ") AS ?cases) WHERE {
      ?language wdt:P2989 ?case.
      SERVICE wikibase:label {
        bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
        ?language rdfs:label ?languageLabel.
        ?case rdfs:label ?caseLabel.
      }
    }
    GROUP BY ?language ?languageLabel
    ORDER BY DESC(COUNT(?case))
    
    Try it!

Use of HAVINGEdit

  • #Translators with only one language spoken, written or signed
    SELECT ?person ?personLabel ?languageLabel WHERE {
      {
        SELECT ?person (SAMPLE(?language) AS ?language) WHERE {
          ?person wdt:P31 wd:Q5;
                  wdt:P106 wd:Q333634;
                  wdt:P1412 ?language.
        }
        GROUP BY ?person ?personLabel
        HAVING(COUNT(?language) = 1)
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    
    Try it!

Use of INEdit

  • #"Wine" as object
    select ?s ?sLabel ?pLabel ?oLabel 
    WHERE { ?s ?p ?o . 
           ?o wdt:P279* wd:Q282 . 
    FILTER(?p in (wdt:P101,wdt:P131, wdt:P136, wdt:P162 ,wdt:P186,wdt:P301,wdt:P452,wdt:P527,p:qualifier\/P642, wdt:P706,wdt:P921,wdt:P971,wdt:P1056,wdt:P1855))
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
           }
    ORDER BY ?pLabel 
    DESC(?count)
    
    Try it!

Use of INCLUDEEdit

  • #Andrew Gray, given names of Westminster MPs and date when 1st MP with that name entered parliament 
    SELECT ?givenName ?givenNameLabel ?firstDate WITH {
      SELECT ?givenName (MIN(?date) AS ?firstDate) WHERE {
        { ?mpStatement ps:P39/wdt:P279? wd:Q16707842. } UNION
        { ?mpStatement ps:P39/wdt:P279? wd:Q18015642. } UNION
        { ?mpStatement ps:P39/wdt:P279? wd:Q18018860. }
        ?mpStatement pq:P580 ?date.
        ?mp p:P39 ?mpStatement;
            wdt:P735 ?givenName.
      }
      GROUP BY ?givenName
    } AS %givenNames WHERE {
      INCLUDE %givenNames.
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    ORDER BY ?firstDate ?givenNameLabel
    
    Try it!
  • #Andrew Gray, version of the above,  includes the MPs who were the first of their given name 
    SELECT ?givenName ?givenNameLabel ?firstDate ?mp ?mpLabel WITH {
      SELECT ?givenName (MIN(?date) AS ?firstDate) WHERE {
        { ?mpStatement ps:P39/wdt:P279? wd:Q16707842. } UNION
        { ?mpStatement ps:P39/wdt:P279? wd:Q18015642. } UNION
        { ?mpStatement ps:P39/wdt:P279? wd:Q18018860. }
        ?mpStatement pq:P580 ?date.
        ?mp p:P39 ?mpStatement;
            wdt:P735 ?givenName.
      }
      GROUP BY ?givenName
    } AS %givenNames WITH {
      SELECT DISTINCT ?givenName ?firstDate ?mp WHERE {
        INCLUDE %givenNames.
        ?mpStatement pq:P580 ?firstDate.
        { ?mpStatement ps:P39/wdt:P279? wd:Q16707842. } UNION
        { ?mpStatement ps:P39/wdt:P279? wd:Q18015642. } UNION
        { ?mpStatement ps:P39/wdt:P279? wd:Q18018860. }
        ?mp p:P39 ?mpStatement;
            wdt:P735 ?givenName.
      }
    } AS %mps WHERE {
      INCLUDE %mps.
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    ORDER BY ?firstDate ?givenNameLabel
    
    Try it!

Use of NOT INEdit

  • #"Chemistry" as object of Wikidata statements.
    SELECT ?p ?pLabel (COUNT(?s) AS ?count) 
    WHERE { ?s ?p ?o. 
           ?o wdt:P279* wd:Q2329 . 
    FILTER(?p NOT IN (wdt:P31,schema:about,p:statement\/P31, wdt:P279, p:statement\/P279)) 
           
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    GROUP BY ?p ?pLabel
    
    Try it!

Use of SAMPLEEdit

  • #Edinburgh U.-educated women by birth-place
    SELECT ?person ?personLabel 
    (SAMPLE(?birth_date) as ?birth_date) 
    (SAMPLE(?death_date ) as ?death_date ) 
    (SAMPLE(?birth_place_label ) as ?birth_place_label ) 
    (SAMPLE(?birth_place_coords ) as ?birth_place_coords ) 
    (SAMPLE(?image ) as ?image ) 
    (SAMPLE(?country ) as ?country) 
    (COUNT(?article) as ?rank)
    WHERE 
    {	 
      	?person wdt:P69 wd:Q160302 .
    	?person wdt:P31 wd:Q5 .
      	?person wdt:P21 wd:Q6581072 .
    
        OPTIONAL {?person wdt:P18 ?image }
      	OPTIONAL {?person wdt:P18 ?image }
    	OPTIONAL {?person wdt:P569 ?birth_date }
      	OPTIONAL {?person wdt:P27 ?countryItem .
                  ?countryItem rdfs:label ?country filter (lang(?country) = "en")
             }
    	OPTIONAL {
                 ?person wdt:P19 ?birth_place . 
                 ?birth_place wdt:P625 ?birth_place_coords .
                 ?birth_place rdfs:label ?birth_place_label filter ( lang(?birth_place_label) = "en" )
             }
    	OPTIONAL {?person wdt:P570 ?death_date } 
        OPTIONAL {?article schema:about ?person}     
      
     SERVICE wikibase:label {bd:serviceParam wikibase:language "en" }
    }
    
     
    GROUP BY ?person ?personLabel
    ORDER BY DESC(?rank)
    
    Try it!

Use of SUMEdit

  • SELECT (SUM(?count) AS ?total) 
    WHERE {
    
       ?item p:P1367/pq:P1114 ?count 
           }
    
    Try it!

Use of UNIONEdit

  • # this query
    # - checks every President of France
    # - finds out who was President on the day they were born
    # - finds out if that person was still alive on the day the new one became President
    #
    # this is a deeply trivial query
    # but as it turns out to be true for Macron, it's an interesting footnote
    # and we should see if it's true for anyone else
    
    SELECT ?newperson ?newpersonLabel ?newstart ?oldperson ?oldpersonLabel ?died # ?newbirth ?start ?end 
    WHERE {
    
      # first, find all presidents (as ?newperson) with their accession dates and birthdates
      
      ?newperson wdt:P31 wd:Q5 ; wdt:P39 wd:Q191954 .  # this is hardcoded as President of France
      ?newperson wdt:P569 ?newbirth .
      ?newperson p:P39 [ ps:P39 wd:Q191954; pq:P580 ?newstart ]. 
    
      # then, find all presidents again (as ?oldperson) with their start/finish dates and deathdates
      
      ?oldperson wdt:P31 wd:Q5 ; wdt:P39 wd:Q191954 .
      OPTIONAL { ?oldperson wdt:P570 ?died . }
      ?oldperson p:P39 [ ps:P39 wd:Q191954; pq:P580 ?start ] .
      ?oldperson p:P39 [ ps:P39 wd:Q191954; pq:P582 ?end ] .
      FILTER ( ?start < ?newbirth ) .  # when the new person was born, the old person had begun their term
      FILTER ( ?end > ?newbirth ) .    # when the new person was born, the old person had not finished their term
                                       # therefore combined, this was presumably the person on the day they were born
                                       # can uncomment ?newbirth ?start ?end to check
      { FILTER ( ?newstart < ?died ) } UNION { FILTER NOT EXISTS{ ?oldperson wdt:P570 ?died } } .
                                       # this complete mess of a query finds the cases
                                       # a) where the new person's start date is before the old person's death
                                       # b) where the old person has not yet died
                                       
      
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }  # get their names
    }
    
    Try it!

Use of WITHEdit

  • #Andrew Gray
    # For a given UK parliament, find
    # - how many MPs sat in it
    # - how many of those have died
    # - how many of those who have died are in the ODNB
    # - how many *overall* are in the ODNB
    #   (remember the ODNB only adds people three years after their death)
    #
    
    SELECT ?parliamentLabel ?start ?end ?mpcount ?odnbcount ?percent_in_odnb ?diedcount ?percent_died_in_odnb 
    
    WITH {
      # all MPs with their associated parliament
      SELECT DISTINCT ?mp ?parliament WHERE {
        ?mp wdt:P31 wd:Q5.
          # using the new data model - so limits how far back it goes
          ?mp p:P39/ps:P39 ?position . 
          ?position wdt:P279 wd:Q16707842; p:P279/pq:P2937|wdt:P2937 ?parliament.
        }
    } AS %MPsWithParliament 
    
    WITH {
      # count of ODNB entries in each parliament
      SELECT ?parliament (COUNT(DISTINCT ?mp) AS ?odnb) WHERE {
        INCLUDE %MPsWithParliament.
        ?mp wdt:P1415 ?odnb
      }
      GROUP BY ?parliament
    } AS %odnb 
    
    WITH {
      # count of dead MPs in each parliament
      SELECT ?parliament (COUNT(DISTINCT ?mp) AS ?died) WHERE {
        INCLUDE %MPsWithParliament.
        ?mp wdt:P570 ?died . 
      }
      GROUP BY ?parliament
    } AS %died 
    
    WITH {
      # count of members entries in each parliament
      SELECT ?parliament (COUNT(DISTINCT ?mp) AS ?mps) WHERE {
        ?mp wdt:P31 wd:Q5.
          ?mp p:P39/ps:P39 ?position.
          ?position wdt:P279 wd:Q16707842; p:P279/pq:P2937|wdt:P2937 ?parliament.
      }
      GROUP BY ?parliament
    } AS %mpcount 
    
    WHERE {
      # get ?odnb
      INCLUDE %odnb
      INCLUDE %mpcount
      INCLUDE %died
      BIND(?odnb AS ?odnbcount)
      BIND(?mps AS ?mpcount)
      BIND(ROUND(100*(?odnbcount/?mpcount)) AS ?percent_in_odnb) 
      BIND(?died AS ?diedcount)
      BIND(ROUND(100*(?odnbcount/?diedcount)) AS ?percent_died_in_odnb) 
    
      # get parliament dates
      ?parliament wdt:P571|wdt:P580 ?start.
      ?parliament wdt:P576|wdt:P582 ?end.
      # add labels
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    ORDER BY ?start
    
    Try it!

Use of VALUESEdit

  • #Biological pathways with protein structures in the PDB database
    SELECT ?pathway ?pathwayLabel ?WikiPathways ?Reactome (COUNT(DISTINCT ?protein) as ?count) WHERE {
      VALUES ?pathwayType { wd:Q4915012 wd:Q2996394 }
      ?pathway wdt:P31 ?pathwayType .
      { ?pathway wdt:P527/wdt:P688 ?protein . } UNION { ?pathway wdt:P527 ?protein . }
      ?protein wdt:P638 ?PDBID .
      OPTIONAL { ?pathway wdt:P2410 ?WikiPathways }
      OPTIONAL { ?pathway wdt:P3937 ?Reactome }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    } GROUP BY ?pathway ?pathwayLabel ?WikiPathways ?Reactome
      ORDER BY DESC(?count)
    
    Try it!

rdfsEdit

  • #UK places without Welsh label
    SELECT DISTINCT ?item ?itemLabel WHERE {
      ?item (wdt:P31/wdt:P279*) wd:Q618123.
      ?item wdt:P17 wd:Q145.
      ?item rdfs:label ?itemLabel.
      MINUS {
        ?item rdfs:label ?label2.
        FILTER((LANG(?label2)) = "cy")
      }
      FILTER((LANG(?itemLabel)) = "en")
    }
    LIMIT 1000
    
    Try it!

skosEdit

  • #Diseases of endocrinology with aliases
    SELECT ?item ?itemLabel ?alternative 
      WHERE {
      ?item wdt:P31 wd:Q12136 .
      ?item wdt:P1995 wd:Q162606 .
      OPTIONAL { ?item skos:altLabel ?alternative . }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!

Inception date=2017-07-10; Inception hits=3014

  • Special:ItemDisambiguation surrogate (slow performance, doesn't deduplicate)
#Special:ItemDisambiguation surrogate 
SELECT ?item ?label ?lang ?desc 
  WHERE 
   {
     { 
       { ?item rdfs:label ?label } 
       UNION
       { ?item skos:altLabel ?label } 
                                      }
  FILTER (lcase(str(?label)) = lcase("John Campbell") )
  #filter(lang(?label)="en") .
  BIND (lang(?label) AS ?lang) .
	OPTIONAL { ?item schema:description ?desc .  FILTER(lang(?desc)="en") }
   }
LIMIT 500

Try it!

schema.orgEdit

  • French citizens, not in frWP but in enWP
SELECT ?item ?itemLabel WHERE {
   ?item wdt:P31 wd:Q5 .
   ?item wdt:P27 wd:Q142 .
   FILTER EXISTS { ?wen schema:about ?item . ?wen schema:inLanguage "en" }
   FILTER NOT EXISTS { ?wfr schema:about ?item . ?wfr schema:inLanguage "fr" }
  SERVICE wikibase:label {
    bd:serviceParam wikibase:language "fr" .
  }
 }

Try it!

  • Swiss women, in ODNB but not in enWP
SELECT ?item WHERE {
  ?item wdt:P1415 ?dummy0 .
  OPTIONAL {
    ?wiki0 <http://schema.org/about> ?item .
    FILTER(SUBSTR(STR(?wiki0),1,24) = 'https://en.wikipedia.org') .
  }
  FILTER(!bound(?wiki0))
  ?item wdt:P21 wd:Q6581072 .
  ?item wdt:P27 wd:Q39 .
}

Try it!

  • Described by source, in enWP, not in (?), date of death in interval.
SELECT ?item WHERE {
  ?item wdt:P1343 [] .
  ?wiki0 <http://schema.org/about> ?item .
  ?wiki0 <http://schema.org/isPartOf> <https://en.wikipedia.org/> .
  OPTIONAL { ?item wdt:P1415 ?dummy0 }
  FILTER(!bound(?dummy0))
  ?item wdt:P570 ?time0 .
  FILTER ( ?time0 >= "1879-01-01T00:00:00Z"^^xsd:dateTime && ?time0 <= "1900-01-01T00:00:00Z"^^xsd:dateTime )
}

Try it!

  • Main subject in enWP, round corner
SELECT ?item ?itemLabel ?subject ?subjectLabel ?article WHERE {

  ?item wdt:P1433 wd:Q15987216 .
  ?item wdt:P921 ?subject .
  ?article 	schema:about ?subject ;
			schema:isPartOf <https://en.wikipedia.org/> .
           
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en" .
    }

}

Try it!

  • French women engineers in enWP
SELECT ?item WHERE {
  ?item wdt:P21 wd:Q6581072 .
  ?item wdt:P27 wd:Q142 .
  ?item wdt:P106 wd:Q81096 .
  ?wiki0 <http://schema.org/about> ?item .
  ?wiki0 <http://schema.org/isPartOf> <https://en.wikipedia.org/> .
}

Try it!

  • #Wikisource authors, DNB No WP
    SELECT ?item ?itemLabel ?subject ?subjectLabel ?article WHERE {
    
      ?item wdt:P1433 wd:Q15987216 .
      ?item wdt:P31 wd:Q19389637.
      ?item wdt:P921 ?subject .
      ?article 	schema:about ?subject ;
    			schema:isPartOf <https://en.wikisource.org/> .
    
      OPTIONAL { ?article2 schema:about ?subject ; schema:isPartOf <https://en.wikipedia.org/> }
      FILTER ( !bound(?article2) )
      
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "en" .
        }
    }
    
    Try it!
  • Footprint, DNB00, SPARQL
  • Footprint, DNB01, SPARQL
  • Footprint, DNB12, SPARQL
  • #Statementless items with Wikispecies sitelink
    SELECT ?q ?x {
      ?x schema:about ?q ; schema:isPartOf <https://species.wikimedia.org/>  .
      ?q wikibase:statements ?statementcount
      FILTER ( ?statementcount = 0 )
    }
    
    Try it!

sitelinksEdit

  • #Counting sitelinks
    SELECT ?item ?itemLabel WHERE{
      {
        SELECT DISTINCT ?item ?cnt WHERE{
          ?item wdt:P106/wdt:P279* wd:Q901;
                wikibase:sitelinks ?cnt
        } ORDER BY DESC(?cnt) LIMIT 500
      }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }        
    } ORDER BY DESC(?cnt)
    
    Try it!
  • #Items with >10 sitelinks that have no image, but Wikipedia does
    SELECT ?q WHERE { ?q wdt:P31 wd:Q5 MINUS { ?q wdt:P18 [] }.?q wikibase:sitelinks ?linkcount FILTER ( ?linkcount > 10 )}
    
    Try it!
  • #Siblings who both have at least 75 Wikipedia pages in different languages 
    SELECT ?pers ?persLabel ?persLinks ?sib ?sibLabel ?sibLinks WHERE {
      ?pers wdt:P31 wd:Q5.
      ?pers wdt:P3373 ?sib.
      ?pers wikibase:sitelinks ?persLinks.
      ?sib wikibase:sitelinks ?sibLinks.
      FILTER (?persLinks > 75 && ?sibLinks > 75)
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    
    Try it!

MediaWiki APIEdit

  • See announcement, manual page Search text of English labels on Wikidata, output information "instance of" or "subclass of".
SELECT ?item ?itemLabel ?type ?typeLabel WHERE {
  SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:api "EntitySearch" .
      bd:serviceParam wikibase:endpoint "www.wikidata.org" .
      bd:serviceParam mwapi:search "cheese" .
      bd:serviceParam mwapi:language "en" .
      ?item wikibase:apiOutputItem mwapi:item .
  }
  ?item (wdt:P279|wdt:P31) ?type
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }                       
}

Try it!

  • Search full text of enWP articles.
SELECT * WHERE {
  SERVICE wikibase:mwapi {
      bd:serviceParam wikibase:api "Search" .
      bd:serviceParam wikibase:endpoint "en.wikipedia.org" .
      bd:serviceParam mwapi:srsearch "Haplogroup" .
      ?title wikibase:apiOutput mwapi:title .
  }
}

Try it!

embed.htmlEdit

  • Viennese composers and compositions by tonality
    #TEMPLATE={ "template": "Composers of ?city and their compositions by tonality", "variables": { "?city": { "query": "SELECT ?id WHERE { ?composition wdt:P86 ?composer; wdt:P826 ?tonality. ?composer wdt:P19|wdt:P20|wdt:P551 ?id. } GROUP BY ?id ?idLabel ORDER BY DESC(COUNT(DISTINCT ?composer))" } } }
    #defaultView:Tree
    SELECT ?composer ?composerLabel ?composerImage ?tonality ?tonalityLabel ?composition ?compositionLabel WHERE {
      BIND(wd:Q1741 AS ?city)
      ?composer wdt:P31 wd:Q5;
                wdt:P19|wdt:P20|wdt:P551 ?city.
      OPTIONAL { ?composer wdt:P18 ?composerImage. }
      ?composition wdt:P86 ?composer;
                   wdt:P826 ?tonality.
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],de-at,de". }
    }
    ORDER BY ?composerLabel ?tonalityLabel
    
    Try it!
  • Counting scientific paper items[11]
  • Counting main subject (P921) statements[12]
  • Places of birth of artists[13]
  • Food with country of origin[14]
  • Space program names, Martin Poulter[15]
  • Quadrilingual Swiss items[16]
  • Nobel geography[17]
  • Parliamentary constituencies errors (Andrew Gray)[18]

schema.orgEdit

  • Try it!
  • Try it!
  • Described by source, in enWP, not in (?), date of death in interval.
SELECT ?item WHERE {
  ?item wdt:P1343 [] .
  ?wiki0 <http://schema.org/about> ?item .
  ?wiki0 <http://schema.org/isPartOf> <https://en.wikipedia.org/> .
  OPTIONAL { ?item wdt:P1415 ?dummy0 }
  FILTER(!bound(?dummy0))
  ?item wdt:P570 ?time0 .
  FILTER ( ?time0 >= "1879-01-01T00:00:00Z"^^xsd:dateTime && ?time0 <= "1900-01-01T00:00:00Z"^^xsd:dateTime )
}

Try it!

  • Main subject in enWP, round corner
SELECT ?item ?itemLabel ?subject ?subjectLabel ?article WHERE {

  ?item wdt:P1433 wd:Q15987216 .
  ?item wdt:P921 ?subject .
  ?article 	schema:about ?subject ;
			schema:isPartOf <https://en.wikipedia.org/> .
           
    SERVICE wikibase:label {
        bd:serviceParam wikibase:language "en" .
    }

}

Try it!

  • French women engineers in enWP
SELECT ?item WHERE {
  ?item wdt:P21 wd:Q6581072 .
  ?item wdt:P27 wd:Q142 .
  ?item wdt:P106 wd:Q81096 .
  ?wiki0 <http://schema.org/about> ?item .
  ?wiki0 <http://schema.org/isPartOf> <https://en.wikipedia.org/> .
}

Try it!

  • Wikisource authors, DNB No WP
  • Footprint, DNB00, SPARQL
  • Footprint, DNB01, SPARQL
  • Footprint, DNB12, SPARQL
  • #Arabic WP female mathematicians, not in enWP. Can use with embed.html for performance reasons.
    SELECT DISTINCT ?item ?itemLabel 
    
    WHERE {?item wdt:P31 wd:Q5 .
           ?item wdt:P21 wd:Q6581072 .
           ?item wdt:P106 wd:Q170790 .
           FILTER EXISTS { ?war schema:about ?item . ?war schema:inLanguage "ar" }
       FILTER NOT EXISTS { ?wen schema:about ?item . ?wen schema:inLanguage "en" }
     
      SERVICE wikibase:label {bd:serviceParam wikibase:language "en" .}
     }
    
    Try it!

wikibase:Edit

  • #Wikidata properties having mix'n'match catalog (Magnus)
    SELECT DISTINCT ?q ?qLabel 
    WHERE 
      { 
        ?q rdf:type wikibase:Property ; wdt:P2264 ?mnm 
           SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } 
      }
    
    Try it!

PerformanceEdit

  • #Hints (JHeald)
    SELECT ?prop ?propLabel ?count WHERE {
        {
            SELECT ?prop (COUNT(DISTINCT ?item) AS ?count) WHERE {
               
               hint:Query hint:optimizer "None" .
               ?item wdt:P4200 [] .
               ?item ?p ?id .
               ?prop wikibase:directClaim ?p .
               {?prop wdt:P31/wdt:P279* wd:Q19847637}  # Wikidata property representing a unique identifier 
                 UNION
               {?prop wdt:P31/wdt:P279* wd:Q18614948}  # Wikidata property for authority control
               
              
            }  GROUP BY ?prop
        }
               
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "en" .
        }
    
    } ORDER BY DESC (?count)
    
    Try it!

Misc SPARQLEdit

  • #Check OBINs 1000 at a time
    #ODNB entries by OBIN, ordered, by 1000
    SELECT *
    WHERE
    { ?ODNBpeople012 wdt:P1415 ?OBIN
    FILTER(STRSTARTS(?OBIN, '101012')) .
    }
    ORDER BY ASC(?OBIN)
    
    Try it!
  • #Female ODNB ids, no link to enWP, no occupation
    SELECT ?item WHERE {
      ?item wdt:P1415 ?dummy0 .
      ?item wdt:P21 wd:Q6581072 .
      OPTIONAL {
        ?wiki0 <http://schema.org/about> ?item .
        FILTER(SUBSTR(STR(?wiki0),1,24) = 'https://en.wikipedia.org') .
      }
      FILTER(!bound(?wiki0))
      OPTIONAL { ?item wdt:P106 ?dummy1 }
      FILTER(!bound(?dummy1))
    }
    
    Try it!
  • #ODNB novelists missing on enWP
    SELECT ?item ?itemLabel
    WHERE {
      ?item wdt:P1415 ?dummy0 .
      OPTIONAL {
        ?wiki0 <http://schema.org/about> ?item .
        FILTER(SUBSTR(STR(?wiki0),1,24) = 'https://en.wikipedia.org') .
      }
      FILTER(!bound(?wiki0))
      ?item wdt:P106 wd:Q6625963 .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • #Female ODNB novelists missing on enWP
    SELECT ?item WHERE {
      ?item wdt:P1415 ?dummy0 .
      OPTIONAL {
        ?wiki0 schema:about ?item .
        FILTER(SUBSTR(STR(?wiki0),1,24) = "https://en.wikipedia.org") .
      }
      FILTER(!bound(?wiki0))
      ?item wdt:P106 wd:Q6625963 .
      ?item wdt:P21 wd:Q6581072 .
    }
    
    Try it!
  • #Timeline of markup languages, ordering by inception date.
    SELECT DISTINCT ?entity ?entityLabel ?sl WHERE 
    {
      ?entity wdt:P31 wd:Q37045 .
      ?entity wdt:P571 ?sl . 
    SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } 
    } 
      ORDER by ?sl
    
    Try it!

prov:Edit

  • #Use ProvenanceOntology
     SELECT ?item ?itemLabel ?ref ?refLabel
      WHERE {
    
      ?item ?prop ?stmt .
      ?stmt prov:wasDerivedFrom/pr:P248 ?ref .
      ?ref wdt:P361? wd:Q7926668      
               
        SERVICE wikibase:label {
            bd:serviceParam wikibase:language "en" .
        }
    }
    
    Try it!
  • # average number of references on “date of death” statements
    # (note: number of references, not reference snaks; arithmetic mean, not median)
    SELECT (AVG(?count) AS ?avgCount) WHERE {
      {
        SELECT (COUNT(?reference) AS ?count) WHERE {
          [] p:P570 ?statement.
          OPTIONAL { ?statement prov:wasDerivedFrom ?reference. }
        }
        GROUP BY ?statement
      }
    }
    
    Try it!

FederatedEdit

  • #Map of “railway things” in Berlin, as classified by LinkedGeoData.org
    #defaultView:Map
    
    PREFIX lgdo: <http://linkedgeodata.org/ontology/>
    PREFIX geo: <http://www.opengis.net/ont/geosparql#>
    PREFIX geom: <http://geovocab.org/geometry#>
    PREFIX bif: <bif:>
    
    SELECT ?railwayThing ?geometry (SAMPLE(?label) AS ?label) ?layer WHERE {
      SERVICE <http://linkedgeodata.org/sparql> {
        ?railwayThing a lgdo:RailwayThing;
                      geom:geometry [ geo:asWKT ?geometry ].
        FILTER(bif:st_intersects(?geometry, bif:st_point(13.383333, 52.516667), 0.1))
        OPTIONAL { ?railwayThing rdfs:label ?label. }
        OPTIONAL { ?railwayThing a lgdo:Subway. BIND("subway"@en AS ?subway) }
        OPTIONAL { ?railwayThing a lgdo:Rail. BIND("rail"@en AS ?rail) }
        OPTIONAL { ?railwayThing a lgdo:LightRail. BIND("light rail"@en AS ?lightrail) }
        OPTIONAL { ?railwayThing a lgdo:Tramway. BIND("tramway"@en AS ?tramway) }
        OPTIONAL { ?railwayThing a lgdo:Platform. BIND("platform"@en AS ?platform) }
        OPTIONAL { ?railwayThing a lgdo:AbandonedRailway. BIND("abandoned railway"@en AS ?abandoned) }
        OPTIONAL { ?railwayThing a lgdo:HistoricThing. BIND("historic"@en AS ?historic) }
      }
      BIND(COALESCE(?subway, ?lightrail, ?rail, ?tramway, ?platform, ?abandoned, ?historic, "other"@en) AS ?layer)
    }
    GROUP BY ?railwayThing ?geometry ?layer
    
    Try it!
  • #Lars W.
    PREFIX wdt: <http://www.wikidata.org/prop/direct/>
    PREFIX wd: <http://www.wikidata.org/entity/>
    PREFIX type: <https://larsgw.github.io/ctj/rdf/#/type/>
    PREFIX cito: <http://purl.org/spar/cito/>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    
    SELECT 
    ?article_on_wikidata 
    ?species_name 
    ?species_on_wikidata 
    ?species_name_wikidata
    
    WHERE
      {
        ?item wdt:P932 ?pmcid ; cito:discusses ?species .
        ?species a type:binomial ; rdfs:label ?species_name .
     
    SERVICE <https://query.wikidata.org/bigdata/namespace/wdq/sparql/> {          
       ?article_on_wikidata wdt:P932 ?pmcid .
       ?species_on_wikidata wdt:P225 ?species_name ;
                       rdfs:label ?species_name_wikidata .
       FILTER(lang(?species_name_wikidata) = 'en')
      }
     }
    
    Try it!

ThematicEdit

PharmaceuticalsEdit

  • #subclasses of "pharmaceutical drug"
    SELECT ?item ?itemLabel
    WHERE {?item wdt:P279 wd:Q12140 .
     SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • #ATC code (P267) statements, code begins "D" (dermatological drugs)
    SELECT DISTINCT ?item ?itemLabel
    WHERE {?item wdt:P267 ?string .
           FILTER (STRSTARTS(?string, "D"))
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!

ArticlesEdit

SELECT ?item WHERE {
  ?item wdt:P2093 [] .
  ?item wdt:P356 [] .
  ?item wdt:P1433 wd:Q45998 .
}

Try it!

DNB/ODNBEdit

  • #DNB articles lacking main subject
    SELECT ?item WHERE {
      ?item wdt:P31 wd:Q19389637 .
      ?item wdt:P1433 wd:Q15987216 .
      OPTIONAL { ?item wdt:P921 ?dummy0 }
      FILTER(!bound(?dummy0))
    }
    
    Try it!
  • #DNB articles, first supplement, lacking main subject
    SELECT ?item WHERE {
      ?item wdt:P31 wd:Q19389637 .
      ?item wdt:P1433 wd:Q16014700 .
      OPTIONAL { ?item wdt:P921 ?dummy0 }
      FILTER(!bound(?dummy0))
    }
    
    Try it!
  • #DNB articles, second supplement, lacking main subject
    SELECT ?item WHERE {
      ?item wdt:P31 wd:Q19389637 .
      ?item wdt:P1433 wd:Q16014697 .
      OPTIONAL { ?item wdt:P921 ?dummy0 }
      FILTER(!bound(?dummy0))
    }
    
    Try it!
  • #All OBINs
    SELECT ?item 
    WHERE {?item wdt:P1415 ?dummy0 .
    }
    
    Try it!
  • All OBINs marked human
  • All OBINs marked female
  • All OBINs marked male
  • #Families with OBIN
          SELECT ?item ?itemLabel
        WHERE {
      ?item wdt:P31 wd:Q8436 .
      ?item wdt:P1415 ?dummy0 .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
    }
    
    Try it!
  • #OBINs marked none of "human", "fictional human", "family", "group of humans" and subclasses
    SELECT ?item ?itemLabel WHERE {
      ?item wdt:P1415 ?dummy0 .
      MINUS { ?item wdt:P31 wd:Q5 }
      MINUS { ?item wdt:P31 wd:Q8436 }
      MINUS { ?item wdt:P31*/wdt:P279* wd:Q16334295 }
      MINUS { ?item wdt:P31 wd:Q15632617 }
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • OBINs marked none of about 20 instances
  • ODNB women, dates of death
  • ODNB women novelists

(AG) ODNB imports which have the "parent item" description attached to the "child item" labels. Two queries that seem useful:

  • SELECT DISTINCT ?description1 ?item1 ?item1Label ?item2 ?item2Label
    {
    	{	SELECT DISTINCT ?item1 ?description1 ?item2
    		{
    			?item1 wdt:P1415 ?whatever1 .
    			?item2 wdt:P1415 ?whatever2 .
        		?item1 schema:description ?description1 .
        		?item2 schema:description ?description1 .
        		FILTER(LANG(?description1) = "en" && ?item1 != ?item2 && str(?item1) < str(?item2)  ) .
                FILTER (CONTAINS(str(?description1),'('))
    		}
    		LIMIT 1000
    	}
      	SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

This one gets all items which have identical descriptions *and* whose identical descriptions contain a bracket; this catches most of the cases where two paired items were imported at the same time. Some related queries to find entries which still use the ODNB summary -

  • SELECT DISTINCT ?description1 ?item1 ?item1Label ?item2 ?item2Label
    {
    	{	SELECT DISTINCT ?item1 ?description1 ?item2
    		{
    			?item1 wdt:P1415 ?whatever1 .
        		?item1 schema:description ?description1 .
               FILTER (CONTAINS(str(?description1),'<'))
    		}
    		LIMIT 10000
    	}
      	SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

This gets all the ones with garbled HTML in the import (there's a couple of hundred)

  • SELECT DISTINCT ?description1 ?item1 ?item1Label ?item2 ?item2Label
    {
    	{	SELECT DISTINCT ?item1 ?description1 ?item2
    		{
    			?item1 wdt:P1415 ?whatever1 .
        		?item1 schema:description ?description1 .
               FILTER (CONTAINS(str(?description1),'['))
    		}
    		LIMIT 10000
    	}
      	SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

All the ones with square brackets (usually only found in ODNB style, not ours)

  • SELECT DISTINCT ?description1 ?item1 ?item1Label ?item2 ?item2Label
    {
    	{	SELECT DISTINCT ?item1 ?description1 ?item2
    		{
    			?item1 wdt:P1415 ?whatever1 .
        		?item1 schema:description ?description1 .
               FILTER (CONTAINS(str(?description1),'),'))
    		}
    		LIMIT 10000
    	}
      	SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

Any with the bracketed dates followed by a comma and other text - usually a sign it's the ODNB description

  • SELECT DISTINCT ?description1 ?item1 ?item1Label ?item2 ?item2Label
    {
    	{	SELECT DISTINCT ?item1 ?description1 ?item2
    		{
    			?item1 wdt:P1415 ?whatever1 .
        		?item1 schema:description ?description1 .
               FILTER (CONTAINS(str(?description1),'–'))
    		}
    		LIMIT 10000
    	}
      	SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
    }
    
    Try it!

Any with a long dash rather than a hyphen (again, likely from ODNB)

MixedEdit

HansardEdit

Art UKEdit


Women in RedEdit

Examples on occupation
  • CLAIM[1415] AND nolink[enwiki] AND CLAIM[106:6625963] AND CLAIM[21:6581072] (WDQ syntax, see https://wdq.wmflabs.org/wdq/) generates the following SPARQL query based on female ODNB novelists missing on enWP: [19] As of 21 June 2016 it had 14 hits. As of 1 January 2017, 33 hits.
  • Artists 483501: 4 hits [20]
  • Painters 1028181: 26 hits [21]
  • Singers 177220: 6 hits [22]
  • Physicians 39631: 1 hit [23]
  • Surgeons 774306: 4 hits [24]
  • Midwives 18519: 0 hits [25]
  • Nurses 186360: 2 hits [26]
  • Journalists 1930187: 5 hits [27]
  • Activists 15253558: 6 hits [28]
  • Philanthropists 13472585: 3 hits [29]
  • Writers 36180: 20 hits [30]
  • Merchants 215536: 2 hits [31]
  • Politicians 82955:3 hits [32]
  • Broadcasters 15958754: 1 hit [33]
  • Actors 33999: 15 hits [34]
  • Nuns 191808: 1 hit [35]
  • Officers 189290: 1 hit [36]
  • Shopkeepers 7501153: 1 hit [37]
  • Modistes (milliners) 18199649: 1 hit [38]
  • Textile artists 10694573: 1 hit [39]
  • Embroiderers 18575684: 1 hit [40]
Example on identifier
  • CLAIM[1415] AND nolink[enwiki] AND CLAIM[21:6581072] AND NOCLAIM[214] (WDQ syntax) generates the following SPARQL query based on female ODNB entries missing VIAF identifier: [41] Had 1728 hits on 22 June 2016.
Example on nationality
  • CLAIM[1415] AND nolink[enwiki] AND CLAIM[21:6581072] AND CLAIM[27:39] (WDQ syntax) generates the following SPARQL query based on Swiss female ODNB entries: [42]

Without ODNB, CLAIM[21:6581072] AND CLAIM[27:39] AND nolink[enwiki]: [43]

HistropediaEdit

SPARQL box at http://www.histropedia.com/showcase/wikidata-viewer.html

  • Prime Ministers of UK
#Prime Ministers of the United Kingdom
SELECT ?person ?personLabel ?birthDate ?image

WHERE {
  ?person wdt:P39 wd:Q14211 .
  ?person wdt:P569 ?birthDate .
  ?person wdt:P18 ?image
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
  }

Try it! NB start date= birthDate, In viewer

  • Places within 5 km of Edinburgh[44]
  • Female archaeologists[45]

ContentMineEdit

  • Subset of "1000 key topics" with "medical specialty"
    #Subset of "1000 key topics" with "medical specialty" SELECT DISTINCT ?item ?itemLabel  ?medspec ?medspecLabel
    WHERE {
      ?item wdt:P972 wd:Q5460604 .
      ?item wdt:P1995 ?medspec  .
      SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    
    Try it!
  • Subset of "1000 key topics" with "medical specialty", packed bubble chart (anomaly for "infectious disease")
    #defaultView:BubbleChart
    SELECT ?medspec ?medspecLabel ?count
    WHERE 
    {
      {
        SELECT ?medspec (COUNT(?item) AS ?count)    
    WHERE {
            ?item wdt:P972 wd:Q5460604 .
      ?item wdt:P1995 ?medspec  .
            
           }
      GROUP BY ?medspec 
      }
       SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
    }
    ORDER BY DESC(?count)
    LIMIT 100
    
    Try it!
  • Items with health specialty (P1995) being pulmonology (Q203337)
SELECT ?item ?itemLabel
WHERE {
  
  ?item wdt:P1995 wd:Q203337 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}

Try it!

WDQEdit

Royal Academy, CLAIM[463:270920] AND link[enwiki]

Autolist, statsEdit

Autolist now redirects to Petscan

uriburnerEdit

See alsoEdit