SQL only return range of records

Posted on 2008-10-23
Last Modified: 2013-11-25
Is there sql to only return back a range of records i.e from 20-30 in access.  or are there any easy work arrounds to this problem


Question by:david_88
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 46

Expert Comment

ID: 22784452
Hi david_88,

Something like
SELECT * FROM Table1 WHERE ((Table1.ID) Between 20 And 30);

Good Luck!


Accepted Solution

cr4ck3rj4ck earned 500 total points
ID: 22784477
In MySQL you can use the LIMIT function but in MSACCESS you have to use a sub query to create an offset.

select top X * from myTable where id not in (select top Y * from myTable)

X would be replaced by the amount of records you wanted to return.
Y would be replaced by the position within the data set at which you wanted to start.
id would be your unique identifier within the table.

For example:
select top 10 * from myTable where id not in (select top 19 * from myTable)

That would return records 20-30 in a recordset.

Expert Comment

ID: 22784598

this is example of SQL paging. You can simply modify it as you need:

top_onPage = "TOP " + Convert.ToString((onPage * page > totalCount) ? (totalCount % onPage) : (onPage));
top_page = "TOP " + Convert.ToString(onPage * page);

      SELECT TOP " + top_onPage + " id  FROM (
            SELECT DISTINCT " + top_page + "
                  FROM dbo.tblTest T
            )      as A ORDER BY X ASC
)      as B ORDER BY X DESC

Author Closing Comment

ID: 31509138
perect  thanks!

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question