Wikidata:SPARQL query service/Building a query/Museums on Instagram

From Wikidata
Jump to navigation Jump to search

A short, annotated guide to building a simple SPARQL query. Comments are preceded by a "#".

Step 1[edit]

We start with a short & simple query, museums with Instagram username (P2003). Firstly, we SELECT (or display) some items, where instance of (P31) or subclass of (P279) is a type of museum (Q33506), AND those items have an Instagram username (P2003) We give the latter a label (not to be confused with the item's Wikidata label), ?instagram, so that we can refer to it later:

SELECT ?item WHERE {
  ?item wdt:P31/wdt:P279* wd:Q33506 . # The full stop (period) is required!
  ?item wdt:P2003 ?instagram . #Note that the label begins with a "?"
}
Try it!

Step 2[edit]

That simply returns a list of "Q" IDs, so we modify it 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/wdt:P279* wd:Q33506 .
  ?item wdt:P2003 ?instagram .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } . # "en" shows the labels in English
}
Try it!

Step 3[edit]

But we also want to see the name of the Instagram account, so we add ?instagram (that's the label we chose in step one) 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 4[edit]

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 5[edit]

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 6[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:

#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 7[edit]

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