Retrieve Odd and Even Rows

How to retrieve odd and even number of rows from a table?

Questions by harpreet.karan

Showing Answers 1 - 12 of 12 Answers

usmanahmad

  • Feb 23rd, 2009
 

Hi there Thanks for your question, if I am not wrong you are asking for odd or even rows based on rownum? Am I right?

If yes then your answer is below

if you are looking for even number rows (based on rownom) then you can use following query.
SELECT usernameFROM (SELECT ROWNUM num, usernameFROM dba_users)

WHERE MOD (num, 2) = 0;

if you are looking odd number rows (based on Rownum) then use following query

SELECT usernameFROM (SELECT ROWNUM num, usernameFROM dba_users)WHERE MOD (num, 2) = 1;

Plase let me know if you have any issue(s).
Thank you. 
Usman


 

  Was this answer useful?  Yes

select * from (select rownum as rwid, a.* from (SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno from emp) a) where mod(rwid,2) = 1

This query will be helpful to find out the odd records for even records just change to mod(rwid,2) = 0

Thanks

Aditya

  Was this answer useful?  Yes

smonroy

  • Sep 23rd, 2011
 

Hi, I think that this will be the best way to do.

Code
  1. span style="color: #FF0000;">'odd''even''RESULT''odd''even''RESULT'

  Was this answer useful?  Yes

ASHOK PANGULURI

  • Feb 11th, 2012
 

For even rows:

Code
  1.  


For odd rows:
Code
  1.  

  Was this answer useful?  Yes

Siva Kumar S

  • Mar 2nd, 2012
 

It is a very simple SQL query, follow the code given below

Code
  1.  

  Was this answer useful?  Yes

sampra

  • Mar 6th, 2012
 

For Even: select * from student where(columnname,0) in (select columnname,mod(rownum,2) from student)
For Odd: select * from student where(columnname,1) in (select columnname,mod(rownum,2) from student)

  Was this answer useful?  Yes

Anup

  • May 23rd, 2012
 

Code
  1.  

  Was this answer useful?  Yes

venkat

  • Jun 27th, 2012
 

Code
  1.  

union all
Code
  1.  

  Was this answer useful?  Yes

Sabbir

  • Aug 17th, 2015
 

What programming language is this? Can you please provide this code in PHP ?

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions