# Elasticsearch SQL LIMIT returns last 10 results instead of first 10 results

**URL:** <https://discuss.elastic.co/t/elasticsearch-sql-limit-returns-last-10-results-instead-of-first-10-results/197450>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [August 30, 2019, 6:50am UTC](https://discuss.elastic.co/t/elasticsearch-sql-limit-returns-last-10-results-instead-of-first-10-results/197450 "2019-08-30T06:50:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![herrBez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/herrbez/32/46108_2.png) [@herrBez](https://discuss.elastic.co/u/herrBez)\
**Post date:** [August 30, 2019, 6:50am UTC](https://discuss.elastic.co/t/elasticsearch-sql-limit-returns-last-10-results-instead-of-first-10-results/197450/1 "2019-08-30T06:50:19Z")

</div>

Hi,  
I am trying to visualize some data in a Canvas and I am using elasticsearch sql to build some visualizations. I am importing data from winlogbeat and I have written this query:

```auto
SELECT count(*) as c, winlog.event_id 
FROM "winlogbeat-*" 
GROUP BY winlog.event_id ORDER BY c DESC 

```

This query works fine and shows the elements in the right order. However, when I add a LIMIT statement

```auto
SELECT count(*) as c, winlog.event_id 
FROM "winlogbeat-*"
GROUP BY winlog.event_id
ORDER BY c DESC
LIMIT 10 

```

Instead of getting the first results I receive the last results.  
(For reference I am using Elasticsearch 6.7)

My expected behavior is that it should return the events with the highest count

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [September 2, 2019, 8:08am UTC](https://discuss.elastic.co/t/elasticsearch-sql-limit-returns-last-10-results-instead-of-first-10-results/197450/2 "2019-09-02T08:08:24Z")

</div>

@herrBez thank you for posting this question.  
Indeed, this is a bug. And fortunately, it has been fixed in the meantime.

The initial bug report: [https://github.com/elastic/elasticsearch/issues/42851](https://github.com/elastic/elasticsearch/issues/42851)  
And the versions (as labels added to the PR) where the fix is available are here: [https://github.com/elastic/elasticsearch/pull/43154](https://github.com/elastic/elasticsearch/pull/43154)

Apologies for the inconvenience, if you have time and the possibility of testing the fix on any of those versions, that would be great.

---

<div class="post-metadata">

**Author:** ![herrBez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/herrbez/32/46108_2.png) [@herrBez](https://discuss.elastic.co/u/herrBez)\
**Post date:** [September 2, 2019, 9:20am UTC](https://discuss.elastic.co/t/elasticsearch-sql-limit-returns-last-10-results-instead-of-first-10-results/197450/3 "2019-09-02T09:20:09Z")

</div>

Thank you very much for the explanation 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 30, 2019, 9:20am UTC](https://discuss.elastic.co/t/elasticsearch-sql-limit-returns-last-10-results-instead-of-first-10-results/197450/4 "2019-09-30T09:20:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
