Advanced

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:

Basic CONSTRUCT Examples

Simple Triple Construction
Run ↗
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.

Extract Labeled Data
Run ↗
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

Complete Entity Description
Run ↗
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

Border Network
Run ↗
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

EntityDescription
wd:Q46Europe
wd:Q515City
wd:Q901Scientist (occupation)
wd:Q6256Country
wdt:P19Place of birth
wdt:P20Place of death
wdt:P30Continent
wdt:P31Instance of
wdt:P36Capital
wdt:P47Shares border with
wdt:P106Occupation
wdt:P1082Population