70 
 
SELECT *  WHERE { ?pizza pizza:purchasedByCustomer ?customer} 
This will show you the two pizzas where the purchase relation was asserted on the instance of Pizza rather 
than on the instance of Customer.  
Suppose you wanted to see all of the things that are objects of Customer?  With a couple of new 
constructs this is simple. First, in SPARQL a shortcut to identify the type of any entity is to use the 
keyword a as the predicate. This is just shorthand for rdf:type. Second, when you have multiple 
statements in a WHERE clause you need to end each one with a period.  
Replace the current query with the following: 
SELECT * 
WHERE { ?customer a pizza:Customer. 
                  ?customer ?relation ?relatedToCustomer.} 
This will provide a long list of everything in the graph that is an object of some instance of the Customer 
class. I.e., any entity that is the object of a predicate with a Customer as the subject. 
Suppose you wanted to count the number of Pizzas purchased by Customers so far. For this you use the 
SPARQL function COUNT. Here is what it would look like: 
SELECT (COUNT(?pizza) AS ?pcount) 
WHERE {?customer pizza:purchasedPizza ?pizza} 
Paste that into the SPARQL query view and hit Execute and you should see the returned value: 15. 
However, remember this isn’t really all the Pizzas because a few of the purchases were recorded on the 
Pizza rather than on the Customer. To get the full number we can take advantage of the fact that we 
have recorded the number of pizzas that each customer has purchased and use the SPARQL SUM 
function. That query would be: 
SELECT (SUM(?pnumber) AS ?psum) 
WHERE { ?customer pizza:numberOfPizzasPurchased ?pnumber} 
This should give you the correct number of 17.  
9.22 SPARQL and IRI Names 
If you recall, at the beginning of the tutorial we changed the preference for creating new entities to user 
supplied name rather than auto-generated name which is the default. Now that you have seen examples of 
using SPARQL we can explain why. With auto-generated names rather than have names such as 
Customer or purchasedByCustomer our entities would have names such as 
OWLPropertyA4257yri73ff90rmbx and OWLClass23gkb0tk5kd30tm. Thus, the first query would 
be something like: 
SELECT * WHERE {?customer pizza:OWLPropertyA4257yri73ff90rmbx ?pizza} 
and the second query would be:  
SELECT * 
WHERE {?customer a pizza:OWLClass23gkb0tk5kd30tm.