Wikidata:SPARQL query service/Building a query/Museums on Instagram
A short, annotated guide to building a simple SPARQL query. Comments are preceded by a "#".
Step 1 : List all museums
[edit]We start with a short & simple query, to list all museums, that is, every item that has a statement instance of (P31) with the value museum (Q33506).
- Try it!
SELECT ?item WHERE { ?item wdt:P31 wd:Q33506 . # The full stop (period) is required! }
Step 2 : Add their labels
[edit]Great, we get a long list of results... that only contains a list of "Q" IDs ! not very readable. So we modify the query to show the name of the museum, by adding ?itemLabel
to the SELECT line and invoking the "label" service:
- Try it!
SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q33506 . SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } # "en" shows the labels in English }
Step 3 : Also subclass of Museums
[edit]Wait... does Wikidata contains _only_ that many museums ? it does not feel right, we should get more. Besides, some famous museums, like Louvre Museum (Q19675), are not part of our list of results ! This is because some entries are not directly related to museum (Q33506), but to a more specific type, a subclass of museum (Q33506). For example Louvre Museum (Q19675) is linked to art museum (Q207694) and to other type of museums, but not directly to museum (Q33506).
So in addition to instance of (P31), we should also follow subclass of (P279) an indefinite number of times to reach museum (Q33506). This is indicated by "wdt:P31/wdt:P279*" (the "*" means "indefinite number of times") :
- Try it!
SELECT ?item ?itemLabel WHERE { ?item wdt:P31/wdt:P279* wd:Q33506 . # Link to Museums or a subclass of museum SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . }
Step 4 : Only museums with an instagram account
[edit]Now let's restrict our query by selecting only those museums that have the statement Instagram username (P2003), in a variable ?instagram
, so that we can refer to it later :
- Try it!
SELECT ?item ?itemLabel WHERE { ?item wdt:P31/wdt:P279* wd:Q33506 . ?item wdt:P2003 ?instagram . # Keep only museums that have an Instagram account SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . }
Note that we get much fewer results, as museums without instagram account in Wikidata are no longer shown.
Step 5 : Also select the instagram account
[edit]But we also want to see the Instagram account in a column of our result ! so we add ?instagram
(that's the label we chose in previous step) to the SELECT line:
- Try it!
SELECT ?item ?itemLabel ?instagram WHERE { ?item wdt:P31/wdt:P279* wd:Q33506 . ?item wdt:P2003 ?instagram . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . }
Step 6 : Also select coordinates
[edit]Museums are places, so let's also add their coordinate location (P625), which we will label as ?coordinates
:
- Try it!
SELECT ?item ?itemLabel ?instagram ?coordinates WHERE { ?item wdt:P31/wdt:P279* wd:Q33506 . ?item wdt:P2003 ?instagram . ?item wdt:P625 ?coordinates . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . }
Note that we may get fewer results, as museums without coordinates are no longer shown.
Step 7 : Display results on a Map
[edit]Now we have the coordinates, we can see the museums on a map, by setting #defaultView:Map
:
- Try it!
#defaultView:Map SELECT ?item ?itemLabel ?instagram ?coordinates WHERE { ?item wdt:P31/wdt:P279* wd:Q33506 . ?item wdt:P2003 ?instagram . ?item wdt:P625 ?coordinates . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . }
Step 8 : Simplify a little bit
[edit]We can rewrite this a bit to simplify — replacing two periods by semicolons and dropping the "subject", somewhat akin to how you could merge ordinary sentences in major natural languages:
- Try it!
#defaultView:Map SELECT ?item ?itemLabel ?instagram ?coordinates WHERE { ?item wdt:P31/wdt:P279* wd:Q33506 ; wdt:P2003 ?instagram ; wdt:P625 ?coordinates . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . }
Step 9 : Get a direct URL to our query
[edit]Finally, here is the URL for the actual map:
The map is regenerated every time the query is run, so it will always be up-to-date (usually within a few seconds or minutes).