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:
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
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)
Want to build AI systems that actually work?
Download my expert-crafted GenAI Transformation Guide for Data Teams and discover how to properly measure AI performance, set up guardrails, and continuously improve your AI solutions like the pros.