From e2163dfa00e2ccc13343b5f6453fd05724e76b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rabea=20M=C3=BCller?= <42644523+RabeaMue@users.noreply.github.com> Date: Fri, 17 Apr 2026 18:34:01 +0200 Subject: [PATCH] Expand empty sections and add advanced queries in episode 5 (closes #93) --- episodes/05-intro_to_querying.md | 78 ++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 9 deletions(-) diff --git a/episodes/05-intro_to_querying.md b/episodes/05-intro_to_querying.md index 4c25563a..b5bec04d 100644 --- a/episodes/05-intro_to_querying.md +++ b/episodes/05-intro_to_querying.md @@ -122,6 +122,20 @@ SELECT * WHERE { ### Showing labels to Q-numbers +By default, SPARQL queries return Q-numbers instead of human-readable +labels. To show labels, add the `SERVICE wikibase:label` block to your +query: + +``` +SELECT ?item ?itemLabel +WHERE { +?item wdt:P31 wd:Q7075. +SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } +} +``` +The `[AUTO_LANGUAGE]` placeholder automatically uses your browser's +language setting, with English as a fallback. + ### Namespaces and Prefixes 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: ### More conditions -- publications from Addison-Wesley vs. books from Addison-Wesley vs. books authored by Richard Feynman from Addison-Wesley -- LIMIT -- ORDER -- FILTER -- OPTIONAL +### More conditions + +You can combine multiple conditions in a single query. Here is an example +that finds books published by Addison-Wesley authored by Richard Feynman: + +``` +SELECT ?book ?bookLabel WHERE { +?book wdt:P31 wd:Q571. # instance of book +?book wdt:P123 wd:Q353060. # published by Addison-Wesley +?book wdt:P50 wd:Q39246. # authored by Richard Feynman +SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } +} +``` +Useful modifiers: + +- `LIMIT 10` — restrict results to 10 rows +- `ORDER BY DESC(?date)` — sort results by date, newest first +- `FILTER(YEAR(?date) > 2000)` — filter by year +- `OPTIONAL { ?item wdt:P18 ?image }` — include a field only if it exists + ### How to visualize your query @@ -552,13 +581,44 @@ WHERE ## 5\.4 More Advanced queries -further links +Here are some more advanced query patterns that are particularly useful +for library use cases. + +### Federated queries and subqueries +You can use subqueries to pre-filter data before the main query processes +it, which can significantly speed up complex queries: + +``` +SELECT ?item ?itemLabel ?authorLabel WHERE { +?item wdt:P31 wd:Q13442814. # scholarly article +?item wdt:P50 ?author. +?author wdt:P108 wd:Q49210. # affiliated with a specific institution +SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } +} +LIMIT 20 ``` -https://commons.wikimedia.org/wiki/File:Wikidata_Query_Service_in_Brief.pdf -https://www.uni-mannheim.de/media/Einrichtungen/dws/Files_Teaching/Semantic_Web_Technologies/SWT05-SPARQL-v1.pdf -https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial +### Maintenance queries + +Maintenance queries help identify items that are missing information. +These are useful for librarians who want to improve data quality: + ``` +# Libraries without coordinates +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 +``` + +``` +# Scholarly articles without a DOI +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 +``` + +### Further resources + +- [Wikidata Query Service in Brief (PDF)](https://commons.wikimedia.org/wiki/File:Wikidata_Query_Service_in_Brief.pdf) +- [SPARQL tutorial on Wikidata](https://www.wikidata.org/wiki/Wikidata:SPARQL_tutorial) +- [SPARQL lecture slides, University of Mannheim](https://www.uni-mannheim.de/media/Einrichtungen/dws/Files_Teaching/Semantic_Web_Technologies/SWT05-SPARQL-v1.pdf) + :::::::::::::::::::::::::::::::::::::::: keypoints