query-eb19b1ac538574ec875ba37b4656c6bd

rq turtle/ttl

ExampleIn our example case, we want a data set that comprises all museums in Switzerland (pretty much all of them already have an item on Wikidata). query selects all Wikidata items that describe an instance of "museum" or of any of the sub-classes of "museum". It further restricts the selection to items that are located in an administrative territorial entity in Switzerland. – The relevant instructions can be found in our example code in the two lines following the "WHERE" tag, which contains the information about which items are to be selected from the entire Wikidata data base. SPARQLThe following

Use at

PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?item
       ?Label_en
       ?Description_en
       (group_concat(distinct ?Alias_en;separator="; ") as ?Aliases_en)    #Concatenate the values in order not to get several rows per item.
       ?Label_de
       ?Description_de
       (GROUP_CONCAT(distinct ?Alias_de;separator="; ") as ?Aliases_de)
       ?Label_fr
       (GROUP_CONCAT(distinct ?Alias_fr;separator="; ") as ?Aliases_fr)
       ?Label_it
       (GROUP_CONCAT(distinct ?Alias_it;separator="; ") as ?Aliases_it)
       (GROUP_CONCAT(distinct ?MunicipalityLabel_de;separator="; ") as ?MunicipalityLabels_de)
       (GROUP_CONCAT(distinct ?CantonLabel_de;separator="; ") as ?CantonLabels_de)
  WHERE {
  {?item wdt:P31 ?museum . ?museum wdt:P279* wd:Q33506 } . # Select items that are instance of "museum" or of any of its sub-classes.
  {?item wdt:P131/wdt:P17 wd:Q39} .  # Select items that are located in an administrative territorial entity in Switzerland.
  OPTIONAL { ?item rdfs:label ?Label_en . FILTER (lang(?Label_en) = "en") }
  OPTIONAL { ?item rdfs:label ?Label_de . FILTER (lang(?Label_de) = "de") } 
  OPTIONAL { ?item rdfs:label ?Label_fr . FILTER (lang(?Label_fr) = "fr") }
  OPTIONAL { ?item rdfs:label ?Label_it . FILTER (lang(?Label_it) = "it") }
  OPTIONAL { ?item skos:altLabel ?Alias_en . FILTER (lang(?Alias_en) = "en") }
  OPTIONAL { ?item skos:altLabel ?Alias_de . FILTER (lang(?Alias_de) = "de") } 
  OPTIONAL { ?item skos:altLabel ?Alias_fr . FILTER (lang(?Alias_fr) = "fr") }
  OPTIONAL { ?item skos:altLabel ?Alias_it . FILTER (lang(?Alias_it) = "it") }
  OPTIONAL { ?item schema:description ?Description_en . FILTER (lang(?Description_en) = "en") }
  OPTIONAL { ?item schema:description ?Description_de . FILTER (lang(?Description_de) = "de") } 
  OPTIONAL { ?item schema:description ?Description_fr . FILTER (lang(?Description_fr) = "fr") }
  OPTIONAL { ?item schema:description ?Description_it . FILTER (lang(?Description_it) = "it") }
  OPTIONAL { ?item wdt:P131 ?Municipality . FILTER EXISTS {?Municipality wdt:P31 wd:Q70208} }  
  OPTIONAL { ?item wdt:P131/rdfs:label ?MunicipalityLabel_de . FILTER EXISTS {?MunicipalityLabel_de ^rdfs:label/wdt:P31 wd:Q70208} . FILTER (lang(?MunicipalityLabel_de) = "de")} 
  OPTIONAL { ?item wdt:P131/rdfs:label ?CantonLabel_de . FILTER EXISTS {?CantonLabel_de ^rdfs:label/wdt:P31 wd:Q23058} . FILTER (lang(?CantonLabel_de) = "de") } 
}
GROUP BY ?item                          #List all the variables for which the values are not concatenated!
         ?Label_en ?Description_en 
         ?Label_de ?Description_de 
         ?Label_fr ?Description_fr 
         ?Label_it ?Description_it

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v10("?Alias_de"):::projected v11("?Alias_en"):::projected v9("?Alias_fr"):::projected v8("?Alias_it"):::projected v19("?Aliases_de") v18("?Aliases_en") v20("?Aliases_fr") v21("?Aliases_it") v1("?CantonLabel_de"):::projected v23("?CantonLabels_de") v6("?Description_de"):::projected v7("?Description_en"):::projected v5("?Description_fr") v4("?Description_it") v14("?Label_de"):::projected v15("?Label_en"):::projected v13("?Label_fr"):::projected v12("?Label_it"):::projected v3("?Municipality") v2("?MunicipalityLabel_de"):::projected v22("?MunicipalityLabels_de") v16("?item"):::projected v17("?museum") a3((" ")) a4((" ")) a2((" ")) a5((" ")) a1((" ")) c3(["wd:Q23058"]):::iri c10(["wd:Q33506"]):::iri c13(["wd:Q39"]):::iri c5(["wd:Q70208"]):::iri v16 --"wdt:P31"--> v17 v17 --"wdt:P279"--> c10 v16 --"wdt:P131"--> a3 a3 --"wdt:P17"--> c13 subgraph optional0["(optional)"] style optional0 fill:#bbf,stroke-dasharray: 5 5; v16 -."rdfs:label".-> v15 end subgraph optional1["(optional)"] style optional1 fill:#bbf,stroke-dasharray: 5 5; v16 -."rdfs:label".-> v14 end subgraph optional2["(optional)"] style optional2 fill:#bbf,stroke-dasharray: 5 5; v16 -."rdfs:label".-> v13 end subgraph optional3["(optional)"] style optional3 fill:#bbf,stroke-dasharray: 5 5; v16 -."rdfs:label".-> v12 end subgraph optional4["(optional)"] style optional4 fill:#bbf,stroke-dasharray: 5 5; v16 -."skos:altLabel".-> v11 end subgraph optional5["(optional)"] style optional5 fill:#bbf,stroke-dasharray: 5 5; v16 -."skos:altLabel".-> v10 end subgraph optional6["(optional)"] style optional6 fill:#bbf,stroke-dasharray: 5 5; v16 -."skos:altLabel".-> v9 end subgraph optional7["(optional)"] style optional7 fill:#bbf,stroke-dasharray: 5 5; v16 -."skos:altLabel".-> v8 end subgraph optional8["(optional)"] style optional8 fill:#bbf,stroke-dasharray: 5 5; v16 -."schema:description".-> v7 end subgraph optional9["(optional)"] style optional9 fill:#bbf,stroke-dasharray: 5 5; v16 -."schema:description".-> v6 end subgraph optional10["(optional)"] style optional10 fill:#bbf,stroke-dasharray: 5 5; v16 -."schema:description".-> v5 end subgraph optional11["(optional)"] style optional11 fill:#bbf,stroke-dasharray: 5 5; v16 -."schema:description".-> v4 end subgraph optional12["(optional)"] style optional12 fill:#bbf,stroke-dasharray: 5 5; v16 -."wdt:P131".-> v3 end subgraph optional13["(optional)"] style optional13 fill:#bbf,stroke-dasharray: 5 5; v16 -."wdt:P131".-> a4 a4 --"rdfs:label"--> v2 end subgraph optional14["(optional)"] style optional14 fill:#bbf,stroke-dasharray: 5 5; v16 -."wdt:P131".-> a5 a5 --"rdfs:label"--> v1 end bind6[/"?Alias_en"/] v11 --o bind6 bind6 --as--o v18 bind7[/"?Alias_de"/] v10 --o bind7 bind7 --as--o v19 bind8[/"?Alias_fr"/] v9 --o bind8 bind8 --as--o v20 bind9[/"?Alias_it"/] v8 --o bind9 bind9 --as--o v21 bind10[/"?MunicipalityLabel_de"/] v2 --o bind10 bind10 --as--o v22 bind11[/"?CantonLabel_de"/] v1 --o bind11 bind11 --as--o v23