# Adding sorting to DQL query reduces the number of results

**URL:** <https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245>\
**Category:** Dgraph\
**Tags:** kind:bug, dql\
**Created:** [August 9, 2021, 2:53pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245 "2021-08-09T14:53:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielhr](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@danielhr](https://discuss.dgraph.io/u/danielhr)\
**Post date:** [August 9, 2021, 2:53pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/1 "2021-08-09T14:53:02Z")

</div>

### What version of Dgraph are you using?

> **Dgraph (Cloud) Version: v21.03.0-58-g17f52bc78**
>

Similar to [this issue](http://discuss.hypermode.com/t/changing-order-from-descending-to-ascending-produces-empty-results-in-query-with-cascade-and-pagination/15229) but here there’s no offset being used and the query is in DQL.

When adding sorting to a query, the number of nodes returned decreases significantly. I wrote a query that returned 2196 results without sorting and 414 results when adding sorting. The query uses `@cascade` but no pagination.

 ![Screen Shot 2021-08-09 at 4.06.36 AM](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/d/d9d3733c97758e4c34bbedb051f4ff205b39bb9d.png)  
 ![Screen Shot 2021-08-09 at 4.06.56 AM](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/e/e3798575da96ed382f668b0fa739ed53c4de25a0.png)

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [August 9, 2021, 9:03pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/2 "2021-08-09T21:03:15Z")

</div>

> [@danielhr](#):
>
> The query uses `@cascade` but no pagination.

Cascade means something like “Return only those who match the request”. So, if the other values have no value in the sorting field target. It probably will be ignored. Check if that is true. You can’t point to an issue in a feature while using another feature in combination. I mean, you can, but the issue should be related to both at least.

Add a value to the other 1782 nodes.

---

<div class="post-metadata">

**Author:** ![danielhr](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@danielhr](https://discuss.dgraph.io/u/danielhr)\
**Post date:** [August 9, 2021, 9:26pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/3 "2021-08-09T21:26:20Z")

</div>

Thanks for the reply. Note that both queries are cascading by the same fields and the predicate I’m using to sort (`Product.currentPrice`) isn’t in them. Furthermore, I verified there are only about 200 nodes that don’t have that predicate. So I still think there might be some sort of a bug here. I can share the full queries with you privately if needed.

 ![Screen Shot 2021-08-09 at 4.14.53 PM](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2ed7c013ff43a743e9946f304a7a4cc8ee00abdf.png)  
 ![Screen Shot 2021-08-09 at 4.17.35 PM](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/a/a57bbe02b5ad917f80f626603235927e3afa938c.png)

EDIT: in case you haven’t, please take a look at this [similar issue](http://discuss.hypermode.com/t/changing-order-from-descending-to-ascending-produces-empty-results-in-query-with-cascade-and-pagination/15229). There, the only difference in the query is changing the sorting from ascending to descending, which again, decreases the number of results

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [August 9, 2021, 11:46pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/4 "2021-08-09T23:46:00Z")

</div>

In the previous, you are using `Product.uploadedOn` - which I suppose to be DateTime? Have you checked if all of them have the correct date format?

What helps is reproducing with a small example. If you can’t reproduce with a controlled scenario, it might mean you have a messy dataset. Or a bad query.

---

<div class="post-metadata">

**Author:** ![Daniel](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/daniel/32/8499_2.png) [@Daniel](https://discuss.dgraph.io/u/Daniel)\
**Post date:** [September 18, 2021, 1:38pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/5 "2021-09-18T13:38:58Z")

</div>

Having a similar problem with “reduced number of results” when adding sorting.  
The only difference between these two queries is the **orderasc:year**. Yet, they are producing different number of results. What could be the cause?

```auto
{
  example( func: eq(cube,"cube2022", "cube2023"), orderasc:year ) 
   @cascade {
     cube
       year 
       country @filter( eq(code, "SWE")){
		code
      }
   }
}

```

 ![image](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/4/4811876fc628d3a7c3a2e65a1447e77ecfda8938.png)

 ![image](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/3/355066945a89c26e4f25be5c1e26e8078798bab1.png)

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [September 18, 2021, 2:20pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/6 "2021-09-18T14:20:02Z")

</div>

Hi Daniel,

It is a good practice to open a new issue even if it is related to these questions. You can even put a link as a reference. But commenting in old issues provoque email triggering.

> [@Daniel](#):
>
> Yet, they are producing a different number of results. What could be the cause?

Maybe your data doesn’t have the predicate year in all nodes. Also, cascade filter those who don’t match the query body request.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [September 18, 2021, 2:44pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/7 "2021-09-18T14:44:07Z")

</div>

There is a known bug/feature (depends on who you ask) that if you order by a field that is not available it doesn’t do logical NULLS first nor NULLS last, but rather NO NULLS.

Let me go find a linked article about this if I remember.

* * *

Here it is: [Expected behaviour for sort queries](http://discuss.hypermode.com/t/expected-behaviour-for-sort-queries/7157)

So it was agreed upon to “fix” this, but I still don’t think it was fixed yet. ☹

@Naman @mrjn @aman-bansal

---

<div class="post-metadata">

**Author:** ![Daniel](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/daniel/32/8499_2.png) [@Daniel](https://discuss.dgraph.io/u/Daniel)\
**Post date:** [September 19, 2021, 7:52am UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/8 "2021-09-19T07:52:11Z")

</div>

> It is a good practice to open a new issue even if it is related to these questions.

OK! Will do for next issue.

> Maybe your data doesn’t have the predicate year in all nodes. Also, cascade filter those who don’t match the query body request.

I believe it has the same predicates (doublechecked using has(predicate)). It seems it could be related to this. I am experiencing something similar, where I try to filter a lower level but it will only loop over 1000 items despite setting `first: 1000000`. It seems (as @iluminae writes) that the “orderasc” **cancels out the possibility to filter all records** before deciding on a final pagination limit:

> **[Sorting applies pagination before @cascade](https://discuss.dgraph.io/t/sorting-applies-pagination-before-cascade/15554)**
>
> What version of Dgraph are you using? ▶ Dgraph Version Have you tried reproducing the issue with the latest release? yes What is the hardware spec (RAM, OS)? 12 alpha @ 16c 32GiB ram on k8s. Steps to reproduce the issue (command/config used...

---

<div class="post-metadata">

**Author:** ![danielhr](https://avatars.discourse-cdn.com/v4/letter/d/7ea924/32.png) [@danielhr](https://discuss.dgraph.io/u/danielhr)\
**Post date:** [September 21, 2021, 3:48pm UTC](https://discuss.dgraph.io/t/adding-sorting-to-dql-query-reduces-the-number-of-results/15245/9 "2021-09-21T15:48:01Z")

</div>

Is there any ETA on when this will be fixed?
