Search code examples
rdfsparql

Paginating SPARQL results


Suppose I have the following SPARQL query:

SELECT DISTINCT ?result ?label
WHERE {
  ?result a database:Column .
  ?result rdfs:label ?label .
}
ORDER BY (LCASE(?label))

What can I add to the query to limit the number of results to the first 10 given? Or preferably, the 10 results after the first n×10 results? I'm trying to implement pagination for visualizing the results.


Solution

  • I'm trying to implement a system of paging for a table that visualizes the returned data.

    You want to use limit, order by, and offset. They're described pretty well in the standard:

    15.4 OFFSET

    OFFSET causes the solutions generated to start after the specified number of solutions. An OFFSET of zero has no effect.

    Using LIMIT and OFFSET to select different subsets of the query solutions will not be useful unless the order is made predictable by using ORDER BY.

    15.5 LIMIT

    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.

    In your case, your query would be something like this to show the fourth page of results when you are showing ten results per page:

    SELECT DISTINCT ?result ?label
    WHERE {
      ?result a database:Column .
      ?result rdfs:label ?label .
    }
    ORDER BY (LCASE(?label))
    LIMIT 10
    OFFSET 30