Skip to content

Instantly share code, notes, and snippets.

@knbknb
Last active June 23, 2023 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knbknb/d18741bf892efae68f726610c3222a38 to your computer and use it in GitHub Desktop.
Save knbknb/d18741bf892efae68f726610c3222a38 to your computer and use it in GitHub Desktop.
SPARQL: Queries for/from Wikidata. Stackoverflow, Blogposts, ForumPosts, Lecture Notes (e.g.2020 OpenHPI course "Knowledge Graphs")
# https://www.wikidata.org/wiki/Wikidata:SPARQL_query_service/query_optimization
# if previous statement is rangeSafe , add:
hint:Prior hint:rangeSafe true.
# This tells the optimizer that ?dateOfBirth doesn’t mix
# dates, strings, integers or other data types,
# which simplifies the range comparison.
# This is almost always true on Wikidata,
# so the main reason not to use this optimizer hint all the time is
# that it’s a bit inconvenient.
# (It’s probably also not a good idea to use it
# when you’re working with unknown values.)
# lots of interesting facts about
# writer Robert Louis Stevenson, (Label-Langs: en, fr, de)
#
# http://scihi.org/robert-louis-stevenson/
# knb 20201218
#
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX schema: <http://schema.org/>
SELECT ?item ?articlename ?itemLabel ?itemDescription ?sl
WHERE {
VALUES ?dod {"+1939-11-05"^^xsd:dateTime}
?dod ^wdt:P569 ?item .
?item wikibase:sitelinks ?sl .
?item ^schema:about ?article .
?article schema:isPartOf <https://en.wikipedia.org/>;
schema:name ?articlename .
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
?item rdfs:label ?itemLabel .
?item schema:description ?itemDescription .
}
BIND(REPLACE(?itemLabel, "^.*(?<! [Vv][ao]n| [Dd][aeiu]| [Dd][e][lns]| [Ll][ae]) (?!([SJ]r\\.?|[XVI]+)$)", "") AS ?sortname)
} ORDER BY DESC(?sl) ASC(UCASE(?sortname)) ASC(UCASE(?itemLabel))
‎‎​
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?property ?propertyLabel ?value ?valueLabel WHERE {
?property a wikibase:Property;
wikibase:directClaim ?wdt;
?wdt ?value.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?property
#-- https://w.wiki/3egQ
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
# list all properts, in clear text, with
SELECT ?propLabel ?oLabel WHERE {
hint:Query hint:optimizer "None" . #disable query optimizer
?prop wikibase:directClaim ?p . #connect wdt namespace with wd namespace
wd:Q8772 ?p ?o .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
# first 100 properties . Starts with P22,P25 (father, mother)
# select ?p {
# ?p a wikibase:Property .
# bind (xsd:integer(substr(str(?i), 33)) as ?order)
# } order by ?order
# offset 10 limit 20
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX bd: <http://www.bigdata.com/rdf#>
# fist 100 entities. Starts with Q1, Universe.
select distinct ?item ?number {
?n wdt:P31 wd:Q21199 ; wdt:P1181 ?number .
filter (?number >=1 && ?number < 100)
bind(iri(concat(str(wd:), "Q", str(?number))) as ?item)
} order by ?number
#defaultView:Timeline
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT ?authorLabel ?bookLabel ?date #?image
WHERE {
?book wdt:P31 wd:Q47461344 .
?book wdt:P50 ?author .
#?author wdt:P18 ?image .
?book wdt:P577 ?date .
FILTER (?date > "1900-01-01"^^xsd:dateTime)
?book rdfs:label ?bookLabel.
FILTER (LANG(?bookLabel)="en")
FILTER regex (?bookLabel,"sex","i") .
?author rdfs:label ?authorLabel.
FILTER (LANG(?authorLabel)="en")}
ORDER BY ?date
# simple query, using a property path
# "Who else was influenced by the influencers of George Orwell?"
SELECT ?influencedByInfluencersLabel
WHERE {
wd:Q3335 wdt:P737/^wdt:P737 ?influencedByInfluencers .
FILTER(wd:Q3335 != ?influencedByInfluencers)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
#defaultView:Graph
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
PREFIX dbo: <http://dbpedia.org/ontology/>
# Living Polititcians which are environmentalists,
# display as Portrait cloud on https://query.wikidata.org/
SELECT DISTINCT ?wditem ?wditemLabel ?date ?image
WHERE {
SERVICE <http://dbpedia.org/sparql> { #Select from DBpedia
?item dct:subject|dct:subject/skos:broader|dct:subject/skos:broader/skos:broader dbc:Environmentalists ;
# Select all from Wikipedia Category "Environmenalist" or its subcategories
owl:sameAs ?wditem
FILTER regex (?wditem, "wikidata.org") .
} # draw the connection to Wikidata
SERVICE <https://query.wikidata.org/sparql> { #Select from Wikidata
?wditem wdt:P106 wd:Q82955 . # the subject must have the occupation (P106) Politician (Q82955)
?wditem wdt:P569 ?date FILTER NOT EXISTS {?wditem wdt:P570 ?date2 } .
# select the birthdate (P569) of still living (not P570) Politicians
?wditem wdt:P18 ?image . # Select an image (P18)
?wditem rdfs:label ?wditemLabel FILTER (LANG(?wditemLabel)="en") .
}
} #Labels should be taken in English
ORDER BY ?date
LIMIT 50
# lots of interesting facts about
# writer Robert Louis Stevenson, (Label-Langs: en, fr, de)
#
# http://scihi.org/robert-louis-stevenson/
# knb 20201218
#
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX bd: <http://www.bigdata.com/rdf#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
#FILTER NOT EXISTS {?value wdt:P629 ?x }
SELECT DISTINCT ?t ?value ?valueLabel ?rrLabel ?rr (SAMPLE(?image) AS ?img) WHERE {
VALUES ?person {
wd:Q1512
}
{
?person wdt:P570 ?death;
?prop ?t.
FILTER((DATATYPE(?t)) = xsd:dateTime)
FILTER(?t <= ?death)
?value wikibase:directClaim ?prop.
}
UNION
{
?person ?q ?statement.
FILTER(REGEX(STR(?q), "prop/P"))
?person 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 # author of / creator of
{
?value (wdt:P50|wdt:P98|wdt:P655|wdt:P1773|wdt:P170) ?person.
?person wdt:P570 ?death;
wdt:P569 ?birth.
?value (wdt:P571|wdt:P577) ?t.
FILTER((?t <= ?death) && (?t >= ?birth))
FILTER(NOT EXISTS { ?person wdt:P800 ?value. })
OPTIONAL { ?value wdt:P1433 ?rr. }
OPTIONAL { ?value wdt:P18 ?image. }
}
UNION #Notable Works
{
?person wdt:P800 ?rr;
wdt:P570 ?death;
wdt:P569 ?birth.
?rr (wdt:P577|wdt:P571|wdt:P1191) ?t.
FILTER((?t <= ?death) && (?t >= ?birth))
OPTIONAL { ?rr wdt:P18 ?image. }
}
UNION #discoverer or inventor
{
?rr wdt:P61 ?person.
?person wdt:P570 ?death;
wdt:P569 ?birth.
?rr (wdt:P575|wdt:P571|wdt:P585) ?t.
FILTER((?t <= ?death) && (?t >= ?birth))
OPTIONAL { ?rr wdt:P18 ?image. }
}
UNION #architect
{
?rr wdt:P84 ?person.
?person wdt:P570 ?death;
wdt:P569 ?birth.
?rr (wdt:P571|wdt:P1619) ?t.
FILTER((?t <= ?death) && (?t >= ?birth))
OPTIONAL { ?rr wdt:P18 ?image. }
}
UNION #composer
{
?rr wdt:P86 ?person.
?person wdt:P570 ?death;
wdt:P569 ?birth.
?rr (wdt:P571|wdt:P1191|wdt:P577) ?t.
FILTER((?t <= ?death) && (?t >= ?birth))
OPTIONAL { ?rr wdt:P18 ?image. }
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en, fr, de". }
}
GROUP BY ?t ?value ?rr ?rrLabel ?valueLabel
ORDER BY (?t)
# wikidata prperties and humanreadable descriptions
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
SELECT ?property ?propertyLabel ?value ?valueLabel WHERE {
?property a wikibase:Property;
wikibase:directClaim ?wdt;
?wdt ?value.
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
}
}
ORDER BY ?property
#-- https://w.wiki/3egQ
# see https://stackoverflow.com/questions/56539793/wikidata-sparql-get-company-entities-and-the-location-of-their-headquarters
# wikidata
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX schema: <http://schema.org/>
SELECT
?company ?companyLabel ?isin ?web ?country ?countryLabel ?inception ?hq ?hqLabel ?hqloc
WHERE
{
?article schema:inLanguage "en" .
?article schema:isPartOf <https://en.wikipedia.org/>.
?article schema:about ?company .
?company p:P31/ps:P31/wdt:P279* wd:Q783794.
?company wdt:P946 ?isin.
OPTIONAL {?company wdt:P856 ?web.}
OPTIONAL {?company wdt:P571 ?inception.}
OPTIONAL {?company wdt:P17 ?country.}
OPTIONAL {?company p:P159 ?hqStmt . # get HQ statement
?hqStmt ps:P159 ?hq . # get HQ item
?hqStmt pq:P625 ?hqloc . # get HQ coordinate location qualifier
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 100
## Timeline Plot of Frederick Sanger, Biochemist / DNA Sequencer
#defaultView:Timeline
# wikidata prefixes
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?t ?rrLabel ?valueLabel WHERE {
{
wd:Q151564 ?prop ?t.
FILTER((DATATYPE(?t)) = xsd:dateTime)
?value wikibase:directClaim ?prop.
}
UNION
{
wd:Q151564 ?q ?statement.
?statement ?p ?t.
?statement ?r ?value.
FILTER(REGEX(STR(?q), "prop/P"))
FILTER(REGEX(STR(?p), "prop/qualifier/P"))
FILTER((DATATYPE(?t)) = xsd:dateTime)
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.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
## Timeline Plot of Frederick Sanger, Biochemist / DNA Sequencer
# defaultView:Timeline
# wikidata prefixes
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?t ?rrLabel ?valueLabel
WHERE
{
{wd:Q151564 ?prop ?t FILTER (datatype(?t)=xsd:dateTime) .
?value wikibase:directClaim ?prop. }
UNION
{
wd:Q151564 ?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 .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment