Advanced

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 FormReturnsUse Case
SELECTTable of resultsRetrieve data
ASKtrue/falseTest existence
CONSTRUCTRDF graphTransform data
DESCRIBERDF descriptionExplore entities

Basic ASK Examples

Is Paris a City?
Run ↗
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.

Is the Earth Larger than the Moon?
Run ↗
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

Did Einstein Study at ETH Zurich?
Run ↗
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.

Are France and Germany Neighbors?
Run ↗
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

Does Tokyo Have a Population Value?
Run ↗
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.

Are There Cities with 10M+ Population?
Run ↗
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

EntityDescription
wd:Q2Earth
wd:Q90Paris
wd:Q142France
wd:Q183Germany
wd:Q405Moon
wd:Q515City
wd:Q937Albert Einstein
wd:Q1490Tokyo
wd:Q11942ETH Zurich
wdt:P31Instance of
wdt:P47Shares border with
wdt:P69Educated at
wdt:P1082Population
wdt:P2046Area