Get alternate record from table
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 9 )
— To fetch even records
select * from tableA WHERE ID % 2 == 0
— To fetch odd number of records
select * from tableA WHERE ID % 2 != 0
Awesome
Select *
from (
Select * , row_number() OVER(order by empid desc ) As RowNumber from Employee) t
Where t.RowNumber % 2 == 0
Note: In case of odd records do
RowNumber % 2 == 1
*Assuming id is unique and auto-increment column*
Select id from table
where mod(id, 2) 0; –to fetch odd rows
Select id from table
where mod(id, 2) = 0; –to fetch even rows
1. To fetch Even records: select * from Table_Name WHERE ID % 2 == 0
2. To fetch Odd records: select * from Table_Name WHERE ID % 2 != 0
SELECT *
FROM (
SELECT *, Row_Number() OVER(ORDER BY id) AS RowNumber
FROM table
) t
WHERE t.RowNumber % 2 = 0
with cte as (select *,row_number() over ( order by empld desc) as r_num from employee_table)
select * from cte where r_num %2=0;
select * from cte where r_num%2!=0;
Assuming Table consists of ID/Sl.no which is AUTOINCREMENT and do not contain any NULL values
Select *from Table
where ID%2 ==0;
1. Assuming the table is ordered in increasing order of ID
SELECT * FROM table WHERE id%2=0
2. Assuming table is not ordered and i can order in increasing order of ID
WITH sub AS
(SELECT *,ROW_NUMBER() OVER (ORDER BY id ASC) AS r_nbr
FROM table)
SELECT * FROM sub WHERE r_nbr%2=0