How One can easily select all even, odd, or Nth rows from a
table using SQL queries?

Answers were Sorted based on User's Feedback



How One can easily select all even, odd, or Nth rows from a table using SQL queries?..

Answer / guru

Please Use Below Query and Kindly post reply feedback

Odd number of records:

select * from emp where (rowid,1) in (select rowid,
mod(rownum,2) from emp);
Output:-
1
3
5
Even number of records:
select * from emp where (rowid,0) in (select rowid,
mod(rownum,2) from emp)
Output:-
2
4
6

For nth number,
Example we r considering number n=10
select * from emp where (rowid,0) in (select rowid,
mod(rownum,10) from emp)

Is This Answer Correct ?    18 Yes 8 No

How One can easily select all even, odd, or Nth rows from a table using SQL queries?..

Answer / mritunjay

hi..mr guru all are correct except for the Ntn row
Odd number of records:
select * from emp where (rowid,1) in (select rowid,
mod(rownum,2) from emp);
Output:-
1
3
5
Even number of records:
select * from emp where (rowid,0) in (select rowid,
mod(rownum,2) from emp)
Output:-
2
4
6
For nth number,
Example we r considering number n=10
select * from emp where (rowid,1) in (select rowid,
rownum/:N) from emp)

Is This Answer Correct ?    12 Yes 8 No

How One can easily select all even, odd, or Nth rows from a table using SQL queries?..

Answer / mahesh

SIMPLE ODD AND EVEN...

SQL> SELECT ROWNUM FROM EMP GROUP BY ROWNUM HAVING MOD(ROWNUM,2)=1
2 /

ROWNUM
----------
1
3
5


1* SELECT ROWNUM FROM EMP GROUP BY ROWNUM HAVING MOD(ROWNUM,2)=0
2 /

ROWNUM
----------
2
4
Goahead...

Is This Answer Correct ?    0 Yes 0 No

How One can easily select all even, odd, or Nth rows from a table using SQL queries?..

Answer / guru

SELECT *
FROM emp
WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,4)
FROM emp);

Is This Answer Correct ?    6 Yes 17 No

Post New Answer

More SQL PLSQL Interview Questions

Explain the rollback statement?

0 Answers  


Is join an inner join?

0 Answers  


Write one update command to update seqno field of a table on the basis of row number.

4 Answers   HCL,


Use The Implicit cursor to Query The Department table information Where Deptno is 30.check,if no record was found then print "Record Was Not Found".Else Print Deptname And Ename.Dept table Have Dname Varchar2(20),Deptno Number,EnameVarchar2(20).Please Answer In 2 mins,with in Maximum 15 lines.

5 Answers   Wipro,


If we declare constraints Unique and Not null on a single column then it will act as a Primary key, so what is the use of primary key??

3 Answers  






what is definer rights invoke rights?

1 Answers  


How to run sql statements through the web interface?

0 Answers  


What does bitemporal mean?

0 Answers  


How does sql store data?

0 Answers  


can i give user defined exception in a package

2 Answers  


What is delete command in sql?

0 Answers  


What is the importance of sqlcode and sqlerrm?

0 Answers  


Categories