User:Charles Matthews/Queries
Warehouse and examples list for queries.
Vanilla SPARQL edit
- Try it!
#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!
#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!
#Single item, all statements SELECT * WHERE {wd:Q1369941 ?p ?o}
Seeing round the corner edit
- 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!
#Given names shown with family name item SELECT ?item ?itemLabel ?nameLabel WHERE {?item wdt:P31 wd:Q5; wdt:P735 ?name. ?name wdt:P31 wd:Q101352. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } LIMIT 100
SPARQL features edit
Handling subclasses edit
- Subclasses of "literary work"
SELECT ?s ?sLabel
WHERE
{
?s wdt:P279 wd:Q7725634 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
}
- From StackExchange thread
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
}
- Units of measurement
SELECT ?q ?qLabel {
?q wdt:P31|wdt:P279* wd:Q47574 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
- 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" }
}
- 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". }
}
- 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" }
}
Regex etc. string operations edit
- 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')).
}
- 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)$").
}
- 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 .
}
- Abbreviated systematic name by regex
SELECT ?q ?name { ?q wdt:P225 ?name FILTER REGEX(?name,'^O\\S+ bicolor$') }
- 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" }
}
- 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$") .
}
- 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$") .
}
- 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" }
}
- 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" } }
Maps edit
- Try it!
#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" } }
- 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". } }
- 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". } }
- Latin American women missing on esWP
- Try it!
#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
- Variant map default view, London Underground IRL map]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
Graph view edit
- Try it!
#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.
Graph patterns edit
- Try it!
# 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". } }
Bar chart view edit
- Try it!
#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
Timeline view edit
- Try it!
#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
- 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 view edit
- 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
- 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
- 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
- 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
- ISBN-13 bubble chart [7]
- World Cup scorers bubble chart [8]
- Try it!
#"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)
LineChart view edit
- Try it!
#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
ScatterChart view edit
ImageGrid view edit
- 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". }
}
- 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!
<strong class="error"><span class="scribunto-error" id="mw-scribunto-error-89c92b51">Lua error in Module:ConcatArgs at line 36: invalid value (nil) at index 1 in table for 'concat'.</span></strong>
- Astrolabes (Poulter)[9]
- Try it!
#defaultView:ImageGrid SELECT ?q ?img { ?q wdt:P170 wd:Q52914924 ; wdt:P18 ?img } LIMIT 2000
used in [10] from Commons
SPARQL Keywords edit
Use of BIND edit
- Try it!
# 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
- 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)
Use of CONTAINS edit
- Try it!
# 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' ") |
Use of COUNT edit
- Try it!
# number of author (P50) statements # runtime: instantaneous SELECT (COUNT(*) AS ?count) WHERE { [] p:P50 []. }
Use of FILTER edit
- Demonyms
SELECT ?country ?name {
?country wdt:P31 wd:Q6256 .
?country wdt:P1549 ?name .
FILTER ( lang(?name)='en')
}
- 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"
}
}
- 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)) }
- 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)
- 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()
Use of FILTER NOT EXISTS edit
- Try it!
#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
Use of FILTER isBLANK edit
# 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' }
}
Use of GROUP BY edit
#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-89c92b51">Lua error in Module:ConcatArgs at line 36: invalid value (nil) at index 1 in table for 'concat'.</span></strong>
- [11]Try it!
<strong class="error"><span class="scribunto-error" id="mw-scribunto-error-89c92b51">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)
Use of GROUP CONCAT edit
- Try it!
# 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))
Use of HAVING edit
- Try it!
#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". } }
Use of IN edit
- Try it!
#"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)
Use of INCLUDE edit
- Try it!
#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
Use of NOT IN edit
- Try it!
#"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
Use of SAMPLE edit
- Try it!
#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)
Use of SUM edit
- Try it!
SELECT (SUM(?count) AS ?total) WHERE { ?item p:P1367/pq:P1114 ?count }
Use of UNION edit
- Try it!
# 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 }
Use of WITH edit
- Try it!
#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
Use of VALUES edit
- Try it!
#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)
rdfs edit
- Try it!
#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
skos edit
- Try it!
#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" } }
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
schema.org edit
- 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" .
}
}
- 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 .
}
- 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 )
}
- 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" .
}
}
- 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" . } }
- Footprint, DNB00, SPARQL
- Footprint, DNB01, SPARQL
- Footprint, DNB12, SPARQL
- Try it!
#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 ) }
- from Ducharme [12]Try it!
#IMDb to WP conversion SELECT ?wppage WHERE { ?subject wdt:P345 'tt0064652' . ?wppage schema:about ?subject . FILTER(contains(str(?wppage),'//en.wikipedia')) }
- Try it!
#Counting across interwiki (Tagishsimon) SELECT ?wiki (count(?wiki) as ?count) WHERE { ?item wdt:P709 ?stat . ?article schema:about ?item ; schema:isPartOf ?wiki . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } group by ?wiki order by desc(?count)
sitelinks edit
- Try it!
#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". } }
MediaWiki API edit
- 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" }
}
- 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!
#(Ainali) Using mwapi to base a query on articles in a Wikipedia category # Use the MediaWiki API to get the articles from a specific category in a specific Wikipedia language version. # Then use this results as usual in a query, in this example a simple query for the gender. # By User:Ainali with the help of User:Dipsacus fullonum 2021-02 SELECT ?item ?itemLabel ?genderLabel WHERE { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "sv.wikipedia.org"; # Set the project here wikibase:api "Generator"; mwapi:generator "categorymembers"; # Selects the content from a category mwapi:gcmtitle "Category:Födda 2001";. # Specifies the category (Born in 2001) ?item wikibase:apiOutputItem mwapi:item. } FILTER BOUND (?item) # Safeguard to not get a timeout from unbound items when using ?item below ?item wdt:P21 ?gender . # Example retrieval of a value SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,sv". } }
embed.html edit
- Viennese composers and compositions by tonality Try it!
#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
schema.org edit
- 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 )
}
- 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" .
}
}
- 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/> .
}
- Wikisource authors, DNB No WP
- Footprint, DNB00, SPARQL
- Footprint, DNB01, SPARQL
- Footprint, DNB12, SPARQL
- Try it!
#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" .} }
wikibase: edit
- Try it!
#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!
#Andrew Gray SELECT ?item ?itemLabel WHERE { ?item wdt:P131 wd:Q22 . ?item wikibase:sitelinks ?linkcount . FILTER (?linkcount = 1) . # only include items with 1 or more sitelinks ?article schema:about ?item . ?article schema:inLanguage "fr" . ?article schema:isPartOf <https://fr.wikipedia.org/> . SERVICE wikibase:label { bd:serviceParam wikibase:language "en,fr" } } GROUP BY ?item ?itemLabel
- Try it!
#added before 2016-10, shows pr: -> wd: SELECT ?entity ?claim ?prop ?propClaim WHERE { ?prop wdt:P31 wd:Q15720608 . ?prop wikibase:reference ?propClaim . ?ref ?propClaim ?o . ?statement prov:wasDerivedFrom ?ref . ?entity ?claim ?statement . } LIMIT 100
- Try it!
#wikibase:reference to identify property used to reference SELECT distinct ?itemLabel ?date ?sourcePropertyLabel ?referenceLabel ?reference WHERE { ?item wdt:P39/wdt:P279 wd:Q1285463. ?item wdt:P569 ?date. OPTIONAL{ ?item p:P569 [ prov:wasDerivedFrom [ ?source ?reference ] ]. ?sourceProperty wikibase:reference ?source. } ?item p:P39 ?decla. ?decla pq:P580 ?ddebut. FILTER NOT EXISTS {?decla pq:P582 ?dfin. } FILTER (?date > "1952-01-01T00:00:00+00:00"^^xsd:dateTime) SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],fr". }. }
Performance edit
- Try it!
#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)
Misc SPARQL edit
- Try it!
#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
prov: edit
- Try it!
#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 } }
Federated edit
- Try it!
#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') } }
Thematic edit
Pharmaceuticals edit
- Try it!
#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" } }
Articles edit
- Author name string and DOI and published in Nature Reviews Drug Discovery (Q45998)
SELECT ?item WHERE {
?item wdt:P2093 [] .
?item wdt:P356 [] .
?item wdt:P1433 wd:Q45998 .
}
DNB/ODNB edit
- Try it!
#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 . }
- All OBINs marked human
- All OBINs marked female
- All OBINs marked male
- Try it!
#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" } }
- 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:
- Try it!
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". } }
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 -
- Try it!
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". } }
This gets all the ones with garbled HTML in the import (there's a couple of hundred)
- Try it!
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". } }
All the ones with square brackets (usually only found in ODNB style, not ours)
- Try it!
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". } }
Any with the bracketed dates followed by a comma and other text - usually a sign it's the ODNB description
- Try it!
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". } }
Any with a long dash rather than a hyphen (again, likely from ODNB)
Mixed edit
- Art UK with ULAN, showing optional OBIN
- All items with History of Parliament ID
- Try it!
#Persons with Legacies of British Slavery ID SELECT ?item ?itemLabel WHERE { ?item wdt:P3023 ?dummy0 . SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } }
- Catholic Hierarchy diocese ID and not an "instance of"
- Distribution maps
Hansard edit
- 2nd UK Parliament, no Hansard ID
- 3rd UK Parliament, no Hansard ID
- 4th UK Parliament, no Hansard ID
- 5th UK Parliament, no Hansard ID
- 6th UK Parliament, no Hansard ID
- 7th UK Parliament, no Hansard ID
- 8th UK Parliament, no Hansard ID
Art UK edit
- User:Charles Matthews/Art UK
- Art UK people with ULAN and OBIN labels
- As above with Art UK ID
- with enWP links
Women in Red edit
- w:User:Edgars2007/ODNB covers all ODNB women, Listeria-generated, so will refresh itself. As of June 2016 over 2100 entries.
- Female ODNB ids, no link to enWP, no occupation as of 21 June 2016 had 1872 hits
- 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: [21] As of 21 June 2016 it had 14 hits. As of 1 January 2017, 33 hits.
- Artists 483501: 4 hits [22]
- Painters 1028181: 26 hits [23]
- Singers 177220: 6 hits [24]
- Physicians 39631: 1 hit [25]
- Surgeons 774306: 4 hits [26]
- Midwives 18519: 0 hits [27]
- Nurses 186360: 2 hits [28]
- Journalists 1930187: 5 hits [29]
- Activists 15253558: 6 hits [30]
- Philanthropists 13472585: 3 hits [31]
- Writers 36180: 20 hits [32]
- Merchants 215536: 2 hits [33]
- Politicians 82955:3 hits [34]
- Broadcasters 15958754: 1 hit [35]
- Actors 33999: 15 hits [36]
- Nuns 191808: 1 hit [37]
- Officers 189290: 1 hit [38]
- Shopkeepers 7501153: 1 hit [39]
- Modistes (milliners) 18199649: 1 hit [40]
- Textile artists 10694573: 1 hit [41]
- Embroiderers 18575684: 1 hit [42]
- 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: [43] 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: [44]
Without ODNB, CLAIM[21:6581072] AND CLAIM[27:39] AND nolink[enwiki]: [45]
Histropedia edit
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" }
}
NB start date= birthDate, In viewer
ContentMine edit
- Subset of "1000 key topics" with "medical specialty" Try it!
#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" } }
- Subset of "1000 key topics" with "medical specialty", packed bubble chart (anomaly for "infectious disease") Try it!
#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
- 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" }
}
WDQ edit
Royal Academy, CLAIM[463:270920] AND link[enwiki]
Autolist, stats edit
Autolist now redirects to Petscan
- CLAIM[463:123885] AND CLAIM[1816] AND NOCLAIM[18] AND BETWEEN[570,1660,1900] AND CLAIM[373]
- CLAIM[1415] AND NOCLAIM[1343] AND BETWEEN[570, 1500, 1600]
- CLAIM[1343] AND link[enwiki] AND NOCLAIM[1415]
- DNB data items, CLAIM[31:19389637] and NOCLAIM[921] No main subject
- Working on CLAIM[1343] AND link[enwiki] AND NOCLAIM[1415] AND BETWEEN[570, 1879, 1900]
- With cutoff CLAIM[1367] AND NOCLAIM[245] AND BETWEEN[570, 1000, 1920]
- Ditto CLAIM[1367] AND NOCLAIM[373] AND BETWEEN[570, 1700, 1920]
Wikimedia Commons Query Service edit
# This query finds the most prolific photographers in an area around a Wikidata item (Magnus Manske)
SELECT ?creatorName ?creatorUrl (count(?creatorUrl) AS ?pictures_taken) WHERE {
BIND ( wd:Q868642 as ?centerItem ) . # CHANGE THIS ITEM TO SET THE NEW CENTER
BIND ( "5" as ?km ) . # CHANGE THIS TO SET THE SEARCH RADIUS (IN KM)
hint:Query hint:optimizer "None".
SERVICE <https://query.wikidata.org/sparql> { ?centerItem wdt:P625 ?center }
SERVICE wikibase:around {
?file wdt:P1259 ?coor.
bd:serviceParam wikibase:center ?center .
bd:serviceParam wikibase:radius ?km.
}
?file p:P170 ?creatorStatement .
?creatorStatement pq:P2699 ?creatorUrl .
?creatorStatement pq:P2093 ?creatorName .
}
GROUP BY ?creatorName ?creatorUrl
ORDER BY DESC(?pictures_taken)
uriburner edit
- Runs Wikidata+DBpedia federation from @kidehen
See also edit
- Wordle lexemes
- Wordle-related frequencies, Chris Groves
- https://wikidata.metaphacts.com/resource/wd:Q26229
- https://www.mediawiki.org/wiki/MW2SPARQL
- https://lucaswerkmeister.github.io/wikidata-ontology-explorer/
- https://tools.wmflabs.org/wikidata-todo/sparql_rc.php?sparql=
- wikidata-todo/user_edits with SPARQL
- https://github.com/wikimedia/wikidata-query-gui/blob/master/examples/app/nearby.html#L79
- BnF checks
- Tree of Life Explorer
- Ancestors tool, relator tool
- Wikidata generic tree
- flickr2commons
- https://query.wikidata.org/bigdata/ldf?
- https://query.wikidata.org/bigdata/ldf?subject=&predicate=http%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2FP921&object=
- https://lucaswerkmeister.github.io/wikidata-lexeme-graph-builder/?subjects=L88%2CL129&predicate=P5191, https://lucaswerkmeister.github.io/wikidata-lexeme-graph-builder/?subjects=L2087&predicate=P5191
- https://query.wikidata.org/bigdata/ldf?subject=&predicate=http%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2FP2860&object=, https://query.wikidata.org/bigdata/ldf?subject=&predicate=http%3A%2F%2Fwww.wikidata.org%2Fprop%2Fdirect%2FP2093&object=
- MeSH Treenumbers
- MeSH Treenumbers starting with C
- Topics for which the English Wikipedia wikilinks to the University of Virginia but the corresponding Wikidata entry does not
- Wikidata:WikiProject Source MetaData/Wikidata lists/Periodicals with no main subject statement
- sophox example, streets named after trees