# Limit and sort per level?

**URL:** <https://community.stardog.com/t/limit-and-sort-per-level/3242>\
**Category:** Support\
**Created:** [September 6, 2021, 1:11pm UTC](https://community.stardog.com/t/limit-and-sort-per-level/3242 "2021-09-06T13:11:56Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![lapidus](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/lapidus/32/1076_2.png) [@lapidus](https://community.stardog.com/u/lapidus)\
**Post date:** [September 6, 2021, 2:56pm UTC](https://community.stardog.com/t/limit-and-sort-per-level/3242/5 "2021-09-06T14:56:09Z")

</div>

Thank you.

Hmm, I wonder if I can make this generic enough to work for any combination of classes and properties without having to predefine Stored Queries?

It also needs to tap into the language fallback functionality:

> [@Fallback language?](https://community.stardog.com/t/fallback-language/3239/3):
>
> Thank you! Will explore and report back!

So a complete, more generic example would be:

1. The class :Country has properties :name, :headOfState.

2. The class :Person has properties :name, :birthDate

3. We can determine a priori that :name is an rdf:langString and want to use that information to apply a fallback.

4. We want to get the top 5 Countries ranked by population and we want to get 3 Heads of State ranked by birthDate.

So basically:

```auto
?country a :Country;
   :population ?population .
   :name ?name . // Apply Fallback

   ORDER BY ?population
   LIMIT 5

// Correlated Query:

?country :headOfState ?Person;
   :birthDate ?birthDate .
   :name ?name . // Apply Fallback

   ORDER BY ?birthDate
   LIMIT 3

```

Can this be achieved? I.e. how customizable is the SQS? Can it accept entire graph patterns?  
(We have 1000s of variations on this theme so a generic solution is important 😅)

Thanks!

---

_[View the full topic](https://community.stardog.com/t/limit-and-sort-per-level/3242)._
