Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Nth row in Postgresql

Tags:

postgresql

In MySQL I can do SELECT * FROM tbl LIMIT 10

In MSSQL I can do SELECT TOP 5 * FROM tbl

How do I do this in Postgresql?

like image 302
Byron Whitlock Avatar asked Jan 17 '10 21:01

Byron Whitlock


People also ask

How do you find the nth row in SQL?

ROW_NUMBER (Window Function) ROW_NUMBER (Window Function) is a standard way of selecting the nth row of a table. It is supported by all the major databases like MySQL, SQL Server, Oracle, PostgreSQL, SQLite, etc.

How do I select every nth row in SQL?

Here's the SQL query to select every nth row in MySQL. mysql> select * from table_name where table_name.id mod n = 0; In the above query, we basically select every row whose id mod n value evaluates to zero.

How do I select a row in PostgreSQL?

If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names. The select list may also contain expressions or literal values. Second, specify the name of the table from which you want to query data after the FROM keyword.


2 Answers

See the LIMIT clause:

SELECT * FROM tbl LIMIT 10

or

SELECT * FROM tbl OFFSET 20

and, of course

SELECT * FROM tbl LIMIT 10 OFFSET 10
like image 62
Dirk Avatar answered Oct 12 '22 19:10

Dirk


From the PostgreSQL docs:

SELECT select_list
  FROM table_expression
  [ ORDER BY ... ]
  [ LIMIT { number | ALL } ] [ OFFSET number ]

So LIMIT should work as it does in MySQL. OFFSET is used to skip rows before starting to return data.

See docs for LIMIT and OFFSET

I hope this helps.

like image 23