query-2227ab85f8d40a209e8f13147b01d96d
- Basic model and SPARQL syntaxFirst, let's look at a simple fragment of SPARQL. This shows the subject / property / value approached used in queries for a single "triple". ?person wdt:P31 wd:Q5 item property value #1 #2 #3 . The "wdt" prefix indicates we want the best available value of a property, and the "wd" indicates the value should be a specified Wikidata item. (Q5)human , with 3) value (P31)instance of This says we want to 1) select an item with the variable ?person, which has a property statement using 2) property There is also a more complicated syntax which we can use where there are likely to be several different values for a property on an item: ?person p:P39 ?ps . ?ps ps:P39 wd:Q77685926 . - a member of the current (2024) Parliament. (Q126063447)member of the 59th Parliament of the United Kingdom using the p: prefix, to give us the statement as variable ?ps. Then it adds a second clause, to say the statement should have value (ps: prefix) the specific statement, and then selects (P39)position held This matches a variable ?person which has a property statement using We can then link the two together to get a combined fragment: ?person wdt:P31 wd:Q5 . ?person p:P39 ?ps . ?ps ps:P39 wd:Q126063447 . who are members of the current Parliament. In theory we might not need the first section - but it is good practice to include it, as Wikidata models a small number of fictional MPs! andThis will give us values which are defined as people, . These are values attached to the main statement to improve or extend it in some way. qualifiersThe main reason to use this slightly more complicated "position statement" syntax, rather than the simple wdt:, is to allow us to access the statement's ?person wdt:P31 wd:Q5 . ?person p:P39 ?ps . ?ps ps:P39 wd:Q126063447 . ?ps pq:P580 ?start . This goes into the position statement ?ps to find the start time qualifier ?start (note the pq: prefix). . The data model used for the UK is constructed on a "per-Parliament" basis, where each position refers to the specific Parliamentary term. We can select a range of these by using a more detailed query: (Q126063447)member of the 59th Parliament of the United Kingdom You will note that we have been looking at the specific period ?person wdt:P31 wd:Q5 . ?person p:P39 ?ps . ?ps ps:P39 ?term . ?term wdt:P279 wd:Q16707842 . - eg a member of the 55th Parliament, the 37th Parliament, etc. (Q16707842)Member of Parliament of (P279)subclass of This finds all MPs who hold a position (?ps), where the value (?term) of that position is Now that we have constructed our basic query syntax, we can look at putting it into practice. For our "current Parliament" query, this would be:
Use at
- https://query.wikidata.org/sparql
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX p: <http://www.wikidata.org/prop/>
SELECT DISTINCT ?person WHERE
{
?person wdt:P31 wd:Q5 . ?person p:P39 ?ps . ?ps ps:P39 wd:Q126063447 .
}
Query found at
graph TD
classDef projected fill:lightgreen;
classDef literal fill:orange;
classDef iri fill:yellow;
v1("?person"):::projected
v2("?ps")
c2(["wd:Q5"]):::iri
c5(["wd:Q126063447"]):::iri
v1 --"p:direct/P31"--> c2
v1 --"p:P39"--> v2
v2 --"p:statement/P39"--> c5