Write a query to display alternate records from the employee table?

Questions by kowmudiswarna   answers by kowmudiswarna

Showing Answers 1 - 19 of 19 Answers

manish dudhe

  • Oct 15th, 2007
 

--odd number query

SELECT *
      FROM   ( SELECT rownum rn, empno, ename
               FROM emp
             ) temp
      WHERE  MOD(temp.rn,2) = 1

--even number query


SELECT *
      FROM   ( SELECT rownum rn, empno, ename
               FROM emp
             ) temp
      WHERE  MOD(temp.rn,3) = 0

  Was this answer useful?  Yes

sgbang

  • Jul 8th, 2008
 

Hi Guys,
 
You can use this query also

select * from employees where employee_id in (
select decode(mod(rownum,2),x,employee_id,null) emp_id from employees)
/

where x=0 for even records
where x=1 for odd   records

Regards,

Snehasish

  Was this answer useful?  Yes

ammupriyaa

  • Jul 8th, 2008
 

To Display even row numbers
---------------------------
select * from emp where (rowid,0) in (select rowid,mod(rownum,2) from emp ) 


To Display odd row numbers
---------------------------
select * from emp where (rowid,1) in (select rowid,mod(rownum,2) from emp )

  Was this answer useful?  Yes

vinodsahani

  • May 18th, 2010
 

SELECT * FROM (SELECT ROWNUM RN, FIRST_NAME FROM EMPLOYEES) TEMP WHERE MOD (TEMP.RN, 2)=1;


CHANGE LAST NUMBER 1 TO 0 FOR EVEN NUMBERED ROWS.

Basically, in this, I used a subquary and selected all rows where RN was holding ROWNUM values, then outer SELECT was used to check with MOD function.

  Was this answer useful?  Yes

pg_143

  • Oct 24th, 2010
 

select ename,job from(select mod(rownum,2) as odd,ename,job from emp) where odd=1;
                   odd rows are retrive
     for even just replace as odd=0;
                    keep smiling(pavankumar_cherukuri@yahoo.com)

  Was this answer useful?  Yes

  1* select * from (select employee_id,last_name,salary,rownum rn from employees ) where mod(rn,2) = 0
SQL> /

EMPLOYEE_ID LAST_NAME                     SALARY         RN
----------- ------------------------- ---------- ----------
        101 Kochhar                        17000          2
        103 Hunold                          9000          4
        107 Lorentz                         4200          6
        141 Rajs                            3500          8
        143 Matos                           2600         10
        149 Zlotkey                        10500         12
        176 Taylor                          8600         14
        200 Whalen                          4400         16
        202 Fay                             6000         18
        206 Gietz                           8300         20

10 rows selected.

  Was this answer useful?  Yes

naveen reddy

  • Aug 17th, 2011
 

for even rows:-

select ename,empno, job, rownum from emp group by ename ,rownum ,empno,job having mod(rownum,2)=0


for odd rows please replace '0' with 1 then u get odd rows:-

  Was this answer useful?  Yes

sukanta

  • May 26th, 2012
 

select r, ename from(select rownum r,ename from emp) where mod(r,2)=1;

rohitosu

  • May 29th, 2012
 

@manish dudhe

Your answer for even number query is wrong. The correct query is
SELECT temp.r, employee_id, first_name, last_name
FROM (SELECT rownum as r, employee_id, first_name, last_name FROM employees) temp
WHERE MOD(r,2) = 0;

  Was this answer useful?  Yes

Nazeera JAffar

  • Sep 30th, 2012
 

Answer

Code
  1.  

  Was this answer useful?  Yes

Gaurav gg

  • Aug 20th, 2014
 

SELECT * FROM tab_name WHERE mod(primary_key_col,2)=0; -- for even records

SELECT * FROM employees WHERE mod(employee_id,2)=0; -- for even records

SELECT * FROM employees WHERE mod(employee_id,2)=1; -- for odd records

  Was this answer useful?  Yes

mradul

  • Dec 22nd, 2014
 

I use this for even records !

  Was this answer useful?  Yes

AVINASH ARUN

  • Jul 27th, 2015
 

How to display alternate records if ID datatype is varchar

  Was this answer useful?  Yes

Anand Prakash Joshi

  • Aug 10th, 2015
 

Code
  1.  

  Was this answer useful?  Yes

Bharathiarun

  • Aug 20th, 2015
 

Even if the Id is varchar, the rows are ordered by rownum.

  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