Patterns
Hierarchy Traversal
Navigate class hierarchies and transitive relationships using property paths. Master the P31/P279 pattern for flexible instance matching.
Understanding Wikidata's Class System
Wikidata organizes knowledge using two key properties:
| Property | Meaning | Example |
|---|---|---|
wdt:P31 |
instance of | Python (P31) programming language |
wdt:P279 |
subclass of | art museum (P279) museum |
The combination wdt:P31/wdt:P279* means "is an instance of something
that is (directly or through subclasses) a specific type."
Property Path Syntax
| Syntax | Meaning | Use Case |
|---|---|---|
p* |
Zero or more | Any depth (including self) |
p+ |
One or more | At least one step |
p? |
Zero or one | Optional single step |
p{n} |
Exactly n | Fixed depth |
p{n,m} |
Between n and m | Limited depth range |
p/q |
Sequence | p then q |
p|q |
Alternative | p or q |
^p |
Inverse | Reverse direction |
The Classic Instance-Subclass Pattern
Find All Programming Languages (Including Subtypes)
SELECT ?lang ?langLabel ?type ?typeLabel
WHERE {
# Instance of programming language OR any subclass
?lang wdt:P31/wdt:P279* wd:Q9143 .
# Get the specific type for display
?lang wdt:P31 ?type .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 50
Find All Museums (Art, History, Science, etc.)
SELECT ?museum ?museumLabel ?typeLabel ?countryLabel
WHERE {
?museum wdt:P31/wdt:P279* wd:Q33506 ; # museum
wdt:P31 ?type ;
wdt:P17 ?country .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 50
Exploring Class Hierarchies
Show Subclasses of Programming Language
SELECT ?class ?classLabel ?parentLabel
WHERE {
# Find all subclasses of programming language
?class wdt:P279+ wd:Q9143 .
# Get direct parent for tree visualization
?class wdt:P279 ?parent .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
ORDER BY ?parentLabel ?classLabel
Administrative Hierarchy (City to Country)
SELECT ?place ?placeLabel ?parentLabel
WHERE {
VALUES ?city { wd:Q90 } # Paris
# Traverse P131 (located in administrative unit)
?city wdt:P131* ?place .
OPTIONAL { ?place wdt:P131 ?parent . }
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
Limited Depth Traversal
Subclasses Within 3 Levels
SELECT ?class ?classLabel
WHERE {
# Limit to 1-3 steps (not unlimited)
?class wdt:P279{1,3} wd:Q33506 . # museum
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
Inverse Traversal
Find Parent Classes (Going Up the Hierarchy)
SELECT ?parent ?parentLabel
WHERE {
# Start from "scripting language" and go UP
wd:Q187432 wdt:P279+ ?parent .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
Alternative Paths
Match Multiple Relationship Types
SELECT ?entity ?entityLabel
WHERE {
# Match P31 OR P279 (instance or subclass)
?entity (wdt:P31|wdt:P279) wd:Q9143 .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
LIMIT 30
Common Pitfalls
| Mistake | Problem | Solution |
|---|---|---|
?x wdt:P279* ?y |
Unbounded on both ends | Always fix one end |
wdt:P279* without wdt:P31 |
Matches classes, not instances | Use wdt:P31/wdt:P279* |
| Deep hierarchies | Timeout on large trees | Use depth limits {1,5} |