Help:SPARQL query service/examples
Advanced search
[edit]These basic queries help to understand SPARQL and the Wikibase RDF format.
Search bahai.works and order results chronologically
[edit]SELECT ?title ?year WHERE {
# 1. Find pages matching the keyword
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "bahai.works";
wikibase:api "Generator";
mwapi:generator "search";
mwapi:gsrsearch "Your Search Terms Here";
mwapi:gsrlimit "20".
?titleStr wikibase:apiOutput mwapi:title.
}
# 2. Get categories for those specific pages
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "bahai.works";
wikibase:api "Categories";
mwapi:titles ?titleStr.
?cat wikibase:apiOutput mwapi:category.
}
# 3. Extract Year from "Category:1908"
BIND(STR(?cat) AS ?catStr)
FILTER(REGEX(?catStr, "Category:[0-9]{4}$"))
BIND(xsd:integer(STRAFTER(?catStr, "Category:")) AS ?year)
# 4. Create clickable URI
BIND(URI(CONCAT("https://bahai.works/", REPLACE(?titleStr, " ", "_"))) AS ?title)
} ORDER BY ?year
Membership
[edit]Current membership of the Universal House of Justice
[edit]# Find current members of an institution
SELECT ?person ?personLabel ?start_date WHERE {
?person p:P55 ?statement.
?statement ps:P55 wd:Q202. # e.g., The Universal House of Justice
?statement pq:P56 ?start_date.
# This is the key part: ensures there is no "end time" (P57) value
FILTER NOT EXISTS { ?statement pq:P57 [] }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
ORDER BY ?start_date
Members of the House who previously served on the ITC
[edit]#defaultView:Table
SELECT ?person ?personLabel ?itc_end_date ?uhj_start_date WHERE {
# Find their statement about serving on the House of Justice
?person p:P55 ?statement_uhj.
?statement_uhj ps:P55 wd:Q202. # Universal House of Justice
?statement_uhj pq:P56 ?uhj_start_date.
# Find their statement about serving on the International Teaching Centre
?person p:P55 ?statement_itc.
?statement_itc ps:P55 wd:Q3930. # International Teaching Center
?statement_itc pq:P57 ?itc_end_date.
# Ensure the UHJ appointment started after the ITC service ended
FILTER(?uhj_start_date > ?itc_end_date)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
ORDER BY ?uhj_start_date
Timelines
[edit]Birth and death dates
[edit]#defaultView:Timeline
SELECT ?person ?personLabel ?birth ?death
WHERE
{
?person wdt:P16 ?birth. # birth date
OPTIONAL { ?person wdt:P17 ?death. } # death date
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
ORDER BY ?birth
Membership on the Universal House of Justice
[edit]#defaultView:Timeline
SELECT ?person ?personLabel ?start ?end WHERE {
?person p:P55 ?statement.
?statement ps:P55 wd:Q202. # Position held: Universal House of Justice
OPTIONAL { ?statement pq:P56 ?start. }
OPTIONAL { ?statement pq:P57 ?end. }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
}
ORDER BY ?start
Maps
[edit]All items displayed as dots on a map
[edit]#defaultView:Map
SELECT ?item ?itemLabel ?coords ?bahaipedia_link ?bahaimedia_link WHERE {
# Find all items that have a coordinate location (P20)
?item wdt:P20 ?coords.
# Get the Bahaipedia sitelink, if it exists
OPTIONAL {
?bahaipedia_link schema:about ?item;
schema:isPartOf <https://bahaipedia.org/>.
}
# Get the Bahai.media sitelink, if it exists
OPTIONAL {
?bahaimedia_link schema:about ?item;
schema:isPartOf <https://bahai.media/>.
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
External federated query (of bahai-library.com)
[edit]Bahai-library.com tags with bahaidata IDs
[edit]SELECT ?s ?bahaidataEntity WHERE {
SERVICE <https://bahai-library.com/sparql> {
?s <http://purl.org/dc/elements/1.1/identifier> ?o .
}
FILTER(STRSTARTS(STR(?o), "bahaidata:"))
BIND(STRAFTER(STR(?o), "bahaidata:") AS ?id)
BIND(IRI(CONCAT("https://bahaidata.org/", ?id)) AS ?bahaidataEntity)
}
ORDER BY ?s
Project maintenance
[edit]Items missing P12 Instance of
[edit]# Find items that do NOT have a P12 ("instance of") statement
SELECT ?item ?itemLabel WHERE {
# This selects all entities that are explicitly typed as an item.
# On large wikibases, you may want to add more conditions here to narrow the search.
?item a wikibase:Item .
# This is the key part: ensures there is no statement using the P12 property.
FILTER NOT EXISTS { ?item wdt:P12 [] . }
# Get the labels for the items.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
LIMIT 500
Table Of Contents
- 1.1 Advanced search
- 2.3 Membership
- 3.6 Timelines
- 4.9 Maps
- 5.11 External federated query (of bahai-library.com)
- 6.13 Project maintenance