What is this Site!!

All about Real time requirements in Orale Stay Tune!!

Wednesday, November 7, 2007

Small Select Statement

If I execute a query which returns 100 records but I want the record from 20 to 30. How is it possible to filter the specific results as the row number can only return top records but what will I have to do if I want results from 30 to 40 or etc and want to ignore some of the top records.

You first select 30 rows in a query using rownum<30 after that make another query for rownum<20
and now subtract second query from first query.

Example:
select * from emp where rownum<30
minus
select * from emp where rownum<20;>


How to select the alternative rows in the table?


select * from emp where rownum <10;>

*so the result will appear less then 10 record


You cannot rely on rownum and what do you mean by alternate rows, as oracle doesn't store the records in any predifined sequence.


This is not right. You can use the following

select rownum,sku from win_store where mod(rownum,2) = 0


In Oracle, you can use the query as:
Select rownum, * From where mod (rownum,2) <> 0

In SQL Server, you can use the query as:
Select id, * from where (id % 2 <> 0 ).
Here id is the identity Column.


This will always return 0 rows.


rownum won't work with any operator, other than <(less than).

So the query can be :

select b.* from (select rownum cnt, a.* from emp a) b where mod(cnt,2)<>0;