For each customer, return their name and surname, as well as the list of all products this customer bought, which have a maximum price of 150 EUR.
SELECT {
?customer cheapItems _:cheapItems ;
name ?name ;
surname ?surname .
_:cheapItems item ?item .
?item name ?itemName ;
price ?itemPrice .
}
WHERE {
?customer -12/14 ?order .
?order -42/40 ?item .
?item 56 ?itemName ;
57 ?itemPrice .
FILTER (?itemPrice <= 150)
}
WHEREclause does pattern matching on the schema category - generates solutions matching this pattern (using triplessubject predicate object .)?customeris a variable named customer56is the ID of a morphism, in this case the morphism from Product to Name. Morphisms prefixed with-are duals, i.e. opposite direction.- Note: I arbitrarily assigned IDs to the morphisms from the categorical approach PDF from Pavel Koupil.
- We can form paths using morphisms:
-12/14means to traverse the dual of12, and then traverse14. ;is syntactic sugar for having multiple triples with the same subject, equivalent to simply repeating the subject again in the next triple.FILTERremoves some solutions from consideration. Only solutions matching theFILTERare returned.
SELECTclause describes the returned data - it implicitly defines a schema category, and returned data will be instances of this category.- Using an alphanumeric string in the predicate position in
SELECTdefines a new morphism (and implicitly its dual also). - Using bound variables will simply substitute that variable's data.
_:cheapItemsis syntax for a blank node named cheapItems - this is necessary in order to add new objects to the returned schema, otherwise we could only use bound variables.- In this case, because
_:cheapItemsis first defined in the object position, a new instance will be created for each?customerinstance. - If
_:cheapItemswas first defined in the subject position, there would be a single object shared between all customers.
- In this case, because
- Using an alphanumeric string in the predicate position in