SQL Interview Questions | Alternate Rows
Question
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
4
SQL
55 years
21 Answers
5932 views
Great Grand Master 6
Answers ( 21 )
SELECT *
FROM (
SELECT *, Row_Number() OVER(ORDER BY ticketno) AS RowNumber
FROM Lottery
) t
WHERE t.RowNumber % 2 = 1
OFFSET 2 rows
Shouldn’t it be offset 1 Rows?
Yes. Offset 1 row
select * from
(select name,ticket number,
row_number() over(order by ticket number))
where ticket_number>=3 and ticet_number %2!=0
select *
from (select name, ROW_NUMBER() over (order by ticket_no) as srNo from db) t
where (t.srNo % 2) = 1
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;
Select Row_Number() OVER( ORDER BY Ticket Number)as Rownumber,
Name,Ticket_Number
from Lottery
where Rownumber >3 and Rownumber/2 !=0;
WHERE clause cannot be used in the query where WINDOW FUNCTION is used.
There should be different queries. (Sub-Query)
select name as winner
from
(select name , ticket_number, row_number() over(order by ticket_number from table)) t1
where t1.row_number >= 3 and t1.row_number%2=1
With CTE as
(Select
name
,Row_number() over (order by select Null) as rn
from table)
Select
Name
from CTE
where rn%2=1
order by rn
offset 1 ROWS
WITH lottery AS
(SELECT
name
,ticketnumber
,ROW_NUMBER() OVER (ORDER BY ticketnumber) AS rn
FROM lottery)
SELECT
name
,ticketnumber
FROM lottery
WHERE MOD( rn ,2) = 1 AND rn != 1
WITH CTE AS (SELECT * FROM
(SELECT NAME,TICKET_NUMBER
ROW_NUMBER()OVER()
FROM HR_EMPLOYEES
) SD
WHERE ROW_NUMBER >= 3
)
SELECT * FROM CTE
WHERE ROW_NUMBER % 2 0
select name as winner from
(select name, ticket_no, row_no() over (order by ticket_no from lottery)) lottery
where ticket_no. >=3 and ticket_no % 2 != 0
Sample table created with name Lottery
Alternate rows without 1st row.
SELECT *
FROM
(SELECT name,ticket number,
row_number() OVER (ORDER BY ticket number))
WHERE ticket_number>=3 and ticket_number %2!=0
select * from(
select name,ticket, row_number() over (order by ticket) as r from table)t where r>=3 and r%2!=0;
select employee_id
from
(
select employee_id, first_name, department,
row_number() over() as row_num from employees
) A
where row_num >=3
and row_num%2 = 1
select name, ticket_no
from
(
select name, ticket_no,
row_number() over() as row_num from lottery
) A
where row_num >=3
and row_num%2 = 1
select name, ticket_no
from
(
select name, ticket_no,
row_number() over() as row_num from lottery
) A
where row_num >=3
and row_num%2 = 1
SELECT row_, name, ticket_number
FROM ( SELECT row_number() over ( partition by name ) row_, name, ticket_number
FROM lottery_table ) t
WHERE row_>=3, row_%2!=0
SELECT *
FROM (
SELECT *, Row _Number() OVER(ORDER BY ticket no) AS Row Number
FROM Lottery
) t
WHERE t.RowNumber % 2 = 1
OFFSET 2 rows