I am new to wikidata and I can't figure out when I should use -->
wdt prefix (http://www.wikidata.org/prop/direct/)
and when I should use -->
p prefix (http://www.wikidata.org/prop/).
in my sparql queries. Can someone explain what each of these mean and what is the difference?
Wikidata is a collaboratively edited knowledge base. It is a source of open data that you may want to use in your projects. Wikidata offers a query service for integrations. The query service can be accessed by navigating to query.wikidata.org If we go there, we’ll see the following screen: Figure 2.
To do this we’ll construct the predicate wdt:P569, where: In line with the SPARQL model of everything as a triple, the wdt: namespace contains manifestations of properties as simple predicates that can directly connect an item to a value. Let’s update our query to return date of birth:
In line with the SPARQL model of everything as a triple, the wdt: namespace contains manifestations of properties as simple predicates that can directly connect an item to a value. — Wikidata:SPARQL query service/queries
Wikidata is a collaboratively edited knowledge base. It is a source of open data that you may want to use in your projects. Wikidata offers a query service for integrations.
If you go to the Wikidata item page for Barack Obama at https://www.wikidata.org/wiki/Q76 and scroll down, you see the entry for the "spouse" property P26
:
Think of the p:
prefix as a way to get to the entire white box on the right side of the image.
In order to get to the information inside the white box, you need to dig deeper.
In order to get to the main part of the information ("Michelle Obama"), you combine the p:
prefix with the ps:
prefix like this:
SELECT ?spouse WHERE {
wd:Q76 p:P26 ?s .
?s ps:P26 ?spouse .
}
The variable ?s
is an abstract statement node (aka the white box).
You can get the same information with only one triple in the body of the query by using wdt:
:
SELECT ?spouse WHERE {
wd:Q76 wdt:P26 ?spouse .
}
So why would you ever use p:
?
You might have noticed that the white box also contains meta information ("start time" and "place of marriage").
In order to get to the meta information, you combine the p:
prefix with the pq:
prefix.
The following example query returns all the information together with the statement node:
SELECT ?s ?spouse ?time ?place WHERE {
wd:Q76 p:P26 ?s .
?s ps:P26 ?spouse .
?s pq:P580 ?time .
?s pq:P2842 ?place .
}
Things in the p:
namespace are used to select statements. Things in the wdt:
namespace are used to select entites. Entity selection, with wdt:
, allows you to simplify or summarize more complex queries involving statement selection.
When you see a p:
you are usually going to see a ps:
or pq:
shortly following. This is because you rarely want a list of statements; you usually want to know something about those statements.
This example is a two-step process showing you all the graffiti in Wikidata:
SELECT ?graffiti ?graffitiLabel
WHERE
{
?graffiti p:P31 ?statement . # entities that are statements
?statement ps:P31 wd:Q17514 . # which state something is graffiti
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
Two different versions of the P31
property are used here, housed in different namespaces. Each version comes with different expectations about how it will connect to other items. Things in the p:
namespace connect entities to statements, and things in the ps:
namespace connect statements to values. In the example, p:P31
is used to select statements about an entity. The entity will be graffiti, but we do not specify that until the next line, where ps:P31
is used to select the values (subjects) of the statements, specifying that those values should be graffiti.
So, that's kind of complicated! The wdt:
namespace is supposed to make this kind of query simper. The example could be rewritten as:
SELECT ?graffiti ?graffitiLabel
WHERE
{
?graffiti wdt:P31 wd:Q17514 . # entities that are graffiti
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
This is now one line shorter because we are no longer looking for statements about graffiti, but for graffiti itself. The dual p:
and ps:
linkages are summarized with a wdt:
version of the same P31
property. However, be aware:
wdt:
stands for "truthy").wdt:
is just missing some facts, sometimes. Often in my experience a p:
and ps:
query will return a few more results than a wdt:
query.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With