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 museumsEdit

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).

SELECT ?item WHERE {
  ?item wdt:P31 wd:Q33506 . # The full stop (period) is required!
}
Try it!

Step 2 : Add their labelsEdit

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:

SELECT ?item ?itemLabel WHERE {
  ?item wdt:P31 wd:Q33506 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } # "en" shows the labels in English
}
Try it!

Step 3 : Also subclass of MuseumsEdit

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") :

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" } . 
}
Try it!

Step 4 : Only museums with an instagram accountEdit

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 :


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" } . 
}
Try it!

Note that we get much fewer results, as museums without instagram account in Wikidata are no longer shown.

Step 5 : Also select the instagram accountEdit

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:

SELECT ?item ?itemLabel ?instagram WHERE {
  ?item wdt:P31/wdt:P279* wd:Q33506 . 
  ?item wdt:P2003 ?instagram .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . 
}
Try it!

Step 6 : Also select coordinatesEdit

Museums are places, so let's also add their coordinate location (P625), which we will label as ?coordinates:

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" } .
}
Try it!

Note that we may get fewer results, as museums without coordinates are no longer shown.

Step 7 : Display results on a MapEdit

Now we have the coordinates, we can see the museums on a map, by setting #defaultView:Map:

#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" } .
}
Try it!

Step 8 : Simplify a little bitEdit

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:

#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" } .
}
Try it!

Step 9 : Get a direct URL to our queryEdit

Finally, here is the URL for the actual map:

https://query.wikidata.org/#%23defaultView%3AMap%0ASELECT%20%3Fitem%20%3FitemLabel%20%3Finstagram%20%3Fcoordinates%20WHERE%20%7B%0A%20%20%3Fitem%20wdt%3AP31%2Fwdt%3AP279%2a%20wd%3AQ33506%20%3B%0A%20%20%20%20%20%20%20%20wdt%3AP2003%20%3Finstagram%20%3B%0A%20%20%20%20%20%20%20%20wdt%3AP625%20%3Fcoordinates%20.%0A%20%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20%7D%20.%0A%7D

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).