Wednesday, September 10, 2014

Fetch the 'nth' element or row from redshift table

Purpose:

A simple query to fetch the nth row in a database table.



        SELECT * 
            FROM tablename 
               LIMIT 1
                 OFFSET n-1;



OFFSET plays a vital role in this query. This query not only works in redshift. All DBs which support postgres queries support OFFSET.

If you need the 21st row data, then the OFFSET will have value as 20. Since OFFSET will omit or skip the first 20 rows and start fetching with the 21st row.

No comments:

Post a Comment