# How to set the LIMIT with FROM and TO in ES Sql query?

**URL:** <https://discuss.elastic.co/t/how-to-set-the-limit-with-from-and-to-in-es-sql-query/275559>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [June 10, 2021, 11:47am UTC](https://discuss.elastic.co/t/how-to-set-the-limit-with-from-and-to-in-es-sql-query/275559 "2021-06-10T11:47:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Resington\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/resington_r/32/85143_2.png) [@Resington\_R](https://discuss.elastic.co/u/Resington_R)\
**Post date:** [June 10, 2021, 11:47am UTC](https://discuss.elastic.co/t/how-to-set-the-limit-with-from-and-to-in-es-sql-query/275559/1 "2021-06-10T11:47:59Z")

</div>

Hi... Can anyone please tell me how to set the Limit with FROM and TO. The below query returns more than 10 thousand records, where I would like to Paginate 50 records in a page. Is there any possibility to set limit with range or is there any other option to do this. Thanks in advance..

```auto
async function mysqlfunc(req, res) {  
    client.transport.request({
        method: "POST",
        path: "/_sql",
        body: {
            query: "SELECT field1, filed2 FROM myindex limit 10",
            fetch_size: 50
        }
    }, function(error, response) {
        if (error) {
            console.error('Something does not compute : ' + error);
        } else {           
            console.log("Output : " + response.rows);
            res.send(response.rows);
        }
    });
}

```

---

<div class="post-metadata">

**Author:** ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)\
**Post date:** [June 14, 2021, 1:58pm UTC](https://discuss.elastic.co/t/how-to-set-the-limit-with-from-and-to-in-es-sql-query/275559/2 "2021-06-14T13:58:33Z")

</div>

Then you keep the `fetch_size` as you have and get rid of the `LIMIT`, so the 1st response apart from the rows will return to you a cursor ID which you can use to scroll through the next pages:

> **[Paginating through a large response | Elasticsearch Guide \[7.13\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-pagination.html)**

So in the next pages you don't provide a `query:` anymore in the body but a `cursor`, and don't forget to clear the cursor once you're done to free up the resources:

> **[API Reference | Elasticsearch Node.js client \[7.x\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_sql_clearcursor)**

---

<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:** [July 12, 2021, 1:59pm UTC](https://discuss.elastic.co/t/how-to-set-the-limit-with-from-and-to-in-es-sql-query/275559/3 "2021-07-12T13:59:12Z")

</div>

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