---
title: "Making OFFSET LIMIT queries in AWS Athena"
description: "How to use OFFSET in AWS Athena queries"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/making-offset-limit-queries-in-aws-athena
---

There is no OFFSET support in AWS Athena, but we can use a workaround to get the same behavior.

Let's assume that I want to execute this query:

```sql
SELECT * FROM some_table
ORDER BY column_A
OFFSET 20 LIMIT 10 -- this doesn't work
```

to get the desired outcome, I need three things:

* a window function that assigns a number to every row
* ordering method that sorts the table, so I get a deterministic outcome
* filtering function that selects only the rows I want

```sql
SELECT * FROM (
    SELECT row_number() over(ORDER BY column_A) AS rn, * FROM some_table)
WHERE rn BETWEEN 20 AND 30; -- 30 = 20 (offset) + 10 (limit)
```

