22

i have a table MEN in sql server 2008 that contain 150 rows.

how i can show only the even or only the odd rows ?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
Gold
  • 60,526
  • 100
  • 215
  • 315
  • 1
    Odd or even according to what criteria? You could mean A, C, E, ... or B, D, F, etc. Or do you mean row index according to the ORDER BY clause... but then what would you order by? – gbn Jun 08 '10 at 13:07

18 Answers18

30

Check out ROW_NUMBER()

SELECT t.First, t.Last
FROM (
    SELECT *, Row_Number() OVER(ORDER BY First, Last) AS RowNumber 
            --Row_Number() starts with 1
    FROM Table1
) t
WHERE t.RowNumber % 2 = 0 --Even
--WHERE t.RowNumber % 2 = 1 --Odd
Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
14

Assuming your table has auto-numbered field "RowID" and you want to select only records where RowID is even or odd.

To show odd:

Select * from MEN where (RowID % 2) = 1

To show even:

Select * from MEN where (RowID % 2) = 0
z-boss
  • 17,111
  • 12
  • 49
  • 81
  • 9
    YOu can never assume no gaps! There wil be delted records and transactions rolled back. – HLGEM Jun 08 '10 at 13:16
  • Never say never. Sometimes you can. For example if you have a static definition table. But the question is not clear to assume anything, so you're right. – z-boss Jun 08 '10 at 13:19
  • Is this a [sargable](https://dba.stackexchange.com/a/217983/140226) filter? In other words, if your column was indexed in the table (e.g. an ID column) instead of computed by the query, would the query be able to use that index? – Elaskanator Nov 14 '19 at 22:11
5

FASTER: Bitwise instead of modulus.

select * from MEN where (id&1)=0;

Random question: Do you actually use uppercase table names? Usually uppercase is reserved for keywords. (By convention)

Juan Marco
  • 736
  • 6
  • 4
5

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
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Rahul
  • 51
  • 1
  • 1
4

For even values record :

select * from www where mod(salary,2)=0;

For odd values record:

select * from www where mod(salary,2)!=0;
Voodoo
  • 1,550
  • 1
  • 9
  • 19
Gowtham
  • 41
  • 1
3
  SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM empsal) a WHERE (row_number%2) = 1

and

      SELECT * FROM (SELECT ROW_NUMBER () OVER (ORDER BY sal DESC) row_number, sr,sal FROM   empsal) a WHERE (row_number%2) = 0
Datta
  • 819
  • 3
  • 14
  • 27
3

Try this :

odd :

select * from( 
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber', 
FROM table1
) d where (RowNumber % 2) = 1 

even :

select * from( 
SELECT col1, col2, ROW_NUMBER() OVER(ORDER BY col1 DESC) AS 'RowNumber', 
FROM table1
) d where (RowNumber % 2) = 0
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1
SELECT *
  FROM   
  ( 
     SELECT rownum rn, empno, ename
     FROM emp
  ) temp
  WHERE  MOD(temp.rn,2) = 1
Taryn
  • 242,637
  • 56
  • 362
  • 405
1
select * from Tablename 
where id%2=0
laalto
  • 150,114
  • 66
  • 286
  • 303
0

Following is for fetching even number:: Select * from MEN where Men_ID%2=0;

Following is for fetching odd number:: Select * from MEN where Men_ID%2!=0;

Here MEN is your table_name Men_ID is the column in MEN Table.

0

Try following

SELECT * FROM Worker WHERE MOD (WORKER_ID, 2) <> 0;
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
0

Here’s a simple and straightforward answer to your question, (I think). I am using the TSQL2012 sample database and I am returning only even or odd rows based on “employeeID” in the “HR.Employees” table.

USE TSQL2012;
GO

Return only Even numbers of the employeeID:

SELECT *
FROM HR.Employees
WHERE (empid % 2) = 0;
GO

Return only Odd numbers of the employeeID:

SELECT *
FROM HR.Employees
WHERE (empid % 2) = 1;
GO

Hopefully, that’s the answer you were looking for.

Aubrey Love
  • 946
  • 6
  • 12
0

To fetch even records

select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=0;

To fetch odd records

select *
from (select id,row_number() over (order by id) as r from table_name) T
where mod(r,2)=1;
Harsh Patel
  • 6,334
  • 10
  • 40
  • 73
Karthik Bs
  • 1
  • 1
  • 4
0

Oracle Database

ODD ROWS

select * from (select mod(rownum,2) as num , employees.* from employees) where num =0;

EVEN ROWS

select * from (select mod(rownum,2) as num , employees.* from employees) where num =1; 
0

To select an odd id from a table:

select * from Table_Name where id%2=1;

To select an even id from a table:

select * from Table_Name where id%2=0;
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Praveen Kumar C
  • 409
  • 4
  • 6
0

We can achieve by this Query To Find ODD Records

/*Query To Find ODD Result using CTE */
WITH EVEN_ODD_RESULT AS
 (
  select *, ROW_NUMBER() OVER (ORDER BY CountryID) AS ROWNUM
   FROM schema.Country_TBL
  )
  SELECT * FROM EVEN_ODD_RESULT
  WHERE (EVEN_ODD_RESULT.ROWNUM % 2) =1

Query To Find EVEN Records

/*Query To Find EVEN Result using CTE */

WITH EVEN_ODD_RESULT AS
(
   select *, ROW_NUMBER() OVER (ORDER BY CountryID) AS ROWNUM
   FROM schema.Country_TBL
 )
 SELECT * FROM EVEN_ODD_RESULT
 WHERE (EVEN_ODD_RESULT.ROWNUM % 2) = 0

Thank You

0

SQL SERVER

Lets say you have student table with ROLL_NO column(int), and want to find the number of even and odd columns

--SOLUTION OPTION 1-USING CASE IN NESTED COLUMN,

SELECT Rolls, Count(Rolls) as Number
FROM (
    SELECT 
    CASE ROLL_NO % 2
    WHEN 0 THEN 'EVEN'
    ELSE 'ODD'
    END Rolls
    FROM STUDENT
    ) S --TABLE NAME TO IDENTIFY THE TABLE
GROUP BY Rolls;

--SOLUTION OPTION 2-USING NESTED QUERIES

SELECT 
(SELECT COUNT(*) FROM STUDENT WHERE ROLL_NO % 2 = 0) AS No_Of_Even_Rolls,
COUNT(*) - (SELECT COUNT(*) FROM STUDENT WHERE ROLL_NO % 2 = 0) AS No_Of_Odd_Rolls
FROM STUDENT;
Prastab Dkl
  • 111
  • 1
  • 2
-2

for SQL > odd:

select * from id in(select id from employee where id%2=1)

for SQL > Even:

select * from id in(select id from employee where id%2=0).....f5
Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57