GeekInterview.com
Series: Subject: Topic:
Question: 52 of 190

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

Asked by: kowmudiswarna | Member Since Sep-2007 | Asked on: Sep 14th, 2007

View all questions by kowmudiswarna   View all answers by kowmudiswarna

Showing Answers 1 - 14 of 14 Answers

SELECT * FROM emp WHERE (rowid,1) IN (SELECT rowid,mod(rownum,2) FROM emp);

Yes  1 User has rated as useful.
  
Login to rate this answer.
manish dudhe

Answered On : 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

  
Login to rate this answer.
mohdfarezpink

Answered On : Jan 14th, 2008

View all answers by mohdfarezpink

select count(*), rownum
from emps
group by rownum
having mod(rowmum,2) = 1

output:-

1      1
1      3
1      5
1      7
1      8
.
.
.
.
.

Yes  1 User has rated as useful.
  
Login to rate this answer.
M.REHMAN

Answered On : May 25th, 2008

View all answers by M.REHMAN

select * from employeeswhere employee_id in (105,160,148,165,175,206);

  
Login to rate this answer.
sgbang

Answered On : Jul 8th, 2008

View all answers by sgbang

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

  
Login to rate this answer.
ammupriyaa

Answered On : Jul 8th, 2008

View all answers by ammupriyaa

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 )

  
Login to rate this answer.
vinodsahani

Answered On : May 18th, 2010

View all answers by vinodsahani

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.

  
Login to rate this answer.
pg_143

Answered On : Oct 24th, 2010

View all answers by pg_143

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)

  
Login to rate this answer.

  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.

  
Login to rate this answer.
naveen reddy

Answered On : 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:-

  
Login to rate this answer.
sukanta

Answered On : May 26th, 2012

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

Yes  1 User has rated as useful.
  
Login to rate this answer.
rohitosu

Answered On : May 29th, 2012

View all answers by rohitosu

@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;

  
Login to rate this answer.
Bhaskara Vamsi

Answered On : Aug 15th, 2012

Code
  1. SELECT * FROM emp WHERE ROWID IN (SELECT DECODE(MOD(ROWNUM,2),0,ROWID, NULL) FROM emp);

  
Login to rate this answer.
Nazeera JAffar

Answered On : Sep 30th, 2012

Answer

Code
  1. SELECT rownum
  2. FROM TABLE
  3. GROUP BY rownum
  4. HAVING mod(rownum,2)=0

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.