Wikidata:WikiProject PCC Wikidata Pilot/UNLV/SPARQL Examples and Concepts

SPARQL: Supplemental SPARQL Examples and Concepts edit

SPARQL Protocol and RDF Query Language (SPARQL) is a query language used to recall and transform data stored in the Resource Description Framework (RDF) format. This page is organized by concept and serves as a supplemental instructional guide for graph-to-table queries using the Wikidata Query Service.

In the section labeled ‘Helpful Links’ you will find documentation including links to the full list of prefixes used in the Wikidata Query Service, available optional services, documentation for the RDF GAS API, and links to examples of display options for search results.

To learn more about ongoing Wikidata efforts at UNLV, please visit our project Wiki site: PCC Wikidata Pilot: UNLV

CONCEPT 1: DISCOVERY USING SEARCH CONDITION edit

Query #1: Select academic journal articles by subject: (linked data) edit

SELECT DISTINCT ?item ?DOI_URI ?title (GROUP_CONCAT(DISTINCT ?authorLabel; SEPARATOR = ", ") AS ?authors) ?publishedInLabel ?publicationDate 
WHERE
{
 ?item wdt:P31 wd:Q18918145 ; # instance of a academic journal article 
       wdt:P31 ?itemType ;
       rdfs:label ?title .
 ?item wdt:P921 wd:Q515701 ; # has the term 'linked data' as main subject
       wdt:P921 ?mainSubject . 
 ?item wdt:P50 ?author .
 ?author rdfs:label ?authorLabel .
 ?item wdt:P1433 ?publishedIn ;
       rdfs:label ?publication .
 ?item wdt:P577 ?publicationDate .
 OPTIONAL {?item wdt:P356 ?DOI} .
 BIND(URI(CONCAT("https://doi.org/", ?DOI)) AS ?DOI_URI)
 FILTER (LANG(?authorLabel) = "en") 
 FILTER (LANG(?title) = "en") 
 FILTER (LANG(?publication) = "en") 
 FILTER(?mainSubject IN (wd:Q515701)) # Filter out 'main subject' values NOT in this list
 FILTER(?itemType IN ( wd:Q18918145 )) # Filter out 'instance of' values NOT in this list
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?DOI_URI ?title ?authors ?publishedInLabel ?publicationDate
ORDER by DESC (?publicationDate)
Try it!

Query #2: Graph of identified researchers within an organization used to identify potential collaborators (clustered by occupation) edit

#defaultView:Graph
SELECT ?item ?itemLabel ?itemDescription ?orcidIdURL ?image ?occupation ?occupationLabel ("CF0A2C" as ?rgb)
WHERE 
{
  ?item wdt:P31 wd:Q5 ; # instance of a human
        rdfs:label ?itemLabel .
  ?item wdt:P108 wd:Q2302311 ; # University of Nevada, Las Vegas
        wdt:P108 ?employer .
  ?item wdt:P106 ?occupation .
  OPTIONAL { ?item wdt:P18 ?image . }
  OPTIONAL {?item wdt:P496 ?orcidId . }
  BIND (URI(CONCAT("https://orcid.org/", ENCODE_FOR_URI (?orcidId))) AS ?orcidIdURL)
  FILTER (?employer IN (wd:Q2302311)) # Filter out employers NOT the University of Nevada, Las Vegas
  FILTER (lang(?itemLabel) = "en") # Display English label
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?itemLabel
Try it!

Query #3: Select popular music genres and their sub-genres edit

SELECT DISTINCT ?item ?itemLabel ?itemDescription ?genreSubClassOf ?genreSubClassOfLabel ?genreSubClassOfDescription ?inceptionDate
WHERE 
{
 {
  ?item wdt:P31/wdt:P279* wd:Q188451 .
  ?item wdt:P31 ?instanceOf .
  FILTER(?instanceOf IN (wd:Q188451)) # Filter out 'instance of' values NOT in this list
  ?item wdt:P279 ?genreSubClassOf .
  OPTIONAL {?item wdt:P571 ?inceptionDate} .
  OPTIONAL {?item wdt:P495 ?countryOfOrgin} .
  FILTER(?countryOfOrgin IN (wd:Q30)) # Filter out 'country of orgin' values NOT in this list
 } 
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?itemLabel
Try it!

Query #4: Using GAS Service to graph sub-genres leading to, and descended from, the popular music (Q373342) musical form and music genre edit

#defaultView:Graph
SELECT ?subGenre ?subGenreLabel ("CF0A2C" as ?rgb) ?genre ?genreLabel ?depth 
WHERE
 {
  SERVICE gas:service {
       gas:program gas:gasClass "com.bigdata.rdf.graph.analytics.BFS" ; gas:in wd:Q373342 ; gas:linkType wdt:P279 ; gas:traversalDirection "Reverse"; gas:out ?subGenre ; gas:out1 ?depth ; gas:out2 ?genre . 
  }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
} order by ?depth
Try it!

CONCEPT 2: COMBINING DIFFERENT SEARCH CONDITIONS edit

Query #5: Graph people whos field of work is either: (linked data; linked open data; OR Semantic Web) edit

#defaultView:Graph
SELECT DISTINCT ?item ?itemLabel ?itemDescription ?orcidIdURL ?fieldOfWork ?fieldOfWorkLabel ("CF0A2C" AS ?rgb)
WHERE
{
  {?item wdt:P31 wd:Q5 ; # instance of a human
         rdfs:label ?itemLabel .
   ?item wdt:P101 wd:Q515701 ; # linked data
         wdt:P101 ?fieldOfWork .
   OPTIONAL {?item wdt:P496 ?orcidId} .
   BIND (URI(CONCAT("https://orcid.org/", ENCODE_FOR_URI (?orcidId))) AS ?orcidIdURL)
   FILTER (lang(?itemLabel) = "en") 
  }
 UNION
  {?item wdt:P31 wd:Q5 ; # instance of a human
         rdfs:label ?itemLabel .
   ?item wdt:P101 wd:Q18692990 ; # linked open data
         wdt:P101 ?fieldOfWork .
   OPTIONAL {?item wdt:P496 ?orcidId} .
   BIND (URI(CONCAT("https://orcid.org/", ENCODE_FOR_URI (?orcidId))) AS ?orcidIdURL)
   FILTER (lang(?itemLabel) = "en") 
  }
 UNION
  {?item wdt:P31 wd:Q5 ; # instance of a human
         rdfs:label ?itemLabel .
   ?item wdt:P101 wd:Q54837 ; # Semantic Web
         wdt:P101 ?fieldOfWork .
   OPTIONAL {?item wdt:P496 ?orcidId} .
   BIND (URI(CONCAT("https://orcid.org/", ENCODE_FOR_URI (?orcidId))) AS ?orcidIdURL)
   FILTER (lang(?itemLabel) = "en") 
  }
 FILTER(?fieldOfWork IN (wd:Q18692990, wd:Q515701, wd:Q54837)) # Filter out 'Field of Work' values NOT in this list
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?itemLabel
Try it!

Query #6: Graph librarian OR subclass of librarian whos field of work is either: (linked data; linked open data; OR Semantic Web) edit

#defaultView:Graph
SELECT DISTINCT ?item ?itemLabel ?itemDescription ?orcidIdURL ?fieldOfWork ?fieldOfWorkLabel ("CF0A2C" as ?rgb) 
WHERE
{
  {?item wdt:P31 wd:Q5 ; # instance of a human
         rdfs:label ?itemLabel .
   ?item wdt:P106/wdt:P279* wd:Q182436 ; # librarian OR Sub-class of librarian
         wdt:P106 ?occupation .
   ?item wdt:P101 wd:Q515701 ; # linked data
         wdt:P101 ?fieldOfWork .
   OPTIONAL {?item wdt:P496 ?orcidId} .
   BIND (URI(CONCAT("https://orcid.org/", ENCODE_FOR_URI (?orcidId))) AS ?orcidIdURL)
   FILTER (lang(?itemLabel) = "en") 
  }
 UNION
  {?item wdt:P31 wd:Q5 ; # instance of a human
         rdfs:label ?itemLabel .
   ?item wdt:P106/wdt:P279* wd:Q182436 ; # librarian OR Sub-class of librarian
         wdt:P106 ?occupation .
   ?item wdt:P101 wd:Q18692990 ; # linked open data
         wdt:P101 ?fieldOfWork .
   OPTIONAL {?item wdt:P496 ?orcidId} .
   BIND (URI(CONCAT("https://orcid.org/", ENCODE_FOR_URI (?orcidId))) AS ?orcidIdURL)
   FILTER (lang(?itemLabel) = "en") 
  }
 UNION
  {?item wdt:P31 wd:Q5 ; # instance of a human
         rdfs:label ?itemLabel .
   ?item wdt:P106/wdt:P279* wd:Q182436 ; # librarian OR Sub-class of librarian
         wdt:P106 ?occupation .
   ?item wdt:P101 wd:Q54837 ; # Semantic Web
         wdt:P101 ?fieldOfWork .
   OPTIONAL {?item wdt:P496 ?orcidId} .
   BIND (URI(CONCAT("https://orcid.org/", ENCODE_FOR_URI (?orcidId))) AS ?orcidIdURL)
   FILTER (lang(?itemLabel) = "en") 
  }
 FILTER(?fieldOfWork IN (wd:Q18692990, wd:Q515701, wd:Q54837 )) # Filter out 'Field of Work' values NOT in this list
 SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ?itemLabel
Try it!

Query #7: Select 'written works' and 'academic journal articles' by subject: (linked data) edit

SELECT DISTINCT ?item ?title ?mainSubjectLabel ?itemTypeLabel (GROUP_CONCAT(DISTINCT ?authorLabel; SEPARATOR = ", ") AS ?authors) 
WHERE
{
 {
  ?item wdt:P31 wd:Q47461344 ; # instance of a written work 
        wdt:P31 ?itemType ;
        rdfs:label ?title .
  ?item wdt:P921 wd:Q515701 ; # has the term 'linked data' as main subject
        wdt:P921 ?mainSubject . 
  ?item wdt:P50 ?author .
  ?author rdfs:label ?authorLabel .
  FILTER (LANG(?authorLabel) = "en") 
  FILTER (LANG(?title) = "en") 
 }
 UNION
 {
  ?item wdt:P31 wd:Q18918145 ; # instance of a academic journal article
        wdt:P31 ?itemType ;
        rdfs:label ?title .
  ?item wdt:P921 wd:Q515701 ; # has the term 'linked data' as main subject
        wdt:P921 ?mainSubject .
  ?item wdt:P50 ?author .
  ?author rdfs:label ?authorLabel .
  FILTER (LANG(?authorLabel) = "en") 
  FILTER (LANG(?title) = "en") 
 }
FILTER(?mainSubject IN (wd:Q515701)) # Filter out 'main subject' values NOT in this list
FILTER(?itemType IN (wd:Q47461344, wd:Q18918145 )) # Filter out 'instance of' values NOT in this list
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
GROUP BY ?item ?title ?mainSubjectLabel ?itemTypeLabel ?authors
ORDER by ASC (?title)
Try it!

CONCEPT 3: WIKIDATA EXPLORATION USING 'ARCHIVES AT' or 'ORAL HISTORY AT' PROPERTIES edit

Query #8: Select items 'Archived At' the University of Nevada, Las Vegas Libraries edit

SELECT ?item ?itemLabel ?itemDescription ?collection ?inventoryNum ?describedAt
WHERE
{
  ?item p:P485 ?archivesAt .
  ?archivesAt ps:P485 wd:Q73644758 .
  OPTIONAL {?archivesAt pq:P1810 ?collection } .
  OPTIONAL {?archivesAt pq:P217 ?inventoryNum } .
  OPTIONAL {?archivesAt pq:P973 ?describedAt } .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

Query #9: Select items which has 'Oral Histories' held by the University of Nevada, Las Vegas Libraries edit

SELECT DISTINCT ?item ?itemLabel ?itemDescription ?oralHistory ?inventoryNum ?describedAt 
WHERE
{ 
  ?item p:P9600 ?oralHistoryAt .
  ?oralHistoryAt ps:P9600 wd:Q73644758 .
  OPTIONAL {?oralHistoryAt pq:P1810 ?oralHistory } .
  OPTIONAL {?oralHistoryAt pq:P217 ?inventoryNum } .
  OPTIONAL {?oralHistoryAt pq:P973 ?describedAt } .
  FILTER (REGEX(STR(?describedAt),"^http://n2t.net/")) 
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Try it!

CONCEPT 4: RESULT VIEWS FOR DATA EXPLORATION (Maps) edit

Query #10: Visualize lakes within Nevada counties edit

#defaultView:Map
SELECT DISTINCT ?item ?itemPrefLabel ?coor ?layerLabel
WHERE 
{
 {
  VALUES ?o { wd:Q108403 wd:Q111220 wd:Q835104 wd:Q495398 wd:Q484340 wd:Q484381 wd:Q203022 wd:Q7103481 wd:Q484418 wd:Q484342 wd:Q484335 wd:Q495376 wd:Q495349 wd:Q484431 wd:Q484349 wd:Q484401 wd:Q484398 } # Counties of Nevada, United States
  ?item wdt:P31 wd:Q23397 ; # instance of lake
        rdfs:label ?itemPrefLabel ;
        ?range ?o ;
        wdt:P625 ?coor ;
        wdt:P131 ?location .
  BIND(?o AS ?layer) 
  FILTER (LANG(?itemPrefLabel) = "en") 
  FILTER(?location IN (wd:Q108403, wd:Q111220, wd:Q835104, wd:Q495398, wd:Q484340, wd:Q484381, wd:Q203022, wd:Q7103481, wd:Q484418, wd:Q484342, wd:Q484335, wd:Q495376, wd:Q495349, wd:Q484431, wd:Q484349, wd:Q484401, wd:Q484398 )) # Filter out 'administrative territorial entities' values NOT in this list
 }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER By (?layerLabel)
Try it!

Query #11: Visualize places within a 50-miles radius around Clark County, Nevada (only include restaurants) edit

#defaultView:Map
SELECT ?item ?itemLabel ?location ?layerLabel
WHERE
{ 
  wd:Q175665 wdt:P625 ?loc .
  SERVICE wikibase:around 
          { 
           ?item wdt:P625 ?location .
           bd:serviceParam wikibase:center ?loc .
           bd:serviceParam wikibase:radius "80.4672" . # 50 Miles converted to kilometers here
          }
  OPTIONAL {?item wdt:P31 ?instance}
  BIND(?instance AS ?layer) .
  BIND(geof:distance(?loc, ?location) as ?dist)
  FILTER(?instance IN (wd:Q11707)) # Filter out 'instance of' values NOT in this list
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} ORDER BY ASC(?layerLabel)
Try it!

CONCEPT 5: RESULT VIEWS FOR DATA EXPLORATION (Timelines) edit

Query #12: Visualize inception dates and end dates for newspapers on the UNLV Wikimedia project focus list edit

#defaultView:Timeline
SELECT ?item ?title ?placeOfPubLabel ?inceptionDate ?endDate 
WHERE 
{ 
  {
   ?item wdt:P31 wd:Q11032 ;
         rdfs:label ?title ;
         wdt:P5008 wd:Q100202113 .
   OPTIONAL {?item wdt:P1476 ?title } .
   OPTIONAL {?item wdt:P291 ?placeOfPub } .
   OPTIONAL {?item wdt:P571 ?inceptionDate } .
   OPTIONAL {?item wdt:P576 ?endDate } .
   FILTER (LANG(?title) ="en") 
  }
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Try it!

Helpful Links: Wikidata Query Service Documentation edit

Full List of Wikidata Prefixes edit

Query Optimization Services edit

RDF GAS API Documentation edit

Wikidata Visualization Options edit

Contributors edit