Retrieve Odd and Even Rows

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

Questions by harpreet.karan

Showing Answers 1 - 36 of 36 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

NAVEEN

  • Sep 7th, 2011
 

Code
  1. SELECT * FROM tablename WHERE MOD (rowindex, 2) = 0; FOR EVEN ROWS

  2.  

  3. SELECT * FROM tablename WHERE MOD (rowindex, 2) = 1; FOR ODD ROWS

smonroy

  • Sep 23rd, 2011
 

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

Code
  1. SELECT  id_table, (CASE WHEN ABS(id_table) % 2 = 1

  2.     THEN 'odd'

  3.     ELSE 'even'

  4.     END) AS 'RESULT'

  5. FROM table_name

  6.  

  7. Example:

  8. SELECT  id_ubication, (CASE WHEN ABS(id_ubication) % 2 = 1

  9.         THEN 'odd'

  10.         ELSE 'even'

  11.     END) AS 'RESULT'

  12. FROM ubication

  13.  

  14. RESULT:

  15.  

  16. id_ubication  RESULT

  17. 18                 even

  18. 19                 odd

  19. 20                 even

  20.  

  21.  

  Was this answer useful?  Yes

ASHOK PANGULURI

  • Feb 11th, 2012
 

For even rows:

Code
  1. SELECT * FROM emp WHERE mod(rowid,2)=0;


For odd rows:
Code
  1. SELECT * FROM emp WHERE mod(rowid,2)<>0;

  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. SELECT R1,employee_id,last_name FROM

  2. (SELECT employee_id,last_name,ROWNUM AS r1

  3. FROM employees)

  4. WHERE MOD(R1,2)=0;

  5.  

  6.  

  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. SELECT id, version, authority

  2. FROM (SELECT id, version, authority, rownum row_num FROM OBTWC_ROLE)

  3. WHERE mod(row_num,2) = 0;

  Was this answer useful?  Yes

venkat

  • Jun 27th, 2012
 

Code
  1. SELECT * FROM (SELECT rownum rn,col1,col1 FROM table_name) WHERE mod(rn,2)=0

union all
Code
  1. SELECT * FROM (SELECT rownum rn, col1,col2 FROM table_name) WHERE mod(rn,2)=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