Search code examples
sparql

Omitting variables in Sparql changes results


I'm querying some data from the Sparql endpoint of the public records of the votings of the Council of European Union.

Currently, I want to get session and act numbers for recorded votes, which I can achieve with this query:

PREFIX codi: <http://data.consilium.europa.eu/def/codi/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX ecv: <http://data.consilium.europa.eu/data/public_voting/rdf/schema/>
PREFIX qb: <http://data.consilium.europa.eu/data/public_voting/qb/dimensionproperty/>

SELECT ?recordedVote ?sessionNumber ?act ?actNumber 
WHERE {
   ?recordedVote a <http://purl.org/linked-data/cube#Observation>.
   ?recordedVote qb:sessionnrnumber ?session.
   ?session skos:prefLabel ?sessionNumber.
   ?recordedVote qb:act ?act.
   ?act skos:prefLabel ?actNumber 

}
LIMIT 10

Note that LIMIT 10 is there only to speed up execution during testing the query. Here are the results: enter image description here

However, I have to include ?act in the select list, otherwise the results are wrong:

enter image description here

Why is that?


Solution

  • The result is not "wrong", they're both correct answers: they give you a subset of 10 solutions of all results that match the query.

    The reason you're getting different solutions in your result is that the SPARQL endpoint optimizes the query execution differently when you leave ?act out of the SELECT clause. It probably switches to a different internal index, and returns solutions in a different order. If you were to leave out the limit, you'd see that both results are the same, just in a different order.

    You can fix this by forcing a specific order to your query, with an ORDER BY clause. For example:

    PREFIX codi: <http://data.consilium.europa.eu/def/codi/>
    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
    PREFIX ecv: <http://data.consilium.europa.eu/data/public_voting/rdf/schema/>
    PREFIX qb: <http://data.consilium.europa.eu/data/public_voting/qb/dimensionproperty/>
    
    SELECT ?recordedVote ?sessionNumber ?act ?actNumber 
    WHERE {
       ?recordedVote a <http://purl.org/linked-data/cube#Observation>.
       ?recordedVote qb:sessionnrnumber ?session.
       ?session skos:prefLabel ?sessionNumber.
       ?recordedVote qb:act ?act.
       ?act skos:prefLabel ?actNumber 
    
    }
    ORDER BY ?act
    LIMIT 10
    

    and

    PREFIX codi: <http://data.consilium.europa.eu/def/codi/>
    PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
    PREFIX ecv: <http://data.consilium.europa.eu/data/public_voting/rdf/schema/>
    PREFIX qb: <http://data.consilium.europa.eu/data/public_voting/qb/dimensionproperty/>
    
    SELECT ?recordedVote ?sessionNumber ?actNumber 
    WHERE {
       ?recordedVote a <http://purl.org/linked-data/cube#Observation>.
       ?recordedVote qb:sessionnrnumber ?session.
       ?session skos:prefLabel ?sessionNumber.
       ?recordedVote qb:act ?act.
       ?act skos:prefLabel ?actNumber 
    } 
    ORDER BY ?act
    LIMIT 10
    

    will return the same result (minus a column, of course).