Basic

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

PrefixNamespacePurpose
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

Standard Wikidata Query
Run ↗
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

Query with Explicit Prefixes
Run ↗
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

Define Your Own Prefixes
Run ↗
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

Using Full URIs
Run ↗
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

EntityDescription
wd:Q5Human
wd:Q42Douglas Adams
wd:Q6256Country
wd:Q937857Footballer
wdt:P31Instance of
wdt:P36Capital
wdt:P106Occupation