If you're on the Mainframe (v9), then you want this page for more info (or version 10). Using the FETCH FIRST syntax or LIMIT syntax (for newer DB2 versions) improves the performance of queries when not all results are required. In your case you may be using DB2 older version (<7). But avoid …. DB2 Version 7 provides an easy way to limit the results of a SELECT statement using a new clause – the FETCH FIRST n ROWS clause. This Version 7 approach requires SQL only and is quite simple and efficient. Search for fetch-first-clause on the page linked for more info. This can have performance benefits, especially in distributed applications. – Christian Maslen Sep 27 '10 at 22:34 Your query should return the 10 rows with the highest KEY, but that does not make them the "last 10". Which will not support for FIRST N ROWS ONLY Option. Thanks for contributing an answer to Stack Overflow! For example, use MySQL SQL syntax SELECT column FROM table LIMIT 10 Optimizing for integer rows can improve performance. When the FETCH FIRST n ROWS clause is specified, DB2 will limit the number of rows that are fetched and returned by a SELECT statement. I think (not 100% on this) the reason you can't with the above is the fetch first x rows only syntax is more of an optimization instruction than say a syntax feature for paging. When you use FETCH statements to retrieve data from a result table, the fetch clause causes DB2 to retrieve only the number of rows that you need. Asking for help, clarification, or … All versions of DB2 provide a mechanism for limiting the records returned from an SQL query with the FETCH FIRST clause. Note that if you use WITH TIES, you must specify an ORDER BY clause in the query. This is for DB2 on Linux/Unix/Windows. So first check which DB2 version u r working in. The INSERT statement, in addition to the FOR n ROWS clause, inserts multiple rows into a … The optimize-clause tells DB2® to assume that the program does not intend to retrieve more than integer rows from the result table. This rowset size minimizes the impact to the network when retrieving a large rowset with a single fetch operation. Please understand that the "solution" you posted does not "get the last 10" in the table - as mentioned before, there is no last 10 (nor is there a first 10). If … Add FETCH FIRST ROW ONLY to your subquery. . It's instructing DB2 to not perform the usual aggressive prefetch reads thus saving some disk access. For DB2 it is SELECT column FROM table FETCH FIRST 10 ROWS ONLY; If working with DB2 v9.7.2 use SQL syntax of the database you are familiar with. Without this clause, DB2 assumes that all rows of the result table will be retrieved, unless the FETCH FIRST clause is specified. The WITH TIES returns additional rows with the same sort key as the last row fetched. The ONLY returns exactly the number of rows or percentage of rows after FETCH NEXT (or FIRST). Databases to not have a front and a back row or a beginning and an end row. so this way sql query get slower for larger record. Use FETCH FIRST n ROWS ONLY clause to limit the number of rows in the result table to n rows. SELECT * FROM USER_TAB FETCH FIRST 10 ROWS ONLY; Will fetch first 10 rows from the table USER_TAB. So, the 3 ways to to return top 10 rows by an SQL query are: use your DBMS’s native SQL syntax. Please be sure to answer the question.Provide details and share your research! FETCH FIRST n ROWS ONLY has the following benefits: . Limit is first answer but problem with Limit is, if you have 100,000 record and you want to fetch 10 with limit, it will still take whole time to fetch 100,000 record then will apply limit. Rowset-positioned cursors also allow multiple-row inserts. . thats why we are looking for alternate of limit. Newer versions of DB2 also support the LIMIT and LIMIT OFFSET clauses.