Skip to content

Commit 819728a

Browse files
authored
Merge pull request #98 from LibraryCarpentry/RabeaMue-patch-16
Expand empty sections and add advanced queries in episode 5 (closes #93)
2 parents 5c689af + e2163df commit 819728a

1 file changed

Lines changed: 69 additions & 9 deletions

File tree

episodes/05-intro_to_querying.md

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ SELECT * WHERE {
122122

123123
### Showing labels to Q-numbers
124124

125+
By default, SPARQL queries return Q-numbers instead of human-readable
126+
labels. To show labels, add the `SERVICE wikibase:label` block to your
127+
query:
128+
129+
```
130+
SELECT ?item ?itemLabel
131+
WHERE {
132+
?item wdt:P31 wd:Q7075.
133+
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
134+
}
135+
```
136+
The `[AUTO_LANGUAGE]` placeholder automatically uses your browser's
137+
language setting, with English as a fallback.
138+
125139
### Namespaces and Prefixes
126140

127141
Prefixes are short abbrevations in the Wikidata Query Service. Some prefixes in Wikidata are: wd, wdt, p, ps, bd, etc.
@@ -148,11 +162,26 @@ Namespaces in Wikidata are:
148162

149163
### More conditions
150164

151-
- publications from Addison-Wesley vs. books from Addison-Wesley vs. books authored by Richard Feynman from Addison-Wesley
152-
- LIMIT
153-
- ORDER
154-
- FILTER
155-
- OPTIONAL
165+
### More conditions
166+
167+
You can combine multiple conditions in a single query. Here is an example
168+
that finds books published by Addison-Wesley authored by Richard Feynman:
169+
170+
```
171+
SELECT ?book ?bookLabel WHERE {
172+
?book wdt:P31 wd:Q571. # instance of book
173+
?book wdt:P123 wd:Q353060. # published by Addison-Wesley
174+
?book wdt:P50 wd:Q39246. # authored by Richard Feynman
175+
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
176+
}
177+
```
178+
Useful modifiers:
179+
180+
- `LIMIT 10` — restrict results to 10 rows
181+
- `ORDER BY DESC(?date)` — sort results by date, newest first
182+
- `FILTER(YEAR(?date) > 2000)` — filter by year
183+
- `OPTIONAL { ?item wdt:P18 ?image }` — include a field only if it exists
184+
156185

157186
### How to visualize your query
158187

@@ -552,13 +581,44 @@ WHERE
552581

553582
## 5\.4 More Advanced queries
554583

555-
further links
584+
Here are some more advanced query patterns that are particularly useful
585+
for library use cases.
586+
587+
### Federated queries and subqueries
556588

589+
You can use subqueries to pre-filter data before the main query processes
590+
it, which can significantly speed up complex queries:
591+
592+
```
593+
SELECT ?item ?itemLabel ?authorLabel WHERE {
594+
?item wdt:P31 wd:Q13442814. # scholarly article
595+
?item wdt:P50 ?author.
596+
?author wdt:P108 wd:Q49210. # affiliated with a specific institution
597+
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
598+
}
599+
LIMIT 20
557600
```
558-
https://commons.wikimedia.org/wiki/File:Wikidata_Query_Service_in_Brief.pdf
559-
https://www.uni-mannheim.de/media/Einrichtungen/dws/Files_Teaching/Semantic_Web_Technologies/SWT05-SPARQL-v1.pdf
560-
https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial
601+
### Maintenance queries
602+
603+
Maintenance queries help identify items that are missing information.
604+
These are useful for librarians who want to improve data quality:
605+
561606
```
607+
# Libraries without coordinates
608+
SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q7075. # instance of library FILTER NOT EXISTS { ?item wdt:P625 ?coord } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } LIMIT 20
609+
```
610+
611+
```
612+
# Scholarly articles without a DOI
613+
SELECT ?item ?itemLabel WHERE { ?item wdt:P31 wd:Q13442814. # scholarly article FILTER NOT EXISTS { ?item wdt:P356 ?doi } SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } } LIMIT 20
614+
```
615+
616+
### Further resources
617+
618+
- [Wikidata Query Service in Brief (PDF)](https://commons.wikimedia.org/wiki/File:Wikidata_Query_Service_in_Brief.pdf)
619+
- [SPARQL tutorial on Wikidata](https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial)
620+
- [SPARQL lecture slides, University of Mannheim](https://www.uni-mannheim.de/media/Einrichtungen/dws/Files_Teaching/Semantic_Web_Technologies/SWT05-SPARQL-v1.pdf)
621+
562622

563623
:::::::::::::::::::::::::::::::::::::::: keypoints
564624

0 commit comments

Comments
 (0)