PREFIX
Define namespace shortcuts for cleaner queries. PREFIX declarations make URIs more readable by creating short aliases for common namespaces.
Understanding PREFIX
In RDF, everything is identified by URIs. PREFIX creates shortcuts so you can write
wd:Q42 instead of <http://www.wikidata.org/entity/Q42>.
Wikidata Query Service has several prefixes pre-defined, so most queries don't need explicit PREFIX declarations. However, understanding them helps when working with other SPARQL endpoints.
Common Wikidata Prefixes
| Prefix | Namespace | Purpose |
|---|---|---|
wd: | http://www.wikidata.org/entity/ | Wikidata items (Q numbers) |
wdt: | http://www.wikidata.org/prop/direct/ | Direct property values (P numbers) |
p: | http://www.wikidata.org/prop/ | Property statements |
ps: | http://www.wikidata.org/prop/statement/ | Statement values |
pq: | http://www.wikidata.org/prop/qualifier/ | Qualifier values |
rdfs: | http://www.w3.org/2000/01/rdf-schema# | RDF Schema (labels, comments) |
Using Predefined Prefixes
SELECT ?person ?personLabel
WHERE {
?person wdt:P31 wd:Q5 ;
wdt:P106 wd:Q937857 .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
LIMIT 10
On the Wikidata Query Service, prefixes like wd:, wdt:,
wikibase:, and bd: are already defined. This query finds
humans (Q5) with occupation (P106) of footballer (Q937857).
Explicit PREFIX Declarations
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
SELECT ?country ?capital
WHERE {
?country wdt:P31 wd:Q6256 ;
wdt:P36 ?capital .
}
LIMIT 5
When running queries on other SPARQL endpoints, you may need to declare prefixes explicitly. PREFIX declarations go at the beginning of the query, before SELECT.
Custom Prefixes
PREFIX schema: <http://schema.org/>
SELECT ?article ?title
WHERE {
?article schema:about wd:Q42 ;
schema:name ?title ;
schema:isPartOf <https://en.wikipedia.org/> .
}
You can define any prefix you need. This query uses the schema.org namespace to find the English Wikipedia article about Douglas Adams (Q42).
Full URIs Without Prefixes
SELECT ?country
WHERE {
?country <http://www.wikidata.org/prop/direct/P31> <http://www.wikidata.org/entity/Q6256> .
}
LIMIT 5
You can always use full URIs in angle brackets instead of prefixed names. This is verbose but sometimes necessary for URIs that don't fit a common prefix.
Key Entities Used in Examples
| Entity | Description |
|---|---|
wd:Q5 | Human |
wd:Q42 | Douglas Adams |
wd:Q6256 | Country |
wd:Q937857 | Footballer |
wdt:P31 | Instance of |
wdt:P36 | Capital |
wdt:P106 | Occupation |