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

what is cursor procedure

1 Answers   iGate,


How do I start pl sql?

0 Answers  


What is sql keyword?

0 Answers  


What is the use of triggers?

0 Answers  


What are user defined functions?

0 Answers  






What are sql ddl commands?

0 Answers  


Explain the purpose of %type and %rowtype data types?

0 Answers  


What is serial sql?

0 Answers  


What is primary key in db?

0 Answers  


What are field types?

0 Answers  


How to create a view on a table which does not exists

4 Answers   Oracle, TCS,


Explian rowid, rownum?

0 Answers  


Categories