ASK
Test whether a pattern exists. ASK queries return a simple true/false answer, perfect for checking conditions without retrieving actual data.
Understanding ASK
ASK is one of SPARQL's four query forms. Unlike SELECT which returns matching data,
ASK returns only a boolean: true if at least one solution exists,
false otherwise.
| Query Form | Returns | Use Case |
|---|---|---|
SELECT | Table of results | Retrieve data |
ASK | true/false | Test existence |
CONSTRUCT | RDF graph | Transform data |
DESCRIBE | RDF description | Explore entities |
Basic ASK Examples
ASK {
wd:Q90 wdt:P31 wd:Q515 .
}
This query checks if Paris (Q90) is an instance of (P31) a city (Q515).
The result is simply true because this statement exists in Wikidata.
ASK {
wd:Q2 wdt:P2046 ?earthArea .
wd:Q405 wdt:P2046 ?moonArea .
FILTER(?earthArea > ?moonArea)
}
ASK queries can include FILTER conditions. This query compares the surface area (P2046) of Earth (Q2) and the Moon (Q405) to verify that Earth is larger.
Checking for Relationships
ASK {
wd:Q937 wdt:P69 wd:Q11942 .
}
This query verifies whether Albert Einstein (Q937) has "educated at" (P69) relationship with ETH Zurich (Q11942). It returns true, confirming the connection.
ASK {
wd:Q142 wdt:P47 wd:Q183 .
}
Check geographic relationships. This confirms that France (Q142) shares a border with (P47) Germany (Q183).
Checking for Property Existence
ASK {
wd:Q1490 wdt:P1082 ?population .
}
Use ASK to check whether an entity has a particular property defined. This verifies that Tokyo (Q1490) has a population (P1082) value in Wikidata.
ASK {
?city wdt:P31 wd:Q515 ;
wdt:P1082 ?population .
FILTER(?population > 10000000)
}
ASK can test whether any entities match complex criteria. This confirms that Wikidata contains cities with populations exceeding 10 million.
Key Entities Used in Examples
| Entity | Description |
|---|---|
wd:Q2 | Earth |
wd:Q90 | Paris |
wd:Q142 | France |
wd:Q183 | Germany |
wd:Q405 | Moon |
wd:Q515 | City |
wd:Q937 | Albert Einstein |
wd:Q1490 | Tokyo |
wd:Q11942 | ETH Zurich |
wdt:P31 | Instance of |
wdt:P47 | Shares border with |
wdt:P69 | Educated at |
wdt:P1082 | Population |
wdt:P2046 | Area |