CONSTRUCT
Create new RDF graphs from query results. CONSTRUCT transforms data into a specified graph structure, enabling data reshaping and export.
Understanding CONSTRUCT
CONSTRUCT queries return an RDF graph rather than a table of bindings. You define a template of triples, and the query engine fills in variable values from the WHERE clause to produce the output graph.
CONSTRUCT is useful for:
- Transforming data into a different vocabulary or schema
- Extracting subgraphs for export
- Creating simplified views of complex data
- Converting between ontologies
Basic CONSTRUCT Examples
CONSTRUCT {
?country <http://example.org/hasCapital> ?capital .
}
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P36 ?capital .
}
LIMIT 10
This query finds countries and their capitals, then outputs them using a custom
predicate http://example.org/hasCapital instead of the Wikidata property.
CONSTRUCT {
?city rdfs:label ?cityLabel .
?city <http://example.org/population> ?population .
}
WHERE {
?city wdt:P31 wd:Q515 ;
wdt:P1082 ?population .
FILTER(?population > 5000000)
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
This query extracts major cities (population over 5 million) and outputs them with their English labels and population values using custom predicates.
Multiple Triple Patterns
CONSTRUCT {
?person a <http://example.org/Scientist> .
?person rdfs:label ?personLabel .
?person <http://example.org/bornIn> ?birthPlace .
?person <http://example.org/diedIn> ?deathPlace .
}
WHERE {
?person wdt:P106 wd:Q901 ;
wdt:P19 ?birthPlace ;
wdt:P20 ?deathPlace .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 20
This query creates a small RDF dataset about scientists (Q901 occupation), including their type classification, name, birth place, and death place — all using a custom vocabulary.
Creating Relationships
CONSTRUCT {
?country1 <http://example.org/neighbor> ?country2 .
}
WHERE {
?country1 wdt:P31 wd:Q6256 ;
wdt:P30 wd:Q46 ;
wdt:P47 ?country2 .
?country2 wdt:P30 wd:Q46 .
}
This query constructs a network graph of European countries (Q46 continent) and their border relationships. The result can be used for network analysis or visualization.
Key Entities Used in Examples
| Entity | Description |
|---|---|
wd:Q46 | Europe |
wd:Q515 | City |
wd:Q901 | Scientist (occupation) |
wd:Q6256 | Country |
wdt:P19 | Place of birth |
wdt:P20 | Place of death |
wdt:P30 | Continent |
wdt:P31 | Instance of |
wdt:P36 | Capital |
wdt:P47 | Shares border with |
wdt:P106 | Occupation |
wdt:P1082 | Population |