postgresql,

How to limit number of rows in PostgreSQL

Feb 15, 2022 · 1 min read · Post a comment

Limiting SQL queries is most likely a useful feature especially if you want to get a sample data chunk from a big table. The LIMIT clause in PostgreSQL is pretty much the same as the MySQL equivalent.

Prerequisites

  • PostgreSQL

Solution

SELECT some_columns
FROM table_name
ORDER BY some_column
LIMIT number_of_rows
OFFSET skip_first_n_rows;

Note(s):

  • The OFFSET clause serves to skip the first N rows. If you don’t specify it though, default value is NULL and there will be no row skips.
  • Using ORDER BY is a good approach if you want the result rows to be returned in a consistent unique order.

Conclusion

Check out the official PostgreSQL documentation for more info. On a side note, follow our official channel on Telegram.