Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 69 additions & 9 deletions episodes/05-intro_to_querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down
Loading