Wednesday, July 7, 2010

TOP n query in PostgreSQL


SQL Server provides TOP clause to return top n row from query,

for ex : SELECT TOP 5 * FROM Tbl_Name order by col_name

in PostgreSql we have LIMIT clause limit the number of rows returned,

for ex : SELECT * FROM Tbl_Name ORDER BY col_name LIMIT 5

PostgreSql and MySql also provides cool feature with LIMIT clause called OFFSET

for ex : SELECT * FROM Tbl_Name ORDER BY col_name LIMIT 5 OFFSET 20

it will skip the row from 1 to 19 and return the record from 20th to 25th record.

No comments: