SQL Interview : Display Odd or Even rows in Oracle SQL

Описание к видео SQL Interview : Display Odd or Even rows in Oracle SQL

5/2=?

2)5(2 --quotient or result
4
___
1 -- remainder

Mod(5,2)=1

Display ODD or EVEN rows from a table
Here I have customers table with
customer_id and customer_name

Mod() function returns reminder
eg Mod(5,2)=1
Mod(4,2)=0
when we pass even number it returns zero
when divided by 2 and vice versa

select row_number() over (order by customer_id) as row_no from customers

this query returns the row number

select * from
(select ROW_NUMBER() over (order by customer_id) as row_no from customers)s1
where mod(s1.row_no,2)=1

Gives Odd rows

Final:
ODD rows:
select * from
(select c.*,ROW_NUMBER() over (order by customer_id) as row_no from customers c)s1
where mod(s1.row_no,2)=1
EVEN rows:
select * from
(select c.*,ROW_NUMBER() over (order by customer_id) as row_no from customers c)s1
where mod(s1.row_no,2)=0

Комментарии

Информация по комментариям в разработке