Fetch Alternate Rows

How to fetch alternate rows from the table?

Questions by priaa_7

Showing Answers 1 - 15 of 15 Answers


Code
  1. --To display the the EVEN records

  2.  

  3. SELECT column_name FROM(SELECT rownum rowno, column_name FROM table_name) WHERE mod(rowno,2)=0

  4.  

  5. --To display the the ODD records

  6.  

  7. SELECT column_name FROM(SELECT rownum rowno, column_name FROM table_name) WHERE mod(rowno,2)=1

  Was this answer useful?  Yes

karthikeyan

  • Sep 22nd, 2011
 

--Use this Script in SqlServer

Select * from (Select *,Row_number()over(Order by Sno) as Row From TableName)A
Where (Row%2)<>0

  Was this answer useful?  Yes

Millar

  • Jul 26th, 2012
 

Code
  1. SELECT * FROM table_name WHERE mod(primary_key_column,2)=0 --to find even rows in a table

  2.  

  3. SELECT * FROM employees WHERE mod(employee_id,2)=0 --to find even rows in a table

  4.  

  5. SELECT * FROM employees WHERE mod(employee_id,2)=1 --to find odd rows in a table

  Was this answer useful?  Yes

Ram

  • Feb 6th, 2013
 

Code
  1. SELECT * FROM EMP

  2. WHERE (ROWID,1) IN (SELECT ROWID,MOD(ROWNUM,2) FROM EMP);

  3.  

  4. OR

  5.  

  6. SELECT * FROM (SELECT EMP.*,ROWNUM K FROM EMP)

  7. WHERE MOD(k,2)=0;

  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