Difference between revisions of "SPARQL query service/queries/examples"
Jump to navigation
Jump to search
(Replaced content with "__TOC__ == Simple queries == These basic queries help to understand SPARQL and the Wikibase RDF format. === Sample === {{SPARQL|query=SELECT ?Item ?OccursInGameLabel ?Wa...") Tag: Replaced |
|||
(One intermediate revision by the same user not shown) | |||
Line 21: | Line 21: | ||
WHERE | WHERE | ||
{ | { | ||
?item wdt:P31 wd:Q146. # | ?item wdt:P31 wd:Q146. # Must be of a cat | ||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # | SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language | ||
} | } | ||
}} | }} |
Latest revision as of 16:37, 23 September 2021
Simple queries
These basic queries help to understand SPARQL and the Wikibase RDF format.
Sample
SELECT ?Item ?OccursInGameLabel ?WallClock ?GameClock ?ItemLabel
WHERE {
?Item wdt:P1 wd:Q1585; # All instances of American football plays
wdt:P19* wd:Q1902. # 2020 Iron Bowl
?Item wdt:P78 ?WallClock.
?Item wdt:P22 ?GameClock.
?Item wdt:P18 ?OccursInGame
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Cats
SELECT ?item ?itemLabel
WHERE
{
?item wdt:P31 wd:Q146. # Must be of a cat
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language
}
Horses (showing some info about them)
#Illustrates optional fields, instances of subclasses, language fallback on label service, date to year conversion
#title: Horses on Wikidata
SELECT DISTINCT ?horse ?horseLabel ?mother ?father (year(?birthdate) as ?birthyear) (year(?deathdate) as ?deathyear) ?genderLabel
WHERE {
?Item wdt:P1 wd:Q1585; # All instances of American football plays
wdt:P19* wd:Q1902. # 2020 Iron Bowl
?Item wdt:P78 ?WallClock.
?Item wdt:P22 ?GameClock.
?Item wdt:P18 ?OccursInGame
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
Cats, with pictures
#defaultView:ImageGrid
SELECT ?item ?itemLabel ?pic
WHERE
{
?item wdt:P31 wd:Q146 .
?item wdt:P18 ?pic
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
Map of hospitals
#added 2017-08
#defaultView:Map
SELECT distinct * WHERE {
?item wdt:P31/wdt:P279* wd:Q16917;
wdt:P625 ?geo .
}
Number of humans in Wikidata
Using the count function
#title: Number of humans in Wikidata
SELECT (COUNT(*) AS ?count)
WHERE {
?item wdt:P31 wd:Q5 .
}
Humans without children
In the simplest form:
#Demonstrates "no value" handling
#title: Humans without children
SELECT ?human ?humanLabel
WHERE
{
?human wdt:P31 wd:Q5 . #find humans
?human rdf:type wdno:P40 . #with at least one truthy P40 (child) statement defined to be "no value"
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
A similar query which also gives non-truthy "no value" statements:
SELECT ?human ?humanLabel
WHERE
{
?human wdt:P31 wd:Q5 . #find humans
?human p:P40 ?childStatement . #with at least one P40 (child) statement
?childStatement rdf:type wdno:P40 . #where the P40 (child) statement is defined to be "no value"
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . }
}
Humans born in New York City
This example highlights the correct way to use the P19 property and by extension the P20 property. P19 is most specific known place of birth. So for example it is known that Donald Trump (Q22686) was born in Jamaica Hospital (Q23497866). Therefore he wouldn't show up in the first query.
Incorrect way:
select distinct ?item ?itemLabel ?itemDescription ?sitelinks where {
?item wdt:P31 wd:Q5; # Any instance of a human.
wdt:P19 wd:Q60; # Who was born in New York City.
# Note. Doesn't include humans with the birth place listed as a hospital
# or an administrative area or other location of New York City.
# Only humans listed as born in New York City.
wikibase:sitelinks ?sitelinks.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,nl" }
}
ORDER BY DESC(?sitelinks)
Correct way:
#title: Humans born in New York City
select distinct ?item ?itemLabel ?itemDescription ?sitelinks where {
?item wdt:P31 wd:Q5; # Any instance of a human.
wdt:P19/wdt:P131* wd:Q60; # Who was born in any value (eg. a hospital)
# that has the property of 'administrative area of' New York City or New York City itself.
wikibase:sitelinks ?sitelinks.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,nl" }
}
ORDER BY DESC(?sitelinks)
Items with a Wikispecies sitelink
#illustrates sitelink selection, ";" notation
#title: Items with a Wikispecies sitelink
SELECT ?item ?itemLabel ?article
WHERE
{
?article schema:about ?item ;
schema:isPartOf <https://species.wikimedia.org/> .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
LIMIT 200
Items about authors with a Wikispecies page
#title: Items about authors with a Wikispecies page
SELECT ?author ?authorLabel (COUNT(?paper) AS ?count)
WHERE
{
?article schema:about ?author ;
schema:isPartOf <https://species.wikimedia.org/> .
?author wdt:P31 wd:Q5.
?paper wdt:P50 ?author.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
}
GROUP BY ?author ?authorLabel
ORDER BY DESC(?count)
LIMIT 200
Lexeme queries
Forms in Swedish that have no example demonstrating them
#title:Forms in Swedish that have no example demonstrating them
# 2021-08-06
select ?form ?lemma
WHERE {
?lexemeId dct:language wd:Q9027;
wikibase:lemma ?lemma;
ontolex:lexicalForm ?form.
MINUS {
?lexemeId p:P5831 ?statement.
?statement ps:P5831 ?example;
pq:P6072 [];
pq:P5830 ?form_with_example.
}
}