Please like the video for more interview questions 🙏
@Sai-dc7lc5 ай бұрын
@Ankit, It’s been a week since the sql video recordings are not opening. Even when I tried reloading the video, it’s not showing anything. Could you please look into it?
@Vinaykumar-ny8rm3 ай бұрын
@ankitbansal your website namaste sql is not working could you please look into it i tried lot but getting 505 server error
@Clintonlobo5 ай бұрын
Can't believe I used method 3 in my first attempt. Watchin your videos has really helped me pick your brain. Thank you Ankit!
@invincible99715 ай бұрын
Same here, I have also created the 3rd attempt logic at first time before watching the solution. This is my version: select * from (select case when free = lead(free) over (order by seat_id) or free = lag(free) over (order by seat_id) then seat_id end as free_seats from cinema ) c where free_seats is not null ; The videos really gives clarity for logic building in SQL.
@saralavasudevan51676 ай бұрын
Hi Ankit thanks for the question and your approaches! This is my take at the problem: with mycte as ( select *, case when (free = 1 and prev_free = 1) or (free = 1 and next_free = 1) then 'Yes' else 'No' end as seat_status from ( select *, lag(free,1) over(order by seat_id) as prev_free, lead(free,1) over(order by seat_id) as next_free from cinema ) as x ) select seat_id from mycte where seat_status = 'Yes'
@thatwasavailable2 ай бұрын
This is an amazing approach, thanks. Very easy to understand.
@suyashgupta61692 ай бұрын
proud, solved using method 3 in first attempt. All thanks to you!
@ankitbansal62 ай бұрын
Excellent!
@namangarg70234 ай бұрын
I was only strong in Power Bi before only but after watching and solving ur problems I have got interest in SQL
@mantisbrainsАй бұрын
select seat_id, seat_id - rn as diff from (select *, row_number() over() rn from (select seat_id from cinema where free = 1)a)l where seat_id - rn in (select diff from (select seat_id, seat_id - rn as diff from (select *, row_number() over() rn from (select seat_id from cinema where free = 1)a)f)l group by diff having count(diff) >= 2) Thanks, Ankit!
@Datapassenger_prashant6 ай бұрын
there is always something to learn in each of your video sir. I have solved such example a lot from your videos. Stil got to learn a different approach: self join approach to find consecutive seat. Thanks a lot...
@tamojeetchatterjee93856 ай бұрын
Hi Ankit vey interesting question Here is my solution for this with cte as (select free , seat_id , lag(seat_id)over() , seat_id - lag(seat_id)over() as diff from cinema where free 0) --select * from cte , gte as (select * , lead(diff)over() as diff_2 from cte) --select * from gte select seat_id from gte where diff 2 or diff_2 2
@sanjayhc86724 ай бұрын
Hi Ankit, with cte as( select *, lag(free,1) over(order by seat_id) as prev_seat, lead(free,1) over(order by seat_id) as next_seat from cinema) select seat_id from cte where (free=1 and prev_seat=1) or (free=1 and next_seat=1) order by seat_id;
@mrjana5204 ай бұрын
This is the Simplest and Smallest query. select seat_id from (select seat_id, free, lead(free,1,1) over(order by seat_id) as lead_free from seats) where free = 1 and free = lead_free;
@vijeayanvj43675 күн бұрын
select seat_id from (select case when lead(free)over(order by seat_id)=1 and free=1 then seat_id when free=1 and lag(free)over(order by seat_id)=1 then seat_id end as seat_id from cinema) abc where seat_id is not null
@shubhamsoni1746 ай бұрын
Thanks for sharing such questions ❤ Please try do more such videos. 👍
@Iyersukz65 ай бұрын
---Method 3--- select * from( select * , lag(free,1) over (order by seat_id) as prev , lead(free,1) over (order by seat_id) as next from cinema) as a where free = prev or free = next
@saiteja-gb8ho6 ай бұрын
Thanks for posting such useful questions bro . Here is my approach in Postgres : WITH CTE AS (SELECT * ,ROW_NUMBER()OVER(ORDER BY SEAT_ID) AS RN FROM CINEMA WHERE FREE=1) SELECT SEAT_ID FROM CTE WHERE SEAT_ID -RN=1
@chinmayjoshi70382 ай бұрын
Hi Ankit, Here's my take on the problem: with cte as ( SELECT * FROM cinema WHERE Free=1) select a.seat_id from cte a join cte b on a.seat_id=b.seat_id+1 union select a.seat_id from cte a join cte b
@namangarg70234 ай бұрын
u r superb like virendra sehwag...har ball pe chauka and 6😀
@Chathur7325 ай бұрын
Hi Ankit, my approach is as follows: with cte as (select seat_id , (seat_id - rn) as diff from ( select * , rank() over(partition by free order by seat_id) as rn from cinema C where C.free = 1 order by C.seat_id) a group by seat_id) select seat_id from cte where diff in (SELECT diff FROM cte GROUP BY diff HAVING COUNT(diff) >= 2)
@dgsharma47736 ай бұрын
with cte as( select *, lag(free,1) over (order by seat_id) as prev_seat, lead(free, 1, free) over (order by seat_id) as next_seat from cinema ) select seat_id from cte where free = 1 and ( prev_seat = 1 and next_seat = 1) or (prev_seat = 1 and next_seat= 0) or (prev_seat = 0 and next_seat = 1)
@aaradhanasrivastava27575 ай бұрын
Hi Ankit, Below is my approach :- with cteseat as ( select * , lead (free ) over ( order by seat_id) as next_seat from cinema ) ,ctenext as ( select seat_id , seat_id +1 as nextseatid from cteseat where free = 1 and free = next_seat ) select seat_id from ctenext UNION select nextseatid from ctenext
@skkholiyaАй бұрын
with cte_preceding as( select *,sum(if(free=1,null,1)) over(order by seat_id rows between unbounded preceding and current row) sm from cinema ) select * from( select *,sum(1) over(partition by free,sm) rnk from cte_preceding order by seat_id) x where rnk>=2
@nirmalpatel34306 ай бұрын
cool question!! channel is treasure of learning!!
@gopinathg3186 ай бұрын
My solution with small differences --Method 1 using lead and lag with free_seats as( select seat_id, lead(seat_id)over(order by seat_id) as next_seat,lag(seat_id)over(order by seat_id) as previous_seat from cinema where free=1 ) select seat_id from free_seats where (seat_id+1=next_seat or seat_id-1=previous_seat) ; --Method 2 using inner join select distinct c1.seat_id from cinema c1 inner join cinema c2 on (c1.seat_id+1=c2.seat_id or c1.seat_id-1=c2.seat_id) where c1.free=1 and c2.free=1 order by c1.seat_id ;
@vivekdutta71315 ай бұрын
with cte AS ( select *,row_number() over(order by seat_id) as rn, seat_id - row_number() over(order by seat_id) as diff from cinema where free = 1 ) select seat_id from cte JOIN (select diff,count(*) as cnt from cte group by diff having count(*) >1 )A on cte.diff = a.diff order by seat_id
@arvindkr9745 ай бұрын
My Approach: with seat_rank as ( select seat_id,free, (seat_id-row_number() over (partition by free order by seat_id)) as diff from cinema ), Consecutive_seat as ( select min(seat_id) start_seat,max(seat_id) end_seat,COUNT(seat_id) as consec from seat_rank where free = 1 group by diff having COUNT(seat_id)=3 ) select seat_id from cinema c join Consecutive_seat f on c.seat_id between f.start_seat and f.end_seat
@aswinc48295 ай бұрын
Grand explanation
@TechWithViresh2 ай бұрын
with cte as ( select * , sum(case when free = 0 then 1 else 0 end) over (order by seat_id) as gr_id from cinema ), cte2 as ( select * , count(*) over (partition by gr_id) as seq_count from cte ) select * from cte2 where seq_count-1>=2 and free !=0;
@SamienRaahhat6 ай бұрын
WITH CTE AS( SELECT seat_id, free, CASE WHEN free=1 THEN 'vacant' ELSE 'occupied' END AS availability, LAG(free) OVER(ORDER BY seat_id) AS prev_seat , LEAD(free) OVER(ORDER BY seat_id) AS next_seat FROM cinema) SELECT seat_id FROM CTE WHERE free=1 AND (next_seat=1 OR prev_seat=1) ORDER BY seat_id; EASY approach
@abhiksaha34515 ай бұрын
It will fail if free =0 for consecutive seats either before or after
@SamienRaahhat5 ай бұрын
@@abhiksaha3451 thanks for the correction.. edited it
@ankitamahajan38994 ай бұрын
select seat_id from ( select *, case when free='1' and( lag(free,1) over(order by seat_id) ='1' or lead(free,1) over(order by seat_id)='1') then 'Y' else 'N' end as flag from cinema) a where flag='Y'
@harshitsalecha2215 ай бұрын
WITH cte1 AS (SELECT *, LEAD(seat_id) OVER(ORDER BY seat_id)-seat_id as lead_seat_diff, seat_id-LAG(seat_id) OVER(ORDER BY seat_id) as lag_seat_diff FROM cinema WHERE free=1) SELECT seat_id FROM cte1 WHERE lead_seat_diff=1 OR lag_seat_diff=1;
@hariikrishnan6 ай бұрын
At 12:17 using c2.seat_id - c1.seat_id = 1 also gives the same output
@ashwingupta47656 ай бұрын
with cte as ( select *, (lag(free,1) over( order by seat_id)*free) as lde, (lead(free,1) over( order by seat_id)*free) as rwn from cinema) Select * from cte where lde =1 or rwn =1
@bommanasravan82796 ай бұрын
with cte as( select seat_id,free,case when free=1 and (lead(free)over(order by seat_id)=1 or lag(free)over(order by seat_id)=1) then 'yes' else 'no' end as 'flag' from cinema) select seat_id,free from cte where flag='yes''
@Vaibha2936 ай бұрын
with cte as( select * ,seat_id-sum(free) over(order by seat_id)t from cinema where free = 1) select seat_id from cte where t in (select t from cte group by t having count(t)>=2)
@mathavansg92274 ай бұрын
with cte as (SELECT *,lead(free,1) over(ORDER by seat_id asc) as ld,lag(free,1,0) over(ORDER by seat_id asc) as lg from cinema) SELECT * from cte where free =1 and (ld=1 or lg=1)
@SumanSadhukhan-md4dq6 ай бұрын
Awesome ❤
@datadecoder_sai12 күн бұрын
The following is my approach: select distinct a.seat_id from cinema a join cinema b on abs(a.seat_id - b.seat_id) = 1 and a.free = 1 and b.free = 1 order by a.seat_id;
@AmanRaj-p8w5 ай бұрын
MySQL solution: with cte as (select seat_id, diff, count(*) over (partition by diff) as cnt from ( select *, row_number() over (order by seat_id) as rw, seat_id - row_number() over (order by seat_id) as diff from cinema where free = 1 ) as x ) select seat_id from cte where cnt >= 2
@KoushikT5 ай бұрын
with A as (select seat_id, free, seat_id - row_number() over (order by seat_id) as grp from cinema where free = 1) select seat_id from A where grp in (select grp from A group by 1 having count(*)>2)
@VAR217236 ай бұрын
Good 👍👍👍👍❤❤
@sahilsood202812 күн бұрын
Solution : with cte as ( select *, lead(seat_id) over( order by seat_id) as nextseat_id, lead(free) over( order by seat_id) as nextfree from cinema ), cte2 as ( select * from cte where free - nextfree = 0 ) select seat_id from cte2 union select nextseat_id from cte2 order by seat_id
@mrpam46726 ай бұрын
This can be solved with self join, row number method, lead/lag and also advanced aggregation
@ankitsaxena5656 ай бұрын
Thanks sir 🙏
@adityakishan15 ай бұрын
with cte1 as ( select seat_id, free, case when (free=1 and lead(free) over(order by seat_id) =1 ) then 'c' when (free=1 and lead(free) over(order by seat_id) =0 and lag(free) over(order by seat_id) =1) then 'c' when (free=1 and lead(free) over(order by seat_id) =0 and lag(free) over(order by seat_id) =0) then 'n' else 'n' end flg from cinema ) select seat_id from cte1 where flg='c'
@bukunmiadebanjo96845 ай бұрын
I used attempt 3 straight off the bat. here is my attempt; with cte as (SELECT c.*, LEAD(c.free,1,0) OVER(ORDER BY c.seat_id) after, LAG(c.free,1,0) OVER(ORDER BY c.seat_id) before FROM cinema c) SELECT seat_id FROM cte WHERE (free = 1 and after = 1) OR (free = 1 and after = 1 and before = 1) OR (free = 1 and before = 1)
@amritsinghania27226 ай бұрын
Sir it's for fresher interview question or experience one
@rahulbehera112Ай бұрын
sir can you share what sql questions were asked in the online assessment of tredence analytics
@monasanthosh92085 ай бұрын
Select Seat_id,Free from (Select *,Count(flag) over (Partition by Flag) as seg from (Select *,seat_id-row_number() over (Order by seat_id) as Flag from (Select * from cinema where free=1)N)N)N where Seg>2;
@sandeepanand38345 ай бұрын
In method 2, how you got output in ascending order; i am not getting the output in ascending order? its union between 2 tables without sorting.
@MuskanGoyal-db7cs5 ай бұрын
with cte as( select seat_id, free,lead(free,1)over(order by seat_id) as new from cinema) select seat_id ,free from cte where new=1 and free=1;
@ashanair16026 ай бұрын
I think the best method was the third.
@gameply3476 ай бұрын
Sir please create a video on TIME function if possible.
@shashwatdev23716 ай бұрын
Here is my approach using correlated subquery- select seat_id from cinema a where exists (select 1 from cinema b where a.free=1 and b.free=1 and (a.seat_id+1=b.seat_id or a.seat_id-1=b.seat_id)) another simpler method- with cte as ( select seat_id from cinema where free=1 ) select seat_id from cte where seat_id-1 in (select seat_id from cte) or seat_id+1 in (select seat_id from cte);
@abhiksaha34515 ай бұрын
The 2nd solution will takes a lot of resources if you have millions of rows as "in" is equivalent to multiple "or"s and it has to run 3x
@mohammaddanishkhan72886 ай бұрын
Here's my solution Sir, let me know what you think about it. WITH flag_cte AS ( SELECT *, SUM(free) OVER(ORDER BY seat_id ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS lead_flag, SUM(free) OVER(ORDER BY seat_id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS lag_flag FROM cinema ) SELECT seat_id FROM flag_cte WHERE lead_flag = 2 OR lag_flag = 2
@Apna_tahlka_1236 ай бұрын
A humble request to u plj thoda thoda hindi bhi bola kro by which i can understand easily
@ishashuklaa2 ай бұрын
WITH t1 as(select *, (seat_id - ROW_NUMBER() OVER ( order by seat_id)) as rnk FROM cinema WHERE free = 1), t2 AS (SELECT *, count(*) over(Partition by rnk) as cons_records From t1) SELECT seat_id from t2 where cons_records >=2;
@sumitshrivastava44945 ай бұрын
with cte as ( SELECT *, seat_id - free as sub_seat_free from cinema) select seat_id, free from ( SELECT *, lag(sub_seat_free, 1) over(order by sub_seat_free) as previous_row, lead(sub_seat_free, 1) over(order by sub_seat_free) as next_row from cte ) a where sub_seat_free - previous_row = 1 or next_row - sub_seat_free = 1
@sravyasrinivas6 ай бұрын
Coudl you please add the DDL for the question
@ankitbansal66 ай бұрын
Description box
@bhumikalalchandani3216 ай бұрын
same Leetcode question too
@ethyria76854 ай бұрын
WITH cte AS (SELECT *, LAG(free)OVER(ORDER BY seat_id) as prev, LEAD(free)OVER(ORDER BY seat_id) as next FROM cinema) SELECT seat_id FROM cte WHERE free = 1 AND (prev = 1 or next = 1)
@abhishekgarg90296 ай бұрын
Hey Ankit, what YOE was this question asked for? If u have any idea
@ArunYadav-s2o5 ай бұрын
method 2 is giving even two consecutive seats
@The5f5f5f55 ай бұрын
WITH T AS ( SELECT SEAT_ID,CASE WHEN (SEAT_ID-LAG(SEAT_ID) OVER() =1) OR (LEAD(SEAT_ID) OVER()-SEAT_ID = 1) THEN 1 ELSE 0 END AS SEQUENCE_FILTER FROM CINEMA WHERE FREE=1) SELECT SEAT_ID FROM T WHERE SEQUENCE_FILTER=1 ;
@girishsrinivasan24143 ай бұрын
We can also do this by WITH ConsecutiveSeats AS ( SELECT seat_id, free, seat_id - ROW_NUMBER() OVER (ORDER BY seat_id) AS group_id FROM cinema WHERE free = 1 ), ConsecutiveGroups AS ( SELECT group_id, MIN(seat_id) AS start_seat, MAX(seat_id) AS end_seat, COUNT(*) AS seat_count FROM ConsecutiveSeats GROUP BY group_id ) SELECT start_seat, end_seat, seat_count FROM ConsecutiveGroups WHERE seat_count > 2 ORDER BY start_seat;
@user-rc2uc1kv6w6 ай бұрын
Ankit sir aap please Full Microsoft SQL Server pe ek course launch karein 🤲 abhi k time MSSQL demand mein hain. usme SSMS, SSAS, SSRS ye bhi sikhain
@Savenature6356 ай бұрын
Thanks for sharing this question, Here is my approach : with cte as (select *,row_number() over() as rn, seat_id-row_number() over() as grp from cinema where free=1) select seat_id from cte where grp in (select grp from cte group by grp having count(1)>=2);
@MonkeyDLuffy48853 ай бұрын
WITH cte AS ( SELECT seat_id, free, ROW_NUMBER() OVER(ORDER BY seat_id) AS rn, seat_id - ROW_NUMBER() OVER(ORDER BY seat_id) AS diff FROM cinema WHERE free=1) SELECT seat_id FROM cte WHERE diff IN ( SELECT DISTINCT diff FROM cte GROUP BY diff HAVING COUNT(*) > 1 )
@vaibhavverma13406 ай бұрын
with cte as(select *, seat_id-row_number() over (order by seat_id)rn from cinema where free = 1) , cte2 as (select *, count(*) over (partition by rn)cnt from cte) select cte2.seat_id from cte2 where cnt>=2
@syedzeeshan474218 күн бұрын
WITH cte AS(SELECT *,row_number() over(PARTITION BY FREE ORDER BY seat_id) AS flag FROM cinema WHERE FREE!=0 ORDER BY seat_id), cte2 AS(SELECT *,count(flag1) OVER(PARTITION BY flag1 ) AS cnt FROM (SELECT *,CASE WHEN FREE=0 THEN 0 ELSE seat_id-flag END AS flag1 FROM cte)) SELECT seat_id FROM cte2 WHERE cnt>1
@RenuSingh-x8d7 күн бұрын
Leetcode ka s h
@hrishiAOL6 ай бұрын
Bhai video mai clarify rakho naa mere just specs waale ko stress aata hai
@ankitbansal66 ай бұрын
Change the video quality
@edumail10165 ай бұрын
WITH cte AS ( SELECT seat_id, free, seat_id - ROW_NUMBER() OVER (ORDER BY seat_id) AS rn FROM cinema WHERE free=1 ) SELECT seat_id FROM cte WHERE rn IN (SELECT rn FROM cte GROUP BY rn HAVING COUNT(*)>=3)
@meghnasoni2 ай бұрын
SELECT * FROM (SELECT case when cnt is not null then cnt ELSE cnt2 END AS final FROM (SELECT *,CASE WHEN free=lagg theN seat_id ELSE null END AS cnt , CASE WHEN free=leadd theN seat_id ELSE null END AS cnt2 FROM ( SELECT * , lag(free) over(ORDER by seat_id) as lagg, lead(free) over(ORDER by seat_id) as leadd FROM cinema) )) WHERE final is not null
@Prakash-xn9hvАй бұрын
@ankitbansal6 in coding question is python language is compulsory?