473,385 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Selecting specific rows using select command

Hi

I would like to write a SQL select statement that would fetch rows
numbered 50 to 100.

Let me clarify, if i say "fetch first 10 rows only", it returns the
first 10 rows from the resultset. how do I get a specific subset of
rows from the result set if I give a start and end value. For example,
retreiving rows 50 to 100 from the resultset.

Cheers
Mahesh

Jan 22 '07 #1
5 22722
maybe something like this will work for you

db2 select * from employee fetch first 20 rows only

EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE
JOB
EDLEVEL SEX BIRTHDATE SALARY BONUS COMM
------ ------------ ------- --------------- -------- ------- ----------
--------
------- --- ---------- ----------- ----------- -----------
000010 CHRISTINE I HAAS A00 3978 1965-01-01
PRES
18 F 1933-08-24 52750.00 1000.00 4220.00
000020 MICHAEL L THOMPSON B01 3476 1973-10-10
MANAGER
18 M 1948-02-02 41250.00 800.00 3300.00
000030 SALLY A KWAN C01 4738 1975-04-05
MANAGER
20 F 1941-05-11 38250.00 800.00 3060.00
000050 JOHN B GEYER E01 6789 1949-08-17
MANAGER
16 M 1925-09-15 40175.00 800.00 3214.00
000060 IRVING F STERN D11 6423 1973-09-14
MANAGER
16 M 1945-07-07 32250.00 500.00 2580.00
000070 EVA D PULASKI D21 7831 1980-09-30
MANAGER
16 F 1953-05-26 36170.00 700.00 2893.00
000090 EILEEN W HENDERSON E11 5498 1970-08-15
MANAGER
16 F 1941-05-15 29750.00 600.00 2380.00
000100 THEODORE Q SPENSER E21 0972 1980-06-19
MANAGER
14 M 1956-12-18 26150.00 500.00 2092.00
000110 VINCENZO G LUCCHESSI A00 3490 1958-05-16
SALESREP
19 M 1929-11-05 46500.00 900.00 3720.00
000120 SEAN O'CONNELL A00 2167 1963-12-05
CLERK
14 M 1942-10-18 29250.00 600.00 2340.00
000130 DOLORES M QUINTANA C01 4578 1971-07-28
ANALYST
16 F 1925-09-15 23800.00 500.00 1904.00
000140 HEATHER A NICHOLLS C01 1793 1976-12-15
ANALYST
18 F 1946-01-19 28420.00 600.00 2274.00
000150 BRUCE ADAMSON D11 4510 1972-02-12
DESIGNER
16 M 1947-05-17 25280.00 500.00 2022.00
000160 ELIZABETH R PIANKA D11 3782 1977-10-11
DESIGNER
17 F 1955-04-12 22250.00 400.00 1780.00
000170 MASATOSHI J YOSHIMURA D11 2890 1978-09-15
DESIGNER
16 M 1951-01-05 24680.00 500.00 1974.00
000180 MARILYN S SCOUTTEN D11 1682 1973-07-07
DESIGNER
17 F 1949-02-21 21340.00 500.00 1707.00
000190 JAMES H WALKER D11 2986 1974-07-26
DESIGNER
16 M 1952-06-25 20450.00 400.00 1636.00
000200 DAVID BROWN D11 4501 1966-03-03
DESIGNER
16 M 1941-05-29 27740.00 600.00 2217.00
000210 WILLIAM T JONES D11 0942 1979-04-11
DESIGNER
17 M 1953-02-23 18270.00 400.00 1462.00
000220 JENNIFER K LUTZ D11 0672 1968-08-29
DESIGNER
18 F 1948-03-19 29840.00 600.00 2387.00

20 record(s) selected.

now fetch row 15 to 20 .....

db2 select * from (select t.*,rownumber() over () as rn from employee
t) as
x where rn between 15 and 20

EMPNO FIRSTNME MIDINIT LASTNAME WORKDEPT PHONENO HIREDATE
JOB
EDLEVEL SEX BIRTHDATE SALARY BONUS COMM RN

------ ------------ ------- --------------- -------- ------- ----------
--------
------- --- ---------- ----------- ----------- -----------
--------------------

000170 MASATOSHI J YOSHIMURA D11 2890 1978-09-15
DESIGNER
16 M 1951-01-05 24680.00 500.00 1974.00
15

000180 MARILYN S SCOUTTEN D11 1682 1973-07-07
DESIGNER
17 F 1949-02-21 21340.00 500.00 1707.00
16

000190 JAMES H WALKER D11 2986 1974-07-26
DESIGNER
16 M 1952-06-25 20450.00 400.00 1636.00
17

000200 DAVID BROWN D11 4501 1966-03-03
DESIGNER
16 M 1941-05-29 27740.00 600.00 2217.00
18

000210 WILLIAM T JONES D11 0942 1979-04-11
DESIGNER
17 M 1953-02-23 18270.00 400.00 1462.00
19

000220 JENNIFER K LUTZ D11 0672 1968-08-29
DESIGNER
18 F 1948-03-19 29840.00 600.00 2387.00
20

/Roger

Jan 22 '07 #2
A good solution using rownum has already been posted, but I wonder if
this is necessary. Maybe with a good 'order by' clause your last 50 of
100 records can become the first 50 of I-don't-care-how-many records
and you can just use 'fetch first 50 rows' again.

Jan 22 '07 #3
Well, the problem is, this table has say 1000 records or more.

When displaying the results on a web page, we are only displaying 25
entries per page and hence the result would be split into 1000/25
entries and there are options allowing the user to navigate to x*25 set
of results where x could be an entry number.

So, how do I randomnly jump to any subset of the results? Isnt there
any option like

select * from table fetch rows 50 to 100 only

or something similar.

Thanks
Mahesh

Jan 22 '07 #4

Mahesh S wrote:
Well, the problem is, this table has say 1000 records or more.

When displaying the results on a web page, we are only displaying 25
entries per page and hence the result would be split into 1000/25
entries and there are options allowing the user to navigate to x*25 set
of results where x could be an entry number.

So, how do I randomnly jump to any subset of the results? Isnt there
any option like

select * from table fetch rows 50 to 100 only

or something similar.

Thanks
Mahesh
Probably the best way to do this, if you have an orderable column, is
to use an order by with fetch first x rows. So, you could select:

select * from table order by orderable_column fetch first 25 rows only

the keep the value of the orderable_column for the last row returned,
and when you want the next 25, issue:

select * from table where orderable_column 'keptvalue' order by
orderable_column fetch first 25 rows only

and so on. If you don't have a column that you want to order by (or
there isn't an appropriate column that works for some reason), then
using row_number is the next best option:

select {column list} from (select {column list}, row_number() over() as
row_num from table) x where row_num between 26 and 50

Note that {column list} needs to be a nice list of columns. Also, you
can put an order by clause in the over() clause if you want an ordering
to the results like over(order by col_a desc).

This (row_number()) works just fine, and is probably what you are
asking for - it just isn't quite as efficient as the other option.

-Chris

Jan 22 '07 #5
In article <11**********************@51g2000cwl.googlegroups. com>,
ma********@gmail.com says...
Well, the problem is, this table has say 1000 records or more.

When displaying the results on a web page, we are only displaying 25
entries per page and hence the result would be split into 1000/25
entries and there are options allowing the user to navigate to x*25 set
of results where x could be an entry number.

So, how do I randomnly jump to any subset of the results? Isnt there
any option like

select * from table fetch rows 50 to 100 only

or something similar.

Thanks
Mahesh

You can use the row_number function for that. You can find a couple of
usefull examples in the SQL Cookbook which you can download from
http://mysite.verizon.net/Graeme_Birchall/id1.html
Jan 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: rg | last post by:
Hello, I use invisionboard on a bid web site (20 000 visitors each day) But my server has high load average. Could you help me to optimize these select command. 1) SELECT p.*,...
2
by: Kevin | last post by:
Hi All, I am new to MS SQL Server.I am using MS SQL 2000.I have a problem in creating a table by using Select command.I have table called "test" and i want to create another table with the same...
3
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a...
1
by: Kevin | last post by:
Hi All I have two questions, if someone can help me or point me in the right direction. 1. I have an OleDbDataadapter, OledbConnection and DataSet. Now the select command for my DataAdapter...
1
by: Jay | last post by:
Hi All, My users are complaining about the page refreshing when they are selecting multiple rows in a datagrid. Has anyone tried to manage this using javascript? I tried smartnavigation but that...
5
by: Gene | last post by:
What can I do if I want to get the result using the sql command? for example, the select command is "select Name from Employee where StaffID=10" How to get the "Name"??? dim Name as string and...
2
by: mokazawa1 | last post by:
Hi, I'm using stored procedures in DB2 UDB 8.1.2. In this stored, I execute a select for update command, opening a cursor. Then I update the rows using fetch and current of. The problem is that...
1
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am...
0
by: bplthebest | last post by:
Hi I have have functionality like Selecting multiple rows (without using check boxes) in Grid view by Ctrl+click (IE) and shift+click(safari and Firefox). And edit the selected rows in a panel...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.