Patterns
Debugging Queries
Diagnose and fix query problems. Learn techniques for debugging empty results, timeouts, unexpected output, and syntax errors.
Common Problems
| Symptom | Likely Cause | Debugging Approach |
|---|---|---|
| Empty results | Over-constrained, typos, wrong properties | Remove patterns one by one |
| Timeout | Too broad, unbounded traversal | Add LIMIT, narrow scope |
| Too many results | Missing constraints, cartesian product | Check variable connections |
| Wrong data | Wrong property, type mismatch | Inspect raw values |
| Syntax error | Missing brackets, typos | Check error message location |
Debug Empty Results
Step 1: Simplify to Minimum
# Start with just the core pattern
SELECT ?item
WHERE {
?item wdt:P31 wd:Q9143 . # Just this - does it return results?
}
LIMIT 5
Step 2: Check Entity Exists
# Verify the entity you're querying exists
SELECT ?item ?itemLabel
WHERE {
VALUES ?item { wd:Q28865 } # Python
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
Step 3: Check Property Has Values
# See what properties an entity actually has
SELECT ?prop ?propLabel ?value
WHERE {
wd:Q28865 ?prop ?value .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 50
Debug Timeouts
Add Aggressive LIMIT First
# Force small result set to test if query works
SELECT ?item ?itemLabel
WHERE {
?item wdt:P31/wdt:P279* wd:Q33506 . # museum
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 5 # Very small limit to test
Check Result Count
# See how many results the pattern matches
SELECT (COUNT(?item) AS ?count)
WHERE {
?item wdt:P31/wdt:P279* wd:Q33506 .
}
Narrow Scope with Filters
# Add constraints to reduce result set
SELECT ?museum ?museumLabel
WHERE {
?museum wdt:P17 wd:Q142 ; # France only
wdt:P31/wdt:P279* wd:Q33506 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
LIMIT 50
Debug Wrong Data
Inspect Raw Values
# Don't use label service - see raw data
SELECT ?item ?value (DATATYPE(?value) AS ?type)
WHERE {
?item wdt:P31 wd:Q9143 ;
wdt:P571 ?value . # inception date
}
LIMIT 10
Check for Multiple Types
# See all types an entity has (may have multiple P31 values)
SELECT ?type ?typeLabel
WHERE {
wd:Q28865 wdt:P31 ?type . # Python
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
Debug Duplicate Rows
Identify Multi-Valued Properties
# Count values per entity to find multiplication source
SELECT ?lang ?langLabel
(COUNT(?paradigm) AS ?paradigmCount)
(COUNT(?designer) AS ?designerCount)
WHERE {
?lang wdt:P31 wd:Q9143 .
OPTIONAL { ?lang wdt:P3966 ?paradigm . }
OPTIONAL { ?lang wdt:P287 ?designer . }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
GROUP BY ?lang ?langLabel
HAVING(?paradigmCount > 1 || ?designerCount > 1)
LIMIT 20
Debug Syntax Errors
| Error Message | Likely Problem | Fix |
|---|---|---|
| Lexical error | Unmatched quotes, brackets | Check string delimiters |
| Unexpected token | Missing period, keyword typo | Check line before error |
| Variable not in scope | Variable used before definition | Check pattern order |
| Unknown prefix | Missing or wrong prefix declaration | Add PREFIX declaration |
Common Syntax Issues
# CORRECT: Periods end triple patterns
SELECT ?item
WHERE {
?item wdt:P31 wd:Q9143 . # Period here!
?item wdt:P287 ?designer . # Period here!
}
# CORRECT: Semicolon continues same subject
SELECT ?item
WHERE {
?item wdt:P31 wd:Q9143 ; # Semicolon - same subject
wdt:P287 ?designer . # Period ends
}
# CORRECT: Comma continues same predicate
SELECT ?item
WHERE {
?item wdt:P31 wd:Q9143 , # Comma - same predicate
wd:Q899523 . # Another object
}
Debugging Checklist
- Empty results? — Remove patterns one by one until results appear
- Timeout? — Add LIMIT 5, then narrow scope
- Wrong data? — Remove label service, inspect raw values
- Duplicates? — Use DISTINCT or GROUP BY with aggregates
- Syntax error? — Check line before the reported error
- Check entity IDs — Verify Q-numbers on wikidata.org
- Check property IDs — Verify P-numbers exist and have data
- Incremental build — Start simple, add complexity gradually
Useful Debug Patterns
List All Properties of an Entity
SELECT ?prop ?propLabel ?value ?valueLabel
WHERE {
wd:Q28865 ?p ?value .
?prop wikibase:directClaim ?p .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}
ORDER BY ?propLabel
Find Property by Name
SELECT ?prop ?propLabel ?propDescription
WHERE {
?prop wikibase:directClaim ?directProp ;
rdfs:label ?propLabel .
FILTER(LANG(?propLabel) = "en")
FILTER(CONTAINS(LCASE(?propLabel), "paradigm"))
OPTIONAL {
?prop schema:description ?propDescription .
FILTER(LANG(?propDescription) = "en")
}
}