LIMIT in SELECT statements in Firebird

The question is, is there a MySQL/PostgreSQL equivalent of LIMIT clause in Firebird? It is very useful in situations where you don’t get to keep the database cursor over multiple requests, i.e. in some web applications. In my scenario, I just want to the database to return the most relevant n rows of result, over a potential huge result set.

You used to need to write store procedures in Firebird to simulate LIMIT clause, but due to popular demand it has been included in Firebird 1.0. But Google did not yield useful results on the first page, except an relevant but somehow inaccurate information on IBPhoenix. Trial and error, and here’s what I found that was working with Firebird 1.0.3.

  SELECT FIRST x [SKIP y] ... [rest of query]

It will then return x rows of result from the query, and optionally skip first y rows. For example, returning row 21-30 from a query would be something like this in Firebird:

  SELECT FIRST 10 SKIP 20 column1, column2, column3 FROM foo

The same statement in MySQL would be:

  SELECT column1, column2, column3 FROM foo LIMIT 10, 20

Again in PostgreSQL:

  SELECT column1, column2, column3 FROM foo LIMIT 20, 10

And let’s not get into the differences between Oracle, MS SQL Server and other RDBSMS. What a wonderful world of SQL :)

By the way, while Firebird is such an excellent open source RDBMS for both the high-end transaction-safe applications and low end embedded database, its documentation is seriously lacking. I am still using the Interbase 6 Beta’s Language Reference PDF to consult SQL syntax, and there is no way I can know all the great things they have added to Firebird 1.0/1.5. All I want is the one-volume reference manual that provides everything a developer needs, in the same style of MySQL and PostgreSQL. Hopefully the documentation will pick up in speed, while the coders move towards releasing 1.5 and developing 2.0.

Category: General | Mon, 12 January 2004 11:29 am
Tags:

Links to This Article

  1. Tue, 23 December 2008 10:17 pm
    Limit Rows in Interbase/Firebird SQL statement

Comments

1.
Avatar for shankar
Posted by shankar on Sat, 14 February 2004 7:01 am

i need the syntax of IF query in Firebird and also
join queries


2.
Avatar for Dan
Posted by Dan on Tue, 18 January 2005 9:54 am

I am trying to collect resources on using Firebird in .NET at http://www.dotnetfirebird.org.


3.
Avatar for Ajith
Posted by Ajith on Mon, 7 March 2005 5:18 pm

Please help me by giving a note on how “To select a particular Row by using rowid as in Oracle”.


4.
Avatar for fatema
Posted by fatema on Wed, 11 May 2005 9:38 pm

thnaks, this info was of grate use to me


5.
Avatar for timbo
Posted by timbo on Sat, 6 May 2006 1:22 am

Ta! Just what I needed


6.
Avatar for Milen A. Radev
Posted by Milen A. Radev on Thu, 11 January 2007 11:08 pm

Actually the PostgreSQL equivalent is:

SELECT column1, column2, column3 FROM foo LIMIT 10 OFFSET 20;

as you could see from here – http://www.postgresql.org/docs/8.1/static/sql-select.html#SQL-LIMIT .


7.
Avatar for scotty
Posted by scotty on Fri, 12 January 2007 7:05 am

You are absolutely right. Guess how long I have not been using PostgreSQL…


8.
Avatar for Rodrigo
Posted by Rodrigo on Wed, 31 January 2007 5:30 am

Great! Thanks man. Works perfectly.


9.
Avatar for Andresa
Posted by Andresa on Mon, 15 October 2007 11:38 pm

Yes! It’s fine, exectaly what I needed!

Thanks!


Avatar for utku
Posted by utku on Tue, 29 January 2008 1:33 am

thanks for this post after 4 years!!! :)


Avatar for Kate
Posted by Kate on Sun, 3 February 2008 7:16 am

yes, it’s a very useful post :D


Avatar for Niels
Posted by Niels on Thu, 24 April 2008 12:25 am

Thanks! You saved my day.


Avatar for matipl
Posted by matipl on Fri, 30 May 2008 11:42 pm

In FirebirdSQL 2 also:

SELECT column1, column2, column3 FROM foo ROWS 10 TO 20


Avatar for Mariano
Posted by Mariano on Sat, 21 June 2008 4:42 am

Thanks Scott, it was indeed an useful tip.

I agree with you regarding Firebird documentation: it’s really really incomplete.
The community should realize how important is to have up2date docs: indeed is a major key to success.


Avatar for Roland
Posted by Roland on Fri, 27 June 2008 3:54 am

Thanks for the perfect hint!
By the way, if you check this on a relativly big table (100k records), using indexes for your query, the result is confusing. While the query without ROWS or FIRST-SKIP runs in milliseconds (using the indexes), the query with the ROWS or FIRST-SKIP doesn’t use the indexes and takes 1-2 seconds to retrieve the result. It’s a bit annoying. Any comments?


Add a comment

Gravatar is used. Email address is required but will not be displayed. Please keep your comment on topic. No spamming and/or bad language. First time poster will be moderated. Scott reserves the right to delete/edit your comments.