SPARQL Examples
Jump to navigation
Jump to search
All -- records
All records associated with our various projects on Wikibase
# All records associated with our various projects on Wikibase
SELECT DISTINCT ?item ?itemLabel ?type ?typeLabel ?project ?projectLabel WHERE {
?item wdt:P1 ?type.
?item wdt:P11 ?project.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?projectLabel
All -- person records
#All person records associated with our projects on Wikibase
SELECT ?person ?personLabel ?project ?projectLabel
WHERE
{
?person wdt:P1 wd:Q1.
?person wdt:P11 ?project.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?projectLabel
All -- city records
#All city records associated with our projects on Wikibase
SELECT ?city ?cityLabel WHERE {
?city wdt:P1 wd:Q19058.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
All -- sponsored by
Show all the entities that have the property P155 ("sponsored by") claim with the value, what the item is and who they are sponsored by
#Show all the entities that have the property P155 ("sponsored by") claim with the value, what the item is and who they are sponsored by
SELECT ?item ?itemLabel ?sponsored_by ?sponsored_byLabel ?instance_of ?instance_ofLabel ?part_of_project ?part_of_projectLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
OPTIONAL { ?item wdt:P155 ?sponsored_by. }
OPTIONAL { ?item wdt:P1 ?instance_of. }
OPTIONAL { ?item wdt:P11 ?part_of_project. }
}
All -- locations of performances
#What are all the locations(venues) that people or performances were performed at and if availble, what was their city and state?
SELECT ?item ?itemLabel ?performed_at ?performed_atLabel ?location ?locationLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item wdt:P68 ?performed_at.
?item p:P68 ?statement.
OPTIONAL { ?statement pq:P125 ?location. }
}
All -- price of things
#Return the entity (regardless of "instance of" value) where there is a property P145 ("price") statement and the value of the P149 ("price type") qualifier (regardless of value).
SELECT DISTINCT ?item ?itemLabel ?instance_of ?instance_ofLabel ?price ?priceLabel ?price_type ?price_typeLabel WHERE {
?item wdt:P1 ?instance_of.
?item wdt:P145 ?price.
?item p:P145 ?statement.
OPTIONAL{ ?statement pq:P149 ?price_type.}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?price_typeLabel
All -- employers
Return the entity and entity label where P110 ("employer") claim is made and the value of that claim
#return the entity and entity label where P110 ("employer") claim is made and the value of that claim
SELECT ?item ?itemLabel ?employer ?employerLabel ?part_of_project ?part_of_projectLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
OPTIONAL { ?item wdt:P110 ?employer. }
OPTIONAL { ?item wdt:P11 ?part_of_project. }
}
All -- employees
#return the instance of organizations with claim of "employee" P179 and their values
SELECT DISTINCT ?item ?itemLabel ?employee ?employeeLabel WHERE {
?item wdt:P1 wd:Q19049;
wdt:P179 ?employee.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
All -- fuzzy string search
#Fuzzy match on a particular word (ex is with "tennis") in a block text
# fuzzy search
SELECT ?item ?label ?block_text WHERE
{
?item rdfs:label ?label.
?item wdt:P19 ?block_text
FILTER (CONTAINS(LCASE(STR(?block_text)), "tennis"))
}
Linked Jazz -- influences with reference text
#Return people in the linked jazz project and who influenced them with the oral history text where the statement came from
select ?person ?personLabel ?influencedBy ?influencedByLabel ?refBlock ?refBlockLabel ?text where{
?person wdt:P11 wd:Q21217.
?person p:P39 ?statement.
?statement ps:P39 ?influencedBy.
?statement prov:wasDerivedFrom ?refnode.
?refnode pr:P26 ?refBlock.
?refBlock wdt:P19 ?text
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
order by ?personLabel ?influencedByLabel
E.A.T. Knowledge Graph -- works and materials used
#return the entities and their instance of where the entities are part of project EAT + LOD and use the property P185 ("uses") claim and the value of the claim
SELECT Distinct ?item ?itemLabel ?instance_of ?instance_ofLabel ?uses ?usesLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item wdt:P11 wd:Q19104.
?item wdt:P1 ?instance_of.
?item wdt:P185 ?uses.
?item p:P185 ?statement.
}
ORDER BY ?itemLabel
E.A.T. Knowledge Graph -- materials used in works
#return the entities and their instance of where the entities are part of project EAT + LOD and use the property P194 ("used in") claim and the value of the claim
SELECT Distinct ?item ?itemLabel ?instance_of ?instance_ofLabel ?used_in ?used_inLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item wdt:P11 wd:Q19104.
?item wdt:P1 ?instance_of.
?item wdt:P194 ?used_in.
?item p:P194 ?statement.
}
ORDER BY ?itemLabel
E.A.T. Knowledge Graph -- connect blocks to parent documents
#query demonstrates the ability to identify the blocks that connect to the parent document which ultimately connects to the source of the document, allowing for verification of the material.
SELECT DISTINCT ?item ?label ?block_text ?parent_document ?parent_documentLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
{
SELECT DISTINCT ?item ?label ?block_text ?parent_document ?parent_documentLabel WHERE {
{
?item wdt:P11 wd:Q19104.
?item rdfs:label ?label;
wdt:P24 ?parent_document;
wdt:P19 ?block_text.
}
UNION
{
?item wdt:P11 wd:Q20517.
?item rdfs:label ?label;
wdt:P24 ?parent_document;
wdt:P19 ?block_text.
}
}
}
}
ORDER BY ASC (?parent_documentLabel)
Asian American Arts Centre Exhibition History -- list of exhibitions
#List of exhibitions with start and end times
select ?exhibition ?exhibitionLabel ?startTime ?endTime
where {
?exhibition wdt:P1 wd:Q22395.
?exhibition wdt:P11 wd:Q24831.
optional{
?exhibition wdt:P120 ?startTime .
?exhibition wdt:P121 ?endTime .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
American Federation of Musicians. Local 496 -- map of where musicians lived
#A map of the address of the homes of the members of Local 496
#defaultView:Map
SELECT ?person ?personLabel ?coord WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?person wdt:P1 wd:Q1;
wdt:P11 wd:Q20719;
?pq ?statement.
?statement pq:P146 ?coord.
}
Linking Lost Jazz Shrines -- Federated Wikidata query of locations
#Uses the Wikidata Qid stored in our Wikibase to ask Wikidata for the loocations and then combines the results to display on a map
#defaultView:Map
# define the property and item prefix used in Wikidata (in our wikibase we will still use wdt: and wd:)
PREFIX WIKIDATAwdt: <http://www.wikidata.org/prop/direct/>
PREFIX WIKIDATAwd: <http://www.wikidata.org/entity/>
SELECT * WHERE {
#Filter inside our wikibase for what we are looking for
?jazzShrine wdt:P1 wd:Q18806.
?jazzShrine wdt:P8 ?wikiDataQid.
# we can't do the ?xxxLabel trick so define what labels we want from out WB here
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?jazzShrine rdfs:label ?shrineLabel
}
# take the wikidata Qid stored in our wikibase and convert it to a Wikidata URI stored in ?wikiDatafiedQid
BIND(IRI(CONCAT(STR(WIKIDATAwd:), ?wikiDataQid)) AS ?wikiDatafiedQid)
# connect to wikidata
SERVICE <https://query.wikidata.org/sparql> {
# ask for the coordinates
?wikiDatafiedQid WIKIDATAwdt:P625 ?coords.
?wikiDatafiedQid WIKIDATAwdt:P276 ?location.
# same deal with the labels, but we use the same namespace as wikidata for these
SERVICE wikibase:label {
bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
?location rdfs:label ?locationLabel.
?wikiDatafiedQid schema:description ?description.
}
}
}