2

I have following sparql query(from the book, semantic web primer):

select ?n
where
{
    ?x rdf:type uni:Course;
        uni:isTaughtBy :949352
    ?c uni:name ?n .
    FILTER(?c=?x) .
}

In this case, I guess this code is same as the the following:

Select ?n
Where
{
    ?x rdf:type uni:course;
        uni:isTaughtBy :949352 .
    ?x uni:name ?n .
}

Does this query lead to coding error?

2 Answers 2

1

No, I don't see why it should give you an error or produce wrong results. Just make sure to always use the right case (uni:Course vs. uni:course), as SPARQL is case sensitive.

To be honest, the first version seems rather obscure as it uses a FILTER without a real need for it. That said, you may further slim down your query if you wish:

SELECT ?n
WHERE
{
    ?x rdf:type uni:Course;
       uni:isTaughtBy :949352;
       uni:name ?n .
}

However, keep in mind that saving characters does not always lead to improved readability.

1
  • Happy to help. Another improvement: You might try using SELECT DISTINCT to get a list without possible duplicates. Also, if you find my answer useful, you might consider accepting it.
    – cyroxx
    Apr 17, 2013 at 12:54
0

For your example yes the queries are identical and there would be no value in using a FILTER over a join.


However the reason why you might use the FILTER form is the difference in semantics between joins and the = operator

Joins require that the values of the variables be exactly the same RDF term, whereas = does value equality - do the values of RDF terms that represent the same value? This is primarily a concern when one/both of the values may have literal values

It's easier to see if you take a specific example, assume ?x=4 and ?c = 4.0 (which is a bad example for your query but illustrates the point)

?x = ?c would give true while a join would give no results because they are not the exact same term

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.