SPARQL

From CODECS Wikibase
Jump to navigation Jump to search
# Example

## Prefixes need to be set explicitly.
PREFIX : <https://codecs.wikibase.cloud/entity/>
PREFIX t: <https://codecs.wikibase.cloud/prop/direct/>

SELECT DISTINCT ?item ?itemLabel ?in_geographic_region ?in_geographic_regionLabel WHERE {
  ## Service required to get the labels
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
  ?item t:P1 :Q7.
  OPTIONAL { ?item t:P15 ?in_geographic_region. }
}
LIMIT 100
Using the sparql tag, with list=1 and tryit=1

The following query uses these:

## Prefixes need to be set explicitly.
PREFIX : <https://codecs.wikibase.cloud/entity/>
PREFIX t: <https://codecs.wikibase.cloud/prop/direct/>

SELECT DISTINCT ?item ?itemLabel ?in_geographic_region ?in_geographic_regionLabel WHERE {
  ## Service required to get the labels
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
  ?item t:P1 :Q7.
  OPTIONAL { ?item t:P15 ?in_geographic_region. }
}
LIMIT 100

Try it!


Example: baronies

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT ?barony ?baronyLabel ?label_ga ?country ?countryLabel ?historic_county ?historic_countyLabel ?located_in_the_administrative_territorial_entity ?located_in_the_administrative_territorial_entityLabel ?coordinate_location ?locator_map_image ?Logainm_ID ?OpenStreetMap_relation_ID ?PlacenamesNI_ID ?Library_of_Congress_authority_ID ?VIAF_ID WHERE {
  #SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en,ga". }
  ?barony wdt:P31 wd:Q853565

  OPTIONAL { ?barony rdfs:label ?label_en filter( lang(?label_en) = "en" ). }
  OPTIONAL { ?barony rdfs:label ?label_ga filter( lang(?label_ga) = "ga" ). }
  OPTIONAL { ?barony wdt:P17 ?country. }
  OPTIONAL { ?barony wdt:P7959 ?historic_county. }
  OPTIONAL { ?barony wdt:P131 ?located_in_the_administrative_territorial_entity. }
  OPTIONAL { ?barony wdt:P625 ?coordinate_location. }
  OPTIONAL { ?barony wdt:P242 ?locator_map_image. }
  OPTIONAL { ?barony wdt:P5097 ?Logainm_ID. }
  OPTIONAL { ?barony wdt:P402 ?OpenStreetMap_relation_ID. }
  OPTIONAL { ?barony wdt:P9370 ?PlacenamesNI_ID. }
  OPTIONAL { ?barony wdt:P244 ?Library_of_Congress_authority_ID. }
  OPTIONAL { ?barony wdt:P214 ?VIAF_ID. }
  filter( lang(?label_ga) = 'ga' )
}
LIMIT 100

Try it!


Printing non-property values

We can use ?itemLabel ?itemDescription but not ?itemAlias. Use skos:altLabel instead, e.g.

PREFIX : <https://codecs.wikibase.cloud/entity/>
PREFIX t: <https://codecs.wikibase.cloud/prop/direct/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

SELECT DISTINCT ?item ?itemLabel ?alias WHERE {
  ## Service required to get the labels
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
  ?item t:P1 :Q3051.
  OPTIONAL { ?item skos:altLabel ?alias  }
}
LIMIT 1000

Try it!