How do I limit the number of results for a specific variable in a SPARQL query?

Let's say I have a SPARQL query like this, looking for resources that have some shared property with a focal resource, and also getting some other statements about the focal resource :

CONSTRUCT {
  ?focal pred:icate ?shared .
  ?other pred:icate ?shared .
}
WHERE {
  ?focal pred:icate ?shared ;
         more:info ?etc ;
         a "foobar" .
  ?other pred:icate ?shared .
}
LIMIT 500

If there are more than 500 other resources, that LIMIT might exclude that more:info statement and object. So, is there a way to say "I only want at most 500 of ?other", or do I have to break this query into multiple pieces?

Upvotes: 13

Views: 13274

Answers (2)

Savino Sguera
Savino Sguera

Reputation: 3572

http://www.w3.org/TR/2012/WD-sparql11-query-20120105/#modResultLimit

The LIMIT clause puts an upper bound on the number of solutions returned. If the number of actual solutions, after OFFSET is applied, is greater than the limit, then at most the limit number of solutions will be returned.

You can only limit the number of solutions to your query, not a specific subset of it. You can use a subquery with a LIMIT clause though: http://www.w3.org/TR/sparql-features/#Subqueries.

Upvotes: 1

Jan
Jan

Reputation: 739

You can use LIMIT in subqueries, i.e. something like the following:

CONSTRUCT {
  ?focal pred:icate ?shared .
  ?other pred:icate ?shared .
}
WHERE {
    ?focal pred:icate ?shared ;
           more:info ?etc ;
           a "foobar" .
    { 
      SELECT ?shared {
        ?other pred:icate ?shared .
      }
      LIMIT 500
    }
}

Upvotes: 13

Related Questions