INTERVIEW QUESTIONS
ORACLE
SQL IN ORACLE
DETAILS
Question: Display Odd/ Even number of records
Answer: 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
|
Question:
Display Odd/ Even number of records
Answer:
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 Source: CoolInterview.com
FOR ODD NUMBER OF ROWS
SELECT*FROM emp WHERE rowid IN(SELECT DECODE(MOD(rowid,2),1,rowid,NULL)FROM emp);
FOR EVEN NUMBER OF ROWS SELECT*FROM emp WHERE rowid IN(SELECT DECODE(MOD(rowid,2),0,rowid,NULL) FROM emp); Source: CoolInterview.com
Answered by: Samir Kumar Sahoo | Date: 1/23/2009
| Contact Samir Kumar Sahoo
for even number of records
select*from emp where rowid in(select decode(mod(rownum,2),0,rowid) from emp);
for odd number of records
select*from emp where rowid in(select decode(mod(rownum,2),1,rowid) from emp); Source: CoolInterview.com
Answered by: Samir Kumar Sahoo | Date: 4/17/2009
| Contact Samir Kumar Sahoo
ODDNUMBER: SELECT ENAME,SAL,DEPTNO,NUM FROM (SELECT EMPNO,ENAME,SAL,DEPTNO, RANK() OVER(ORDER BY SAL DESC)NUM FROM EMP) WHERE MOD(NUM,2)=1 Source: CoolInterview.com
Answered by: K.SARATH | Date: 6/10/2010
| Contact K.SARATH
-- odd numbers SELECT * FROM (SELECT rownum poradi, employee_id FROM employees) WHERE mod(poradi,2) = 0
-- even numbers .... WHERE mod(poradi,2) = 1 Source: CoolInterview.com
Answered by: Lenka | Date: 6/24/2010
| Contact Lenka
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|