Complex SQL 3 | Scenario based Interviews Question for Product companies

In this video we will talk about a scenario based question. It is a medium complex query and these kind of questions you can expect in product based company interviews. here is the script if you want to try yourself.
create table entries (
name varchar(20),
address varchar(20),
email varchar(20),
floor int,
resources varchar(10));
insert into entries
values ('A','Bangalore','A@gmail.com',1,'CPU'),('A','Bangalore','A1@gmail.com',1,'CPU'),('A','Bangalore','A2@gmail.com',2,'DESKTOP')
,('B','Bangalore','B@gmail.com',2,'DESKTOP'),('B','Bangalore','B1@gmail.com',2,'DESKTOP'),('B','Bangalore','B2@gmail.com',1,'MONITOR')

Пікірлер: 247

  • @ankitbansal6
    @ankitbansal6 Жыл бұрын

    Master the art of SQL @ Rs 1999 with my zero to hero SQL course. The course is focused on data analytics and covers all the advanced concepts starting from scratch. www.namastesql.com/courses/SQL-For-Analytics-6301f405e4b0238f71788354 The course contains 2 portfolio projects and a bonus session on Tableau. 100+ interview problems to crack any SQL INTERVIEW. You will also get a premium subscription to a website to practice SQL problems worth Rs 5000.

  • @pavitrashailaja850
    @pavitrashailaja8502 жыл бұрын

    Great videos u make. Keep going!

  • @vikasvk9174
    @vikasvk9174 Жыл бұрын

    Thank you Ankit Because of you am able to think and write such complax sql ......................................................... with concat_resources as ( select name,count(1) as total_count,group_concat(distinct resources) as resources_used from entries group by name ), most_visit_floor as ( select name ,floor,count(1) as no_of_floor_visit,rank() over(partition by name order by count(1) desc) as rn from entries group by name,floor ) select cr.name,cr.total_count,mv.floor,cr.resources_used from concat_resources cr inner join most_visit_floor as mv on cr.name=mv.name and mv.rn = 1;

  • @weshallbe
    @weshallbe10 ай бұрын

    I'm just in awe the things you can output using SQL! I just started preparing for my future SQL interviews. Just love your videos and the way you approach a problem. One thing I'm learning from you is, a complex query needs to broke down into simpler queries. Thank you, Ankit!

  • @ankitbansal6

    @ankitbansal6

    10 ай бұрын

    You got this!

  • @varaprasadpurum389
    @varaprasadpurum389 Жыл бұрын

    Great to see your efforts making this Ankit. I really appreciate you making the challenging tasks and solving them at the same time

  • @ankitbansal6

    @ankitbansal6

    Жыл бұрын

    Thanks a ton

  • @ritika29
    @ritika29 Жыл бұрын

    Great question. Thanks! Below is my version of SQL in Oracle with cte as( select name,floor,count(floor) as floor_visit_count ,rank() over(partition by name order by count(floor) desc) rn from entries group by name,floor) select e.name, count(e.name),cte.floor most_visired_floor ,listagg(distinct e.resources, ',') within group(order by e.resources) as resources_used from entries e inner join cte on (e.name=cte.name) where rn=1 group by e.name,cte.floor;

  • @ankitbansal6

    @ankitbansal6

    Жыл бұрын

    Good effort. Keep going 😊

  • @bhuwansaoji5307
    @bhuwansaoji53075 ай бұрын

    another approach would be like this select name, count(*) as total_visits, (select floor from entries b where b.name=a.name group by floor order by count(*) desc limit 1) as most_floor_visited, group_concat(distinct resources) as resources_used from entries a group by name

  • @sujaa1000
    @sujaa100025 күн бұрын

    Thank you very much, please make more such videos, very very helpful!

  • @vaibhavverma1340
    @vaibhavverma13402 жыл бұрын

    I solved 50% and not getting further , so watch it your videos it's too complex question but you did in a easy way. One of the best sql channel till now . No doubt keep uploading videos and soo get 1M subscribers sir:)

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thank you Vaibhav 😊

  • @anthonychilaka
    @anthonychilaka8 ай бұрын

    Day 3. thank you Ankit for this scenario sql session.

  • @rishabhdhupar4579
    @rishabhdhupar4579 Жыл бұрын

    I solved this in Postgressql using following query select name, sum(count) as total_visits,first_value as most_visted_floor, array_to_string(array_agg(resources),',') as resource_used from(select name, floor, resources, count(*), first_value(floor) over(partition by name order by count(*) desc) from entries group by name, floor, resources) a group by name, first_value

  • @zeeshanahmed2594
    @zeeshanahmed25949 ай бұрын

    Thank you very much, Sir. After watching your two videos from the playlist, I was able to solve this on my own.

  • @ankitbansal6

    @ankitbansal6

    9 ай бұрын

    Great job!

  • @sakshamratra4199
    @sakshamratra41992 жыл бұрын

    Loved it! Thanks for sharing!

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thank you 😊

  • @manjunathreddy5566
    @manjunathreddy556626 күн бұрын

    Hi Ankit thanks for uploading videos ..Need same way datascience videos it's helpful

  • @nidhisingh4973
    @nidhisingh4973 Жыл бұрын

    You are just awesome

  • @rahulbansal3811
    @rahulbansal38112 жыл бұрын

    Thanks for sharing Ankit just loved it

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Welcome

  • @indergaming3053
    @indergaming305311 ай бұрын

    Ankit bhai tumhare questions solve karke really bahut achi practise hoti ha really love your content >>>>>

  • @ankitbansal6

    @ankitbansal6

    11 ай бұрын

    Keep going 💪

  • @Tushar-xe6is
    @Tushar-xe6is Жыл бұрын

    Very interesting Question Ankit, keep uploading videos like that it helps alot.

  • @ankitbansal6

    @ankitbansal6

    Жыл бұрын

    Thank you, I will

  • @VivekKumar-dz7ie
    @VivekKumar-dz7ie2 жыл бұрын

    You are awesome. I can think SQL now.

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Wow, thanks! if you can think then you can write :)

  • @ajaxaj6749
    @ajaxaj67492 жыл бұрын

    Good question. Pls make more such videos

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Sure.

  • @kunalberry5776
    @kunalberry5776 Жыл бұрын

    Thanks for this questions

  • @playtrip7528
    @playtrip7528 Жыл бұрын

    amazing as always

  • @krishnapavanayitha5749
    @krishnapavanayitha57492 жыл бұрын

    Might be below provided is easiest solution. Have a look at it. select * from entries; select name,count(name),floor,GROUP_CONCAT(DISTINCT resources) from ( select name,floor,count(floor) over(partition by name,floor order by floor) as c,resources from entries ) ta group by name having c=max(c); -- GROUP_CONCAT concats all the string in a group -- string_agg is almost equivalent function to GROUP_CONCAT in sql server

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Having c= max(c) won't work.

  • @shahakunal1
    @shahakunal13 ай бұрын

    Hi Ankit , This is the way we can implement as well select t.name ,total_visits, floor as most_used,string_agg as resources_used from (select name,floor from ( select name,floor ,count,rank() over (partition by name order by count desc) as rnk from ( select name,floor,count(*) from entries group by name,floor) as a) as b where rnk = 1) as t JOIN (select name,count(*) as total_visits , STRING_AGG(distinct resources,',') from entries group by name ) as q on t.name = q.name;

  • @DilipKumar-of3jk
    @DilipKumar-of3jk Жыл бұрын

    Thanks Ankit for the video!! Very useful as always. Here is my Oracle SQL version of the solution. with t1 as (select name,floor, count(floor) over (partition by name) as total_visits, count(floor) over (partition by name, floor) as most_visit_floor, listagg(distinct resources,',') within group (order by resources) over (partition by name) as resource_used from entries), t2 as (select name,total_visits, floor,resource_used, rank () over (partition by name order by most_visit_floor desc) as rnk from t1) select name,total_visits,floor as most_visited_floor,resource_used from t2 where rnk=1;

  • @ankitbansal6

    @ankitbansal6

    Жыл бұрын

    Thanks for posting 👏

  • @sailajatumu3682
    @sailajatumu3682 Жыл бұрын

    This is another way of writing

  • @ramakumarguntamadugu1299
    @ramakumarguntamadugu12994 ай бұрын

    good one !

  • @mrrathore55
    @mrrathore552 жыл бұрын

    Outstanding.

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thank you.

  • @SarmisthaDebRoy
    @SarmisthaDebRoy2 ай бұрын

    with y as( select name, floor, resources, count(distinct email) visited_floors from employee group by 1,2,3) select name,group_concat(resources) resources_used, case when visited_floors=max(visited_floors) then floor else null end as most_visited_floor, sum(visited_floors) as total_visits from y group by 1;

  • @anuragsekhri2315
    @anuragsekhri2315 Жыл бұрын

    insightful

  • @aishwaryabhangarshettra984
    @aishwaryabhangarshettra9842 жыл бұрын

    amazing video

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thank you 😊

  • @shivangisharma592
    @shivangisharma5922 ай бұрын

    with cte1 as ( select distinct name,floor,count(1) over (partition by name,floor order by name) as rnk from entries ) ,floor_max as ( select name,floor as floor_max from cte1 where rnk = (select max(rnk) from cte1) ) , tot_vis as ( select a.name,b.floor_max,count(1) as tot_visits,group_concat(resources) as tot_visits from entries a join floor_max b on a.name=b.name group by 1,2 ) select * from tot_vis

  • @mrx7148
    @mrx71487 ай бұрын

    Good explanation Ankit. Can you also try to provide equivalent queries supported in other database systems like Oracle, Postgres, MySQL etc. I know we can get the same using ChatGPT but sometimes it's not very accurate. Anyways kudos to your knowledge. Also what are your other recommended sources to improve on SQL knowledge including performance tuning.

  • @mihirsamant9352
    @mihirsamant9352 Жыл бұрын

    I want to take a moment to thank @Ankit Bansal for all great stuff. I cleared couple of tech rounds because of your videos. Love the way you solve problems and share the knowledge. Thank you so much Keep it up

  • @ankitbansal6

    @ankitbansal6

    Жыл бұрын

    Congratulations. Keep rocking 🥳🥳

  • @SudhirKumar-rl4wt
    @SudhirKumar-rl4wt2 жыл бұрын

    I am just wondering.. in your solution ..in cte floor_visit ..we are getting correct ranking of most visited floors because of group by name,floor ..so no need of adding name in "partition by" of rank function ..and anyways this grouping is on name,floor level ..so only "partition by" at name level will give wrong result ..here I believe "order by count(1) desc" itself is ranking groups of each name,floor combination count wise in descending order.

  • @temitopeolagoke
    @temitopeolagoke Жыл бұрын

    DISTINCT resources without the CTE works for me on PostgreSQL

  • @pravinmahindrakar6144
    @pravinmahindrakar6144 Жыл бұрын

    Hi Ankit, Will that works select name, count(1) as no_of_visits, floor as most_visited_floor, group_concat(distinct resources) as resources from entries group by name having count(floor)>1

  • @sobhiksaha7140
    @sobhiksaha7140 Жыл бұрын

    Thanks Ankit Sir for such an interesting problem. Here's my solution in MySQL: with floor_count as (SELECT name, floor, count(*) as floor_count from entries group by 1,2), cte as (select e.*,floor_count from entries e join floor_count f on e.name = f.name and e.floor = f.floor ) select e.name, count(e.name) as total_visists, case when e.floor_count = max(f.floor_count) then e.floor end as most_visited_floor, group_concat(distinct resources) as resources_used from cte e join floor_count f on e.name = f.name and e.floor = f.floor group by 1 order by 1;

  • @ankitbansal6

    @ankitbansal6

    Жыл бұрын

    Great effort 😊

  • @kanchankumar3355
    @kanchankumar33552 жыл бұрын

    Informative

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thank you.

  • @learnandapplywithMohit
    @learnandapplywithMohit5 күн бұрын

    Another approach to solve it : with b as (select name,count(name) Total_visits from entries group BY 1 order by 1), d as (select name,floor as most_visited_floor from (select name,floor,cnt,RANK()over(partition by name order by cnt desc) rnk from (select name,floor,count(floor) as cnt from entries group by 1,2 order by 1,2) order by 1) where rnk=1 order by 1), e as (select name, (resources || ',' || resources_next) as resources_used from (select * from (select name,resources,lead(resources)over(PARTITION by name order by resources) resources_next from entries order by 1) where resources resources_next and resources_next is not NULL)) select b.name,b.Total_visits,d.most_visited_floor,e.resources_used from b join d on b.name=d.name join e on b.name=e.name order by 1

  • @abhimistry9226
    @abhimistry9226 Жыл бұрын

    thanks sir

  • @discovery4808
    @discovery48082 жыл бұрын

    Nice Video

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thanks Alok bhai 🙂

  • @sailajatumu3682
    @sailajatumu3682 Жыл бұрын

    with CTE as ( select a.* ,row_number() over(order by customer_id,order_date) as Rn ,order_Date - row_number() over(order by customer_id,order_date) as rp from EIM_QA.customer_orders a ) ,noofcust as ( select customer_id,count(rp) as cnt from CTE group by customer_id ) select customer_id,cnt ,case when cnt=1 then 'New cust' ---end Newcust when cnt> 1 then 'Rep crusomter' end cust from noofcust group by customer_id,cnt

  • @VishalSharma-hv6ks
    @VishalSharma-hv6ks2 жыл бұрын

    Great

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thank you 😊

  • @aaravkumarsingh4018
    @aaravkumarsingh4018 Жыл бұрын

    with cte as( select name ,floor,count(floor) as max_visited_floor ,resources from entries group by name,floor) select name,sum(max_visited_floor) as total_visits, case when max_visited_floor=max(max_visited_floor) then floor else Null end most_visited_floor, group_concat(resources) as resources from cte group by name;

  • @satyajitbiswal6162

    @satyajitbiswal6162

    Жыл бұрын

    please once check .its showing eror

  • @fog2640
    @fog26408 ай бұрын

    finished watching

  • @vishnujatav6329
    @vishnujatav6329 Жыл бұрын

    Useful

  • @amithgshetty8171
    @amithgshetty81712 ай бұрын

    Hello ankit sir, just wanted your opinion on one thing. Can we use mode() function on the floor number to get most visited floor? And just do group_concat on the resources to get the list of resources used? Please correct me if im wrong

  • @abhishekbhowmick2590
    @abhishekbhowmick2590 Жыл бұрын

    Thanks Ankit for the video. It is very help full. Here is mysql solution:- select name, count(*) Total_visited_floor, floor as most_visited_floor, group_concat(distinct resources) used_resources from entries group by name

  • @florincopaci6821
    @florincopaci68212 жыл бұрын

    Thank you!

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Glad you liked it 😊

  • @revathigangisetty5734
    @revathigangisetty5734 Жыл бұрын

    'STRING_AGG' is not a recognized built-in function name .iam getting this error ankit. i tried in 2016 and 2012 version.?

  • @prajwalkunder2760
    @prajwalkunder2760 Жыл бұрын

    select name,count(*) as total_visits, max(floor) over(partition by name order by name) as most_visited_floor, group_concat(distinct resources ) as resources_used from entries group by name order by name;

  • @sivaganesh3224
    @sivaganesh32247 ай бұрын

    Select name,count(floor)total visist,max(floor)most visits,max(resources) || ' ,' || min(resources) resources used from table name group by name

  • @shalinimeena7863
    @shalinimeena78639 ай бұрын

    select name,sum(visits),(case when max(cnt) then floor end) as floor, STRING(distinct resources,',') as res from(select name,count(1) as visits,floor,count(floor) as cnt,resources from entries group by name,floor)aa group by name;

  • @sanyamgarg8288
    @sanyamgarg828811 ай бұрын

    Thanks brother!!!!!! For these videos.

  • @ankitbansal6

    @ankitbansal6

    11 ай бұрын

    My pleasure!!

  • @SudhirKumar-rl4wt
    @SudhirKumar-rl4wt2 жыл бұрын

    I initially tried below query (wanted to use window functions instead of group by) but it gives duplicate rows. Can you please help me correct that? Distinct is something if required then I think there's some issue in the logic. with temp as ( select name, count(floor) over(partition by name) vis_cnt, floor, count(floor) over(partition by name,floor) vis_cnt_per_emp_per_flr, resources, listagg(distinct resources,',') over(partition by name) resoueces_used from input ), temp1 as ( select name, vis_cnt, floor, dense_rank() over(partition by name order by vis_cnt_per_emp_per_flr desc) rnk , resoueces_used from temp ) select name,vis_cnt,floor,resoueces_used from temp1 where rnk=1;

  • @exanode

    @exanode

    Жыл бұрын

    Here's my query. --Just use a subquery for listagg WITH RESOURCES_LIST AS ( SELECT NAME, LISTAGG(RESOURCES, ',') WITHIN GROUP (ORDER BY RESOURCES) RESOURCES FROM( SELECT DISTINCT NAME,RESOURCES FROM ENTRIES GROUP BY NAME, RESOURCES ) GROUP BY NAME ), MOST_VISITED_FLOOR AS ( SELECT NAME, FLOOR FROM ( SELECT NAME,FLOOR, DENSE_RANK() OVER(PARTITION BY NAME ORDER BY COUNT(FLOOR) DESC) 1_IF_MOST_VISITED_FLOOR FROM ENTRIES GROUP BY NAME,FLOOR ) WHERE 1_IF_MOST_VISITED_FLOOR = 1 ) SELECT E.NAME, COUNT(E.NAME) VISITS,RL.RESOURCES,MVF.FLOOR FROM ENTRIES E INNER JOIN RESOURCES_LIST RL ON E.NAME = RL.NAME INNER JOIN MOST_VISITED_FLOOR MVF ON MVF.NAME = E.NAME GROUP BY E.NAME,RL.RESOURCES, MVF.FLOOR ;

  • @nishchaysharma5904
    @nishchaysharma59042 жыл бұрын

    Hi Ankit, thanks for the question. Below is my solution - WITH CTE_floor_visit AS ( SELECT name, floor, RANK() OVER(PARTITION by name ORDER BY COUNT(1) DESC) as Floor_Rank FROM entries GROUP BY name, floor), CTE_floor_resources AS ( SELECT name, COUNT(1) as total_visits, GROUP_CONCAT(DISTINCT resources) AS resources_used FROM entries GROUP BY name ) SELECT t1.name, t2.total_visits, t1.floor AS most_visited_floor, t2.resources_used FROM CTE_floor_visit t1 JOIN CTE_floor_resources t2 on t1.name = t2.name WHERE Floor_Rank = 1

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thanks for posting 👏

  • @satyajitbiswal6162

    @satyajitbiswal6162

    Жыл бұрын

    nice one

  • @gothams1195
    @gothams1195 Жыл бұрын

    with cte as ( select name, sum(case when floor=1 then 1 else 0 end) as 1st, sum(case when floor=2 then 1 else 0 end) as 2nd, count(1) as total_visits, group_concat(distinct(resources)) as total_resources from entries group by name ) select name, case when 1st>2nd then 1 else 2 end as m, total_visits, total_resources from cte

  • @ft_peakhd2921

    @ft_peakhd2921

    Жыл бұрын

    Best one yet! very good solution bro

  • @satyajitbiswal6162

    @satyajitbiswal6162

    Жыл бұрын

    this logic is not good as because we are using hardcorded.if 2/3 more input will come we need to change the querry

  • @mohammedfaisal2668
    @mohammedfaisal2668 Жыл бұрын

    Hello All , String_agg function is not working in SSMS , it is being displayed as "No such inbuilt function Available", can anyone plz guide me on this. Thanks

  • @revathigangisetty5734
    @revathigangisetty5734 Жыл бұрын

    GROUP_CONCAT also tried its saying not a built in function.

  • @Aman-lv2ee
    @Aman-lv2ee3 ай бұрын

    with entries_cte as ( select name, floor, resources, count(name)over(partition by name) as total_count, count(name)over(partition by name, floor) as floor_count, listagg(distinct resources, ',') over(partition by name) as resources_used from entries ) select name,total_count,floor,resources_used, row_number()over(partition by name order by floor_count desc) as rn from entries_cte qualify rn = 1

  • @amitsaurabh9948
    @amitsaurabh9948 Жыл бұрын

    hello sir, i have some questions from your test. I can mail you those. can you please solve them in a video. Atleast a few of them

  • @nihal2725
    @nihal2725 Жыл бұрын

    Easy to read solution: with visits as (select name,floor, count(1) as no_of_floor_visits, rank()over(partition by name order by count(1) desc) as rankd from entries group by name,floor) select v.name,total_visits,floor as most_visited_floor,used_resources from visits v join (select name,string_agg(resources,',') as used_resources from (select distinct name,resources from entries)x group by x.name)B on v.name=B.name join (select name,count(*) as total_visits from entries group by name)C on v.name=C.name where rankd=1

  • @exam_prep_cat5605
    @exam_prep_cat56052 жыл бұрын

    with floor_visits as (select * from (select name ,floor, row_number() over(partition by name order by floor_visit_count desc ) as rk from (select name , floor, count(*) as floor_visit_count from entries group by 1,2)t)temp where rk=1) select name ,(select floor from floor_visits where name=entries.name ) as _most_floor_visited ,count(*) as freq_visit, group_concat(distinct resources) as resources_used from entries group by 1

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thanks for posting 👏

  • @stat_life
    @stat_life Жыл бұрын

    #easiest #solution #mysql with cte as( select *, row_number() over(order by floor desc) as most_visited_floor from entries ) select name, count(name) as total_visits, most_visited_floor, group_concat(distinct resources) as resouces_used from cte group by 1

  • @vikhyatjalota2213
    @vikhyatjalota2213Ай бұрын

    My Solution : with cte_1 as ( select name,count(name) as total_visits, group_concat(distinct resources) as resources from entries group by name), cte_2 as( select name,floor as max_visited_floor from( select floor,name,count(floor) as floors,dense_rank() over(partition by name order by count(floor) desc) as rnk from entries group by 1,2) new where rnk =1 ) select name,total_visits,max_visited_floor,resources from cte_1 join cte_2 using(name)

  • @AsifAnas-nu6iu
    @AsifAnas-nu6iu Жыл бұрын

    SELECT name, count(*) as number_of_visit, group_concat(distinct resources) as used_resources FROM entries group by name;

  • @satyajitbiswal6162

    @satyajitbiswal6162

    Жыл бұрын

    left one column that most_visited_floor

  • @anirvansen5024
    @anirvansen50242 жыл бұрын

    Another MYSQL Solution with base as ( select *,count(1) over(partition by name,floor) as repeatation from entries ), visited_floor as ( select *,first_value(floor) over(partition by name order by repeatation desc) as most_visited_floor from base order by name ) select name, count(1) as no_of_visits,most_visited_floor,group_concat(distinct resources) as resources_used from visited_floor group by name,most_visited_floor;

  • @HarshRaj-tm2vl
    @HarshRaj-tm2vl3 ай бұрын

    hi, Can anyone help me understand, how long should it take to solve these Questions in Interviews.

  • @2412_Sujoy_Das
    @2412_Sujoy_Das8 ай бұрын

    Ankit Sir..... is there any condition or a rule perhaps that using too much cte will lead to bulky queries??

  • @ankitbansal6

    @ankitbansal6

    8 ай бұрын

    If data volume is high try temp tables

  • @shahinulhaque6320
    @shahinulhaque63202 жыл бұрын

    Select T1.name, floor most_vistied_floor, t2.total_visits, t2.resources_used from ( Select name, floor, rank() over(partition by name order by count(1) desc) as tt from entries group by name, floor ) as T1 inner join ( Select name, count(1) total_visits, STRING_AGG(resources, ',') as resources_used from entries group by name) T2 on T1.name = T2.name Where tt = 1 order by T1.name Great videos you make. Keep going please !

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thank you 😊

  • @user-zx1ii2cx2j
    @user-zx1ii2cx2j Жыл бұрын

    (enjoy my simple ans) with cte_1 as (select name,count(*) no_of_times_visited, group_concat(distinct resources) resources_used from entries group by name), cte2 as (select name,floor,max(c) from (select name,floor,count(*) c from entries group by name,floor order by count(*) desc) a group by name) select cte_1.*,floor from cte_1 join cte2 using (name)

  • @Abhisheksingh-vd6yo
    @Abhisheksingh-vd6yoАй бұрын

    with cte_rank as (select name, floor, count(floor) as most_visited from entries group by name,floor ) select e.name,count(e.name) as total_visit,i.floor as most_visited_floor,group_concat(distinct e.resources) from entries as e inner join (select name,floor from (select *,Rank() over(partition by name order by most_visited desc) as rnk from cte_rank)as r where r.rnk=1) as i on i.name=e.name group BY e.name,i.floor;

  • @Gaurav-wy2wm
    @Gaurav-wy2wm11 ай бұрын

    This is my solution :- select name,floor,count(floor) as floor_count,group_concat(distinct resources) as resou from entries group by name ;

  • @ankitbansal6

    @ankitbansal6

    11 ай бұрын

    We need the most visited floor

  • @swamybittu7548
    @swamybittu754811 ай бұрын

    I feel rank is giving just based on the name but not based on how many times he visited the floor

  • @amithgshetty8171

    @amithgshetty8171

    2 ай бұрын

    Rank needs to be applied on the count of floors visited in desc order

  • @SaurabhKumar-ic7nt
    @SaurabhKumar-ic7nt Жыл бұрын

    with temp as(select name, floor, count(1) as total_visit_per_floor, dense_rank() over(partition by name order by count(1) desc) as rn From entries group by name, floor) select t.*, temp.floor as most_visited_floor from temp, (Select name, count(1) as total_visits, group_concat(distinct resources order by resources separator ',') as resources_used from entries group by name) as t where t.name = temp.name and temp.rn = 1;

  • @manishchauhan5625
    @manishchauhan562510 ай бұрын

    Is there any specific reason for using count(1) instead of count(*)?

  • @ankitbansal6

    @ankitbansal6

    10 ай бұрын

    It's the same. Anything is fine.

  • @somyasingh6968
    @somyasingh6968 Жыл бұрын

    I think this will also work: select name,count(*) as total_visits, (select e1.floor from entries e1 where e1.name=e.name group by floor order by count(e1.floor) desc limit 1) as frequent_floor ,group_concat(distinct resources) as used_resources from entries e group by name;

  • @playtrip7528

    @playtrip7528

    Жыл бұрын

    nice solution, can u pls elaborate (select e1.floor from entries e1 where e1.name=e.name group by floor order by count(e1.floor) desc limit 1) as frequent_floor

  • @shrinjaysoni99

    @shrinjaysoni99

    Жыл бұрын

    grt!!

  • @jayosreedas7673
    @jayosreedas76733 ай бұрын

    I tried to solve the query using following approach with alias as (SELECT DISTINCT NAME, COUNT(*) as total_visits FROM EMP GROUP BY NAME), alias2 as (select *, group_concat(distinct resources order by resources) as resources_used from emp group by name) select name,total_visits,floor as most_vistited_floor,resources_used from( select x.*,a.total_visits,a2.resources_used from ( select *, count(*) as counts from emp group by name, floor) as x join alias a on x.name=a.name join alias2 a2 on x.name=a2.name group by x.name having counts=max(counts) ) as y; let me know if you think this approach will be effective with larger datasets, or do you foresee any potential limitations.

  • @shankrukulkarni3234
    @shankrukulkarni3234 Жыл бұрын

    with tmp_tbl1 as ( select name,floor as most_visited_floor from (select name,floor,count(*), rank() over(partition by name order by count(*) desc) as rn from entries group by name,floor)a where rn=1), tmp_tbl2 as ( select name,group_concat(distinct resources) as resource_used, count(*) as total_visited from entries group by name) select a.name, b.total_visited, a.most_visited_floor, b.resource_used from tmp_tbl1 a inner join tmp_tbl2 b on a.name=b.name; using group_concat(distinct resources) in mysql

  • @rishabhagarwal4164
    @rishabhagarwal41642 жыл бұрын

    with temp as (select * , count(*) as floor_visit from entries group by name, floor), temp2 as (select name, group_concat( distinct resources) as resources_used from entries group by name) select t.name, sum(t.floor_visit) as total_visit, (case when t.floor_visit = max(t.floor_visit) then t.floor else 0 end) as most_visit, t2. resources_used from temp t inner join temp2 t2 on t.name = t2.name group by name;

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thanks for posting 👏

  • @satyajitbiswal6162

    @satyajitbiswal6162

    Жыл бұрын

    WOULD IT WORK t.floor_visit = max(t.floor_visit)?

  • @sivachaitanya6330
    @sivachaitanya6330 Жыл бұрын

    how to select max visited floor with out using rank funtion.......?

  • @playwithvihaan7601

    @playwithvihaan7601

    Жыл бұрын

    use co-related nested query.

  • @sathwikdhanya5400
    @sathwikdhanya5400 Жыл бұрын

    with gr as ( select name,floor,count(1) as no_visit, STRING_AGG(resources,',') as res from entries group by name ,floor ), rnked as (select *, sum(no_visit) over(partition by name) as tot_vis, rank() over(partition by name order by no_visit) as rnk from gr), dis_res as (select distinct name,resources from entries), res_agg as (select name , STRING_AGG(resources,',') as res from dis_res group by name) select a.name,a.floor as most_visited_floor,a.tot_vis as total_visits,b.res from rnked a INNER JOIN res_agg b on a.name=b.name where rnk=(select max(rnk) from rnked)

  • @vinayghildiyal2727
    @vinayghildiyal2727 Жыл бұрын

    with visits as ( select name,floor as most_visited_floor, rank() over(partition by name order by count(*) desc) as rnk from entries group by name,floor ),cte as ( select name,count(*) as total_visits,group_concat(distinct resources) as resources_used from entries group by name ) select c.name,c.total_visits,v.most_visited_floor,c.resources_used from cte as c inner join visits as v on c.name = v.name and v.rnk=1;

  • @anuragshukla9480
    @anuragshukla9480 Жыл бұрын

    I tried this Solution, Select name, count(name), case when count(floor)>1 then floor else 0 end as most_visited_floor, group_concat(resources) as resources_used from entries group by name

  • @psyche5184
    @psyche5184 Жыл бұрын

    I mostly work with Impala/Hive, so here is a solution: select t3.name, t3.total_visits, t3.floor,t4.resources_used from (select * from (select *, row_number() over(partition by name order by floor_visits desc) as rn from (select name, count() over(partition by name) as total_visits, count() over(partition by name,floor) as floor_visits, resources,floor from entries) t1)t2 where rn=1) t3 left join (select name, group_concat(distinct resources) as resources_used from entries group by 1)t4 on t3.name=t4.name;

  • @siddheshkalgaonkar2752
    @siddheshkalgaonkar27522 жыл бұрын

    I developed a solution without looking at yours. Great question though. The below snippet works with Postgres. The only change required is to replace the string_agg method with group_concat while running on MySQL. with except_most_visited_floor as( select name, count(*) as no_of_visits, string_agg(distinct resources,',') as resources_used from entries group by name ), only_most_visited_floor as ( select name,floor from entries group by name,floor having count(floor) > 1 ), res as ( select a.name,a.no_of_visits as total_visits,b.floor as most_visited_floor, a.resources_used from except_most_visited_floor a join only_most_visited_floor b on a.name=b.name ) select name, total_visits, most_visited_floor,resources_used from res;

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Thanks for posting. Good stuff 👏

  • @SS-lt1nf

    @SS-lt1nf

    2 жыл бұрын

    Would this work if the person visited say floor 2 twice and floor 3 thrice?

  • @siddheshkalgaonkar2752

    @siddheshkalgaonkar2752

    2 жыл бұрын

    @@SS-lt1nf Yes, I don't see a problem here. I am calculating it separately and then joining it so the number of visits is handled in a separate block.

  • @satyajitbiswal6162

    @satyajitbiswal6162

    Жыл бұрын

    @@siddheshkalgaonkar2752 no it will not work if the person visited say floor 2 twice and floor 3 thrice .then we need to change the querry

  • @siddheshkalgaonkar2752

    @siddheshkalgaonkar2752

    Жыл бұрын

    @@satyajitbiswal6162 Can you show me how it will not work? or with an example considering my solution?

  • @abhishekp7375
    @abhishekp7375 Жыл бұрын

    with cte as ( select * ,count(*) over(partition by name) as total_visits --to find how many times each floor the person visited ,row_number() over(partition by name,floor order by floor) as rn ,concat_ws(',',collect_set(resources) over(partition by name)) as resources_used from entries ) ,cte2 as ( select distinct name ,total_visits ,floor ,rn --to find max among each floor he visited ,max(rn) over(partition by name,floor) as max --to find max among all floor which he visited ,max(rn) over(partition by name) as person_max ,resources_used from cte ) select distinct name ,total_visits ,floor as most_visted_floor ,resources_used from cte2 where max == person_max

  • @devangsareen8755
    @devangsareen8755 Жыл бұрын

    this is mysql version select name,count(email)as total_visits,max(floor)as most_floor_visited,distinct(resources) from employee groupby name;

  • @yatinshekhar787
    @yatinshekhar78710 ай бұрын

    6/141

  • @mrs.vadivuvigram6397
    @mrs.vadivuvigram6397 Жыл бұрын

    select Name,visited,Resources_used, case when visited>1 then floor else 0 end as freq_visited from (select name,floor,count(*) as visited,group_concat( distinct resources) as Resources_used from entries group by name)a group by name;

  • @mrs.vadivuvigram6397

    @mrs.vadivuvigram6397

    Жыл бұрын

    select name,max(floor) as frequent_visit,count(*) as total_visited, group_concat( distinct resources) as Resources_used from entries group by name;

  • @jimitshah7636
    @jimitshah7636Ай бұрын

    select name, count(*) --times visited each floor, mode(floor), --if you recall mean,median,MODE GROUP_CONCAT(DISTINCT resources) --distinct to remove dups from entries group by name;

  • @rajunaik8803
    @rajunaik8803 Жыл бұрын

    **without distinct resources used** with cte2 as( select name,count(*) as total_visits from entries GROUP by name ), cte1 as( select name,floor,count(1) as no_of_floor_visit,rank() over(partition by name order by count(1) desc) as rnk from entries GROUP by name,floor ), cte3 as (select name,STRING_AGG(resources,',') as resources_used from entries group by name) select cte1.name,cte1.floor as most_visted_floor,cte2.total_visits,cte3.resources_used from cte1 as cte1,cte2 as cte2,cte3 as cte3 where cte1.name=cte2.name and cte1.rnk=1 and cte3.name=cte1.name

  • @msd7451
    @msd74518 ай бұрын

    with table1 as ( select t.name,t.floor as most_visited_floor from (select e.name,e.floor,count(*) as count_floor_visited, row_number() over(partition by e.name order by count(*) desc) as rn from entries as e group by e.name,e.floor ) as t where t.rn=1 ), table2 as ( select `name`,count(*) as total_visits,group_concat(distinct resources) as resources from entries group by `name`) select t1.name,t2.total_visits,t1.most_visited_floor ,t2.resources from table1 as t1 join table2 as t2 on t1.name=t2.name

  • @prabhatgupta6415
    @prabhatgupta6415 Жыл бұрын

    with a as (select name ,count(name) as total_visits from entries group by name), b as(select name,floor as most_visited_floor from entries group by name,floor having count(floor) >1), c as (select name,group_concat(distinct resources) as resources_used from entries group by name) select a.name,total_visits ,most_visited_floor,resources_used from a join b on a.name=b.name join c on a.name=c.name

  • @satyajitbiswal6162

    @satyajitbiswal6162

    Жыл бұрын

    Would this work if the person visited floor 2 twice and floor 3 thrice?

  • @shubhamsaluja8173
    @shubhamsaluja81732 жыл бұрын

    IN POSTGRES this DISTINCT is working with STRING AGG. select entries.name,count(*) as total_visits, STRING_AGG(DISTINCT entries.resources,',') as resources_used,t2.floor as max_floor from entries JOIN t2 on entries.name=t2.name WHERE t2.rnk=1 Group by entries.name,t2.floor order by entries.name asc create temporary table t2 as (select e.name,count(e.*) as no_of_floor_visits,e.floor, rank() over(partition by e.name order by count(e.*) desc) as rnk From entries e group by e.name,e.floor)

  • @kaushalyadav6553
    @kaushalyadav655311 ай бұрын

    here is solution for MYSQL - select name, sum(visitCount) as total_visit, case when visitCount = max(visitCount) then floor end as most_visited_floor, GROUP_CONCAT (resources ) as resources from ( select name,floor,count(1) as visitCount,GROUP_CONCAT ( distinct resources ) as resources from Entries group by name,floor )data group by name;

  • @Hope-xb5jv
    @Hope-xb5jv3 ай бұрын

    with cte as ( select name, floor, count(floor) as visit from entries group by name,floor ) select a.name, c.Total_visit, a.Most_visited_floor, b.Resource_used from (select name, STRING_AGG(resources,',') as Resource_used from ( select distinct name,resources from entries) as query group by name) b join (select name, floor as Most_visited_floor from cte where visit = (select max(visit) from cte) group by name,floor ) a on a.name = b.name join (select name, count(Name) as Total_visit from entries group by name) c on a.name = c.name

  • @blazerider200
    @blazerider200 Жыл бұрын

    Simplest Solution Available: with abc as( select name ,floor,count(floor) as floor_visited_count,resources as resources_used from entries group by name,floor,resources) select name,floor as most_visited_floor,sum(floor_visited_count) as total_visits,GROUP_CONCAT(resources_used,',') as resources_using from abc group by name having max(floor_visited_count)

  • @user-sv6hh4gt4z

    @user-sv6hh4gt4z

    6 ай бұрын

    is string_agg and group_concat the same thing, In my case string_agg is not working I do not why

  • @sinhaapurva25
    @sinhaapurva252 жыл бұрын

    select Y.name, Y.total_visits, X.most_visited_floor, Y.resources_used from (select * from (select name,floor as most_visited_floor,count(floor) as count_of_most_visited_floor from entries group by name,floor) S group by S.name) X join (select name, count(*) as total_visits, group_concat(distinct resources) as resources_used from entries group by name) Y on X.name = Y.name;

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    Where is the filter for most visited floor ?

  • @sinhaapurva25

    @sinhaapurva25

    2 жыл бұрын

    @@ankitbansal6 It's in the lines 6-10. I have created two tables. First found out the most_visited_floor for each name and created a table, aliased as X. And a second table to get the rest of the columns, aliased as Y. Then I have joined these two tables on the name column to obtain the final table.

  • @ankitbansal6

    @ankitbansal6

    2 жыл бұрын

    You have taken the count but where you are filtering for most visited floor by each person..

  • @sinhaapurva25

    @sinhaapurva25

    2 жыл бұрын

    @@ankitbansal6 Got you. Thanks. I inserted another name 'C' to get this right. I used join on join on join. select Y.name, Y.total_visits, X.most_visited_floor, Y.resources_used from (select S1.name, S1.visited_floor as most_visited_floor from (select name, floor as visited_floor, count(floor) as number_of_times from entries group by name,floor) S1 join (select name, max(S.number_of_times) as number_of_times from (select name, floor as visited_floor, count(floor) as number_of_times from entries group by name,floor) S group by S.name) S2 on S1.number_of_times = S2.number_of_times where S1.name = S2.name) X join (select name, count(*) as total_visits, group_concat(distinct resources) as resources_used from entries group by name) Y on X.name = Y.name;