Help:SPARQL query service/examples/en
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]
See Help:SPARQL query service/bahai-library.com for more details.
Bahai-library.com tags with bahaidata IDs[edit]
SELECT ?s ?bahaidataEntity ?instanceOf WHERE {
# We query bahaiquery.org for bahai-library.com data as the latter's SPARQL does not allow joins
SERVICE <https://bahaiquery.org/sparql> {
?s <http://purl.org/dc/elements/1.1/identifier> ?o .
FILTER(STRSTARTS(STR(?o), "bahaidata:"))
# Extract the ID part after "bahaidata:" (e.g., "Q958")
BIND(STRAFTER(STR(?o), "bahaidata:") AS ?id)
# Construct the bahaidata.org entity URI
BIND(IRI(CONCAT("https://bahaidata.org/entity/", ?id)) AS ?bahaidataEntity)
}
# Join to other metadata, with, e.g., the following to show instance-of statements (may time out)
# ?bahaidataEntity wdt:P12 ?instanceOf .
}
Bahai-library.com authors[edit]
SELECT ?s ?author ?authorItem WHERE {
SERVICE <https://bahaiquery.org/sparql> {
?s <http://purl.org/dc/elements/1.1/description> ?author .
FILTER(STRSTARTS(STR(?s), "https://bahai-library.com/author/"))
}
# Uncommenting the following demonstrates joining with local data (the Bahaidata item whose label matches the author name exactly), but may time out
# OPTIONAL {
# ?authorItem rdfs:label ?label .
# FILTER(STR(?label) = STR(?author))
# }
} LIMIT 10
Bahai-library.com Partial Inventory[edit]
SELECT ?s ?instanceOf ?id ?bahaidataEntity ?bahaidataEntityLabel WHERE {
# We query bahaiquery.org for bahai-library.com data as the latter's SPARQL does not allow joins
SERVICE <https://bahaiquery.org/sparql> {
?s <http://purl.org/dc/elements/1.1/identifier> ?o .
FILTER(STRSTARTS(STR(?o), "partial-inventory:"))
BIND(STRAFTER(STR(?o), "partial-inventory:") AS ?id)
}
# Partial Inventory ID
?bahaidataEntity wdt:P65 ?id .
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],mul,en". }
# Join to other metadata if present, with, e.g., the following to show instance-of statements (may time out)
# ?bahaidataEntity wdt:P12 ?instanceOf .
}
Bahai-library.com Chronology[edit]
SELECT ?s ?o ?year ?yearItem ?sitelink WHERE {
# We query bahaiquery.org for bahai-library.com data as the latter's SPARQL does not allow joins
SERVICE <https://bahaiquery.org/sparql> {
?s <http://purl.org/dc/terms/URI> ?o .
FILTER(STRSTARTS(STR(?s), "https://bahai-library.com/chronology/"))
# Get the year out of the chronology URL
BIND(SUBSTR(STR(?s), 38, 4) AS ?year)
}
# Perform a search which is faster than `?yearItem rdfs:label ?year .`
SERVICE wikibase:mwapi {
bd:serviceParam wikibase:endpoint "bahaidata.org";
wikibase:api "EntitySearch";
mwapi:search ?year; # Replace with your search term
mwapi:language "en".
?yearItem wikibase:apiOutputItem mwapi:item.
# ?num wikibase:apiOrdinal true. # Captures search relevance rank
}
OPTIONAL {
# We should also be able to get metadata (in this case the bahaipedia link) for the yearItem (may time out)
?sitelink schema:about ?yearItem ;
# schema:isPartOf ?siteName .
# Filter for specific languages or sites
FILTER(CONTAINS(STR(?sitelink), "bahaipedia.org"))
}
} LIMIT 5
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.16 Project maintenance