SQL Interview Questions | Alternate Rows


You have data of people having applied for a lottery ticket. The data consists of their name and ticket number. You have to choose winners by selecting the people present in the alternate rows (first winner starting from row number 3). Write a query to make things easy to select the winners.

(Hint- Choose alternate rows, beginning from row number 3)

in progress 0
Dhruv2301 4 days 4 Answers 131 views Contributor 0

Answers ( 4 )

  1. SELECT *
    FROM (
    SELECT *, Row_Number() OVER(ORDER BY ticketno) AS RowNumber
    FROM Lottery
    ) t
    WHERE t.RowNumber % 2 = 1
    OFFSET 2 rows

  2. select * from
    (select name,ticket number,
    row_number() over(order by ticket number))
    where ticket_number>=3 and ticet_number %2!=0

  3. select *
    from (select name, ROW_NUMBER() over (order by ticket_no) as srNo from db) t
    where (t.srNo % 2) = 1

  4. select name,ticket_number from (
    select name,ticket_number,row_number() over (order by ticket_number) as r_num from table) t
    where t.r_num >=3 and t.r_num%2=1;

Leave an answer