How to limit SPARQL solution group size?

I'm wondering how queries like:

select the three largest towns of each country

should be expressed in SPARQL 1.1. I hope I'm just missing something obvious, but I can't see a way of limiting the size of solution groups...


[edit] So far I was able to work out the limited case:

select the largest town of each country

as something like:

select ?country ?town where {
   ?town a :Town; :country ?country; :population ?size filter ( ?size = ?max )
   { select (max(?size) as ?max) {
        [] a :Town; :country ?country; :population ?size } group by ?country } }
}

Found a SPARQL 1.1 solution that relies purely on aggregates, GROUP BY and HAVING without any need for sub-queries :-D

Note I used a slightly different schema in my query when I was testing this out but it should be easy enough to edit to fit your schema:

SELECT ?city (SAMPLE(?country) AS ?Country) (SAMPLE(?size) AS ?Population) 
WHERE
{
  ?city a :City ; :locatedIn ?country ; :population ?size .
  ?largerCity a :City ; :locatedIn ?country ; :population ?largerSize .
  FILTER(?largerSize >= ?size)
} 
GROUP BY ?city 
HAVING (COUNT(?largerCity) <= 3)

select the three largest towns of each country

It ain't pretty, or cheap, but FWIW, you can do this in SPARQL.

SELECT ?country ?town 
WHERE {
   ?town a :Town ;  
         :country ?country ; 
         :population ?size .

OPTIONAL {
?town2 a :Town ;
:country ?country ;
:population ?size2 .
FILTER(?size2 > ?size)

   OPTIONAL {
      ?town3 a :Town ;  
         :country ?country ; 
         :population ?size3 .
      FILTER(?town3 != ?town2 &amp;&amp; ?size3 &gt; ?size)

       OPTIONAL {
           ?town4 a :Town ;  
           :country ?country ; 
           :population ?size4 .
         FILTER(?town4 != ?town2 &amp;&amp; ?town4 != ?town3 &amp;&amp; ?size4 &gt; ?size)
       }
   }

}
FILTER(!bound(?town4))
}

Obviously, you could do this a little neater in SPARQL 1.1 (with GROUP BY/EXISTS) but I like being awkward. ;)

There's no way to do it, as far as I've ever been able to figure out. At Cambridge Semantics, we've played with several designs of extensions to support it, but have had trouble getting the semantics right. We've hacked around it by extending GROUP_CONCAT to support ordering and limiting, but that's pretty inelegant as well. We also needed to add capabilities to include terms' serializations in what's concatenated in GROUP_CONCAT, rather than just stringified values. So it's been a lot of work for something that feels like it ought to be easier.

I'd be thrilled if someone had a better way to do it.

I've also been unable to find an elegant way to do it in SQL, which is little but cold comfort.