Search code examples
sparqlrdfwikidata

Querying WikiData, difference between p and wdt default prefix


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?


Solution

  • 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:

    • This technique only works for statements that are true or false in nature, like, is a thing graffiti or not. (The "t" in wdt: stands for "truthy").
    • Information available to 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.