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
Run ↗
# 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
Run ↗
# 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
Run ↗
# 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
Run ↗
# 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
Run ↗
# See how many results the pattern matches
SELECT (COUNT(?item) AS ?count)
WHERE {
  ?item wdt:P31/wdt:P279* wd:Q33506 .
}
Narrow Scope with Filters
Run ↗
# 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
Run ↗
# 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
Run ↗
# 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
Run ↗
# 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
Run ↗
# 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

Useful Debug Patterns

List All Properties of an Entity
Run ↗
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
Run ↗
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")
  }
}