Dan Scott <https://dscott.ca/#i>
PhD student, McGill University
Associate Librarian, Laurentian University
| Table | Columns |
|---|---|
| University | id (PK), name, founding date, location, ... |
| UniversityPerson | id (PK), university (FK), person (FK), role |
| Person | id (PK), name, birth date, death date, ... |
| Subject | Predicate | Object |
|---|---|---|
| Dan Scott | member of | Laurentian University |
| Laurentian University | location | Sudbury |
| Laurentian University | founding date | 1960 |
Use HTTP URIs (web addresses) to identify things:
| Subject | Predicate | Object |
|---|---|---|
| https://dscott.ca/#i | http://schema.org/memberOf | https://laurentian.ca/ |
| https://laurentian.ca/ | http://schema.org/location | https://greatersudbury.ca/ |
| https://laurentian.ca/ | http://schema.org/foundingDate | "1960"^^xsd:gYear |
These three-part statements are called triples.
owl:equivalentClass, owl:equivalentProperty) and things (owl:sameAs)
SELECT variables rather than columnsFROM clause: just the entire dataset of triples!WHERE clause creates a pattern for the triples you wantJOINs: relationships between entitiesFILTER attribute values to narrow furtherSELECT * WHERE {
?s ?p ?o
}
LIMIT 10CTRL + ENTER, or click the arrow button on the left, to submit the query.?o to "Laurentian University":
SELECT * WHERE {
?s ?p "Laurentian University"
}
LIMIT 10wdt:P31 (instance of).wd:Q3918 (university).
SELECT * WHERE {
?s wdt:P31 wd:Q3918
}wdt: = "truthiness"wd: = entity.) to your first statement.wdt:P17) for the subject and store the value in a new variable, ?country.
SELECT * WHERE {
?s wdt:P31 wd:Q3918 .
?s wdt:P17 ?country
}rdfs:label of the subject.rdfs:label of the country.LIMIT 100 clause to keep things fast.SELECT * WHERE {
?s wdt:P31 wd:Q3918 .
?s wdt:P17 ?country .
?s rdfs:label ?sLabel .
?country rdfs:label ?countryLabel .
}
LIMIT 100
Don't forget the "AND" operator (period .) for your statements!
FILTER(LANG(?countryLabel) = "en"))
SELECT * WHERE {
?s wdt:P31 wd:Q3918 .
?s wdt:P17 ?country .
?s rdfs:label ?sLabel .
?country rdfs:label ?countryLabel .
FILTER(LANG(?sLabel) = "en") .
FILTER(LANG(?countryLabel) = "en") .
}
LIMIT 100ORDER BY ?countryLabel clause just before the LIMIT clause:
SELECT * WHERE {
?s wdt:P31 wd:Q3918 .
?s wdt:P17 ?country .
?s rdfs:label ?sLabel .
?country rdfs:label ?countryLabel .
FILTER(LANG(?sLabel) = "en") .
FILTER(LANG(?countryLabel) = "en") .
}
ORDER BY ?countryLabel
LIMIT 100P625 and store it in a ?coords variable:
SELECT * WHERE {
?s wdt:P31 wd:Q3918 .
?s wdt:P17 ?country .
?s rdfs:label ?sLabel .
?country rdfs:label ?countryLabel .
FILTER(LANG(?sLabel) = "en") .
FILTER(LANG(?countryLabel) = "en") .
?s wdt:P625 ?coords .
}
ORDER BY ?countryLabel
LIMIT 100
Let's focus on Canadian universities
# show instances & subclasses of Canadian universities on a map
#defaultView:Map
SELECT * WHERE {
?s wdt:P31/wdt:P279* wd:Q3918 . # instances of and subclasses of university
?s wdt:P31 ?instanceOf . # instance of what?
?s wdt:P17 wd:Q16 . # in Canada
?s rdfs:label ?sLabel .
?instanceOf rdfs:label ?instanceLabel .
FILTER(LANG(?sLabel) = "en") . # give us the English label
FILTER(LANG(?instanceLabel) = "en") . # give us the English label
OPTIONAL{?s wdt:P625 ?coords} . # and coordinates, if possible
}
ORDER BY ?sLabel
SELECT clause to select ?countryLabel (COUNT (?s) AS ?cnt).GROUP BY DESC(?cnt) clause just before the ORDER BY clause:
SELECT ?countryLabel (COUNT (?s) AS ?cnt) WHERE {
?s wdt:P31 wd:Q3918 .
?s wdt:P17 ?country .
?country rdfs:label ?countryLabel .
FILTER(LANG(?countryLabel) = "en") .
}
GROUP BY ?countryLabel
ORDER BY DESC(?cnt)
LIMIT 100HAVING(COUNT(?s) <= 50) clause just before the ORDER BY clause:
SELECT ?countryLabel (COUNT (?s) AS ?cnt) WHERE {
?s wdt:P31 wd:Q3918 .
?s wdt:P17 ?country .
?country rdfs:label ?countryLabel .
FILTER(LANG(?countryLabel) = "en") .
}
GROUP BY ?countryLabel
HAVING(COUNT(?s) <= 50)
ORDER BY DESC(?cnt)
LIMIT 100