Wikidata:WikiProject PCC Wikidata Pilot/UC Davis/Example Queries
These queries are meant to show some queries specific for the UC Library and Special Collection Contributors.
For more basic queries help to understand SPARQL and the Wikibase RDF format, see the Wikidata Example Page
Reference Queries
editList of Carnegie Melon Classifications
editThe |University recommendations suggest the Carnegie Classification of Institutions of Higher Education (P2643) predicate for organizing university organizations. Valid entries are sub-classes of Carnegie Classification of Institutions of Higher Education (Q4223026)
The following query uses these:
- Properties: subclass of (P279)
SELECT ?s ?sLabel ?p ?pLabel ?gp ?gpLabel WHERE { # https://www.wikidata.org/wiki/Q23337118 ?s wdt:P279+ wd:Q4223026. ?s wdt:P279 ?p. OPTIONAL { ?p wdt:P279 ?gp . } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } order by ?sLabel
Academic Queries
editWhat we are trying to do is find some information about professors in WikiData. We can do a simple search in Wikidata to find the class Professor. This is a subclass of a University Teacher.
Common Predicates for Professors
editThe following query uses these:
- Items: professor (Q121594)
SELECT ?p (count(?s) as ?c) WHERE { ?s ?p wd:Q121594 . } GROUP BY (?p) ORDER BY DESC(?c) LIMIT 10
From the example, most entries use Occupation, but some use professorship, and some use academic degree, but that's not a profession, so we can ge most with the first two. We expect from above to get about 14K entries in this case.
Common Predicates for professors
editThe following query uses these:
- Items: professor (Q121594)
- Properties: occupation (P106) , professorship (P803)
SELECT ?p (count(?s) as ?c) WHERE { BIND(wd:Q121594 as ?prof). VALUES (?p) { (ps:P106) (ps:P803) } ?s ?p ?prof . } group by (?p)
Okay, let's get some examples. Let's choose some professors that were educated at UC Davis or UCLA
UC Davis Professors
editThe following query uses these:
- Items: professor (Q121594) , University of California, Los Angeles (Q174710) , University of California, Davis (Q129421)
- Properties: educated at (P69) , employer (P108) , occupation (P106) , professorship (P803)
SELECT ?s ?sLabel ?is ?from ?ucLabel WHERE { BIND(wd:Q121594 as ?prof). VALUES(?from) { (wdt:P69) (wdt:P108) } VALUES(?is) { (wdt:P106) (wdt:P803) } VALUES(?uc ?uc_name) { # (wd:Q174710 "ucla") (wd:Q129421 "ucd") } ?s ?is ?prof . ?s ?from ?uc . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
Queries on UC Davis
editEmployees
editThere are many persons that are Employed by UCD.
The following query uses these:
- Properties: occupation (P106) , professorship (P803) , employer (P108)
SELECT ?s ?sLabel ?is ?what ?whatLabel WHERE { BIND(wd:Q121594 as ?prof). VALUES(?is) { (wdt:P106) (wdt:P803) } ?s ?is ?what . ?s wdt:P108 wd:Q129421 . SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
Organizations
editOne goal of this project is that all organizations within UC Davis can be captured with a single query, where we look at organizations that are part of Davis.
The following query uses these:
- Properties: part of (P361)
SELECT ?org ?orgLabel WHERE { bind(wd:Q129421 as ?ucd) ?org wdt:P361 ?ucd. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } }
Predicates that Point to UC Davis
editThe following query uses these:
- Items: University of California, Los Angeles (Q174710) , University of California, Davis (Q129421)
SELECT ?p (count(*) as ?cnt) WHERE { VALUES(?uc ?uc_name) { # (wd:Q174710 "ucla") (wd:Q129421 "ucd") } ?s ?p ?uc . filter(regex(str(?p),str(wdt:))) } group by ?p order by desc(?cnt)
Items that have UC Davis in the label
editThe query below uses the CirrusSearch extension. This extension uses another indexer (elasticsearch) to allow much better text searches. These are the SERVICE extensions below:
The following query uses these:
SELECT DISTINCT ?item ?label
WHERE {
{
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "Generator";
mwapi:generator "search";
mwapi:gsrsearch "inlabel:university_of_California_davis"@en;
mwapi:gsrlimit "max".
?item wikibase:apiOutputItem mwapi:title.
}
?item rdfs:label ?label. FILTER( LANG(?label)="en" )
} UNION {
SERVICE wikibase:mwapi
{
bd:serviceParam wikibase:endpoint "www.wikidata.org";
wikibase:api "Generator";
mwapi:generator "search";
mwapi:gsrsearch "inlabel:UC Davis"@en;
mwapi:gsrlimit "max".
?item wikibase:apiOutputItem mwapi:title.
}
?item rdfs:label ?label. FILTER( LANG(?label)="en" )
}
}
The types of items that have these labels
editThe following query uses these:
- Properties: instance of (P31)
SELECT ?class ?classLabel ?cnt WHERE { { SELECT ?class (count(*) as ?cnt) WHERE { SELECT DISTINCT ?item ?class WHERE { { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "www.wikidata.org"; wikibase:api "Generator"; mwapi:generator "search"; mwapi:gsrsearch "inlabel:university of California, davis"@en; mwapi:gsrlimit "max". ?item wikibase:apiOutputItem mwapi:title. } ?item wdt:P31 ?class. } UNION { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "www.wikidata.org"; wikibase:api "Generator"; mwapi:generator "search"; mwapi:gsrsearch "inlabel:UC Davis"@en; mwapi:gsrlimit "max". ?item wikibase:apiOutputItem mwapi:title. } ?item wdt:P31 ?class. } } #order by ?item ?class limit 300 } group by ?class order by ?class } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } order by?classLabel
Reference Queries
editSpecific Types of Objects
editUsing the above query, we can limit the objects that we need to review by looking at the items that match the UC Davis label, and are instances of types that look like some sort of organization. From this, we can
The following query uses these:
- Items: academic department (Q2467461) , academic institution (Q4671277) , academic library (Q856234) , archive (Q166118) , botanical garden (Q167346) , business school (Q1143635) , census-designated place in the United States (Q498162) , facility (Q13226383) , faculty (Q180958) , fire department (Q6498663) , government organization (Q2659904) , herbarium (Q181916) , hospital (Q16917) , institute (Q1664720) , land-grant university (Q615150) , law school (Q1321960) , library network (Q28324850) , medical organization (Q4287745) , medical school (Q494230) , organization (Q43229) , public educational institution of the United States (Q23002039) , research institute (Q31855) , university (Q3918) , veterinary school (Q1384955)
- Properties: instance of (P31)
SELECT ?item ?itemLabel (group_concat(?orglikeLabel;SEPARATOR=";") as ?orgs) { SELECT ?item ?itemLabel ?orglikeLabel WHERE { VALUES (?orglike) { (wd:Q2467461) (wd:Q4671277) (wd:Q856234) (wd:Q166118) (wd:Q167346) (wd:Q1143635) (wd:Q498162) (wd:Q13226383) (wd:Q180958) (wd:Q6498663) (wd:Q2659904) (wd:Q181916) (wd:Q16917) (wd:Q1664720) (wd:Q615150) (wd:Q1321960) (wd:Q28324850) (wd:Q4287745) (wd:Q494230) (wd:Q43229) (wd:Q23002039) (wd:Q31855) (wd:Q3918) (wd:Q1384955) } { SELECT DISTINCT ?item ?orglike WHERE { { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "www.wikidata.org"; wikibase:api "Generator"; mwapi:generator "search"; mwapi:gsrsearch "inlabel:university of California, davis"@en; mwapi:gsrlimit "max". ?item wikibase:apiOutputItem mwapi:title. } ?item wdt:P31 ?orglike. } UNION { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "www.wikidata.org"; wikibase:api "Generator"; mwapi:generator "search"; mwapi:gsrsearch "inlabel:UC Davis"@en; mwapi:gsrlimit "max". ?item wikibase:apiOutputItem mwapi:title. } ?item wdt:P31 ?orglike. } } } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } } group by ?item ?itemLabel order by ?itemLabel
Potential Missing Oraganizations
editWe may also be interested in Organizations that might by part of UC Davis, but are not properly Standardized. For example, this query shows potential Organizations that don't show to be partOf UC Davis
The following query uses these:
- Properties: part of (P361) , instance of (P31) , subclass of (P279)
SELECT ?item ?itemLabel WHERE {# (group_concat(?instanceLabel;SEPARATOR=";") as ?org_types) WHERE { filter(NOT EXISTS {?item wdt:P361 wd:Q129421.} ) filter(EXISTS{?item wdt:P31/wdt:P279* wd:Q43229}) { SELECT ?item WHERE { { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "www.wikidata.org"; wikibase:api "Generator"; mwapi:generator "search"; mwapi:gsrsearch "inlabel:university of California, davis"@en; mwapi:gsrlimit "max". ?item wikibase:apiOutputItem mwapi:title. } } UNION { SERVICE wikibase:mwapi { bd:serviceParam wikibase:endpoint "www.wikidata.org"; wikibase:api "Generator"; mwapi:generator "search"; mwapi:gsrsearch "inlabel:UC Davis"@en; mwapi:gsrlimit "max". ?item wikibase:apiOutputItem mwapi:title. } } } } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } limit 20 #group by ?item ?itemLabel