REAL SQL Interview PROBLEM by Capgemini | Solving SQL Queries
Вставка
- Опубліковано 21 тра 2024
- In this video, I solve a REAL SQL Interview PROBLEM asked by Capgemini. This problem was shared with me by someone who took this interview.
Upcoming Data Science Bootcamp on OdinSchool:
hubs.la/Q02y4C_d0
In this problem, we are given information about Lifts and Passengers. We need to come up with the final list of passengers allowed to enter the lift based on the maximum capacity of the lift.
THANK YOU for watching!
I am an Odin student. No placement support nothing. It's been 3 months since I completed the course. Not a single vacancy I got in placement assistance. Anyway you are the best teacher after completing the course still clearing doubts from your videos.
This is called real facts 😂😂😂
people from iit, even after completing full time course are not getting placed. Doing these online course will not help. There was a time in the past when these quick certifications would work, but right now its not possible
Which Batch DS3A😄
What they r telling regarding job assistance?
Hi can we connect? I'm also from odinschool
Thanks ! You are back with new challenge , Thank you for the video, everytime I learn something new or different aporoach solving queries, thank you very much.
Glad to see you back again sir 😌
Thoufiq you are the real mentor good teacher waiting for more videos and live advance SQL classes.😊
Explanation is awesome 💯
Well explained! Intuitive and to the point
For my fellow Americans Lift = Elevator. 😀
Welcome back Thoufiq!!!
create table lifts
(
id int
, capacity_kg int
);
insert into lifts values (1, 300);
insert into lifts values (2, 350);
create table lift_passengers
(
passenger_name varchar(50)
, weight_kg int
, lift_id int
);
insert into lift_passengers values ('Rahul', 85, 1);
insert into lift_passengers values ('Adarsh', 73, 1);
insert into lift_passengers values ('Riti', 95, 1);
insert into lift_passengers values ('Dheeraj', 80, 1);
insert into lift_passengers values ('Vimal', 83, 2);
insert into lift_passengers values ('Neha', 77, 2);
insert into lift_passengers values ('Priti', 73, 2);
insert into lift_passengers values ('Himanshi', 85, 2);
Thanks for providing script. This will definitely help most of us
thank you for script
Thoughtful and much needed
Thank you so much :)
You're an angel
Welcome back sir
Thank you :)
I miss your videos. Thank you for this one.
Thanks for the video ❤
Thanks for the great video 🎉
Could you please do a video where you explain the select statement or maybe the aggregates with group by and having ? Thank you for your work 🙏🏼
Welcome back Touafiq
Thank you :)
Hi Thoufiq , I have been following your channel from past 1.5years it was helped me lot for sql and absolutely you are doing a great job .we all knew how much effort you are putting to create this series for sql and please try to create the same series for python also.Thank you so much for your hardwork☺️.
Thanks Anusha .. appreciate your kind words.. I’ll think about your suggestions 👍
Let's begin
Thank you bro👌
Good job
Ms sql:
with cte as(
select p.* ,l.weight, case when sum(weight_kg) over(partition by p.lift_id order by weight_kg) < l.weight then 1 else 0 end flag
from passengers p
inner join
lift l
on l.lift_id=p.lift_id)
select lift_id, string_agg(passengername , ',') name
from cte
where flag=1
group by lift_id
order by lift_id;
with cte as
(select * ,
sum(weight_kg) over(partition by lift_id order by weight_kg) runing
from lift_passengers)
select c.lift_id,group_concat(c.passenger_name) as passenger
from cte as c
join lifts as l
on c.lift_id = l.id and l.capacity_kg >= c.runing
group by c.lift_id
Hi Sir,
Thank you for your videos
hereby sharing my solution
with cte as (
select *,sum(weight_kg) over(partition by lift_id order by weight_kg) as running_weight from lift_passengers P
join lifts L
on p.lift_id =l.id
)
select lift_id
,STRING_AGG(Passenger_name,',') as list_passenger
from cte
where running_weight
Below method is using CTE and this works in Oracle SQL:
with new_table as (select lp.*,
sum(weight_kg) over (partition by lift_id order by weight_kg asc) as sum_weight
from lift_passengers lp)
select l.id, LISTAGG(nt.PASSENGERS_NAME, ',')
from lift l
inner join new_table nt on nt.sum_weight < l.CAPACITY_KG and nt.lift_id =l.id
group by l.id
Hey Toufiq, if we dont want to use the string_agg function, what other method can we use?
My first thought came to use recurssive
Pyspark Version of this problem :
ua-cam.com/video/0n7aGNVCtRc/v-deo.htmlsi=hnpqw1o3yiNLUWi2
with cte as (select *,
sum(weight_kg) over (partition by lift_id order by weight_kg) as sum_weights
from lift_passengers)
,cte2 as (
select c.passenger_name, l.id,
case when c.sum_weights - l.capacity_kg
Thanks for the Beautiful SQL question. Here is my Solution to the Problem.
WITH CTE as (
select p.*,l.*,SUM(weight_kg) OVER (Partition By id ORDER BY weight_kg) as Cumulative_Sum from lift_passengers p
LEFT JOIN lift l
ON p.lift_id = l.id)
SELECT string_agg(passenger_name,',') as Passenger_Name from (
SELECT *,CASE WHEN Cumulative_Sum
Hi sir,
Could you update if you are working on any sql course like you mentioned in your previous servey?
I have been waiting on update since that post
drop table if exists lift;
create table lift(
id int,
capacity_kg int
);
drop table if exists lift_passengers;
create table lift_passengers(
passenger_name varchar(50),
weight_kg int,
lift_id int
);
insert into lift values (1,300);
insert into lift values (2,350);
insert into lift_passengers values ('Rahul',85,1);
insert into lift_passengers values ('Adarsh',73,1);
insert into lift_passengers values ('Riti',95,1);
insert into lift_passengers values ('Dheeraj',80,1);
insert into lift_passengers values ('Vimal',83,2);
insert into lift_passengers values ('Neha',77,2);
insert into lift_passengers values ('Priti',73,2);
insert into lift_passengers values ('Himanshi',85,2);
select * from lift;
select * from lift_passengers;
Nice one
Thanks 🔥
Capegemini interview very simple I have attended three times cleared three times as well
For which role?
Ouais, on te croit pas gros, sa sent la merde.
Thanks Toufik,
However You considered only one combination but There could be multiple combination from lift 1 of those having sum of weight
+1
A solution with output as you mentioned would go better with the requirement of the question.
with cte1 as (
select *, sum(weight_kg) over(partition by lift_id order by weight_kg) as running_sum
from lift_passengers lp
inner join lift l on lp.lift_id = l.id),
cte2 as (select *, case when capacity_kg>= running_sum then 1 else 0 end as runn_sum
from cte1)
select id , STRING_AGG(passenger_name, ',') as total_pass
from cte2
group by id
with cte as (
select lp.passenger_name,
lp.weight_kg,
sum(weight_kg) over (partition by lift_id order by weight_kg) as cumu_sum,l.capacity_kg,lp.lift_id
from lift_passengers lp
join lifts l on l.id=lp.lift_id
)
select lift_id,string_agg(passenger_name,' , ')
from cte
where cumu_sum
same question in meesho also asked
I would suggest you layoff is going in IT so wait atleast 3 month wherever you r
Good to know you still remember your YT password!
yeah, I am glad too :D
Hi sir , instead of using string_agg shall we use listag is that possible
I too thinking the same
Is this String_agg function will work on all the environments
My solution using SQL Server:
with main as (
select lp.lift_id,
l.capacity_kg,
lp.passenger_name,
weight_kg,
sum(weight_kg) over(partition by lp.lift_id order by weight_kg
range between unbounded preceding and current row ) as Total_Kg
from lifts l inner join lift_passengers lp on l.id=lp.lift_id
),
Overlift_Capacity_check as
(
select * , case when Total_Kg < capacity_kg then 1 else 0 end as Overlift_Flag
from main)
select lift_id,STRING_AGG(passenger_name,',') as passengers
from Overlift_Capacity_check
where Overlift_Flag 0
group by lift_id;
Why do not you teach PL\SQL?
for the sum aggregate function ,
inside over()
with order by clause gives running total
without order by clause gives total sum within the window
why it is so???
Hi taufiq, I didn't get your solution. I'm little confused. Our task was to find the list of passengers who can be accomodated in lift without exceeding lift capacity, right? So, Shouldn't there be all the possible combinations for each lift 1 & 2, for e.g. for lift 1: { rahul, adarsh, riti},{rahul, adarsh,dheeraj},{adarsh, riti, dheeraj},{ rahul, riti, dheeraj} like that but in increasing order of weight?? Please clarify me. Thanks
Exactly, I resonate you.
Just asking this question here (out of curiosity) that, If we have to get all the possible combinations, Is it possible to achieve just by SQL alone? or should we be using any programming language like Python/Java etc? Please can anyone respond from this forum...
+1
Exactly my thought as well when i first read the question. But then why would they give lift id against the passengers. I guess we will need to accommodate Riti (in this case) on a separate row, with lift id 1, which will make more sense (Unlike the sample output).
just for info.... ORDER BY can't use in CTE statement... correct me if i am wrong
Within Post Gre SQL yes, but not SQL Server.
But your also ordering it within the SUM(weight_kg) window function so you should be good either way.
How to retrieve all sets of lift passengers combinations for two sets of given lifts weight?
It's an extension to the question??
how would you estimate the level of this task? (beginner, intermediate etc). thanks
Intermediate
can we solve this problem without using window function
with cte as (
select l.*,lp.*, sum(lp.WeightKG) over(partition by l.id order by l.id,lp.WeightKG) as w from LiftPassengers lp
left join Lift l
on lp.LiftID =l.ID
)
select ID, STRING_AGG(PassengerName, ' , ') as passengers from cte
where w< CapacityKG
group by id
with cte as
(select lift_id,passenger_name,weight_kg,sum(weight_kg) over(partition by lift_id order by weight_kg) as cc, capacity_kg from lift_passengers
join lifts on lift_passengers.lift_id=lifts.id
),
cte2 as(select *,case when cc>capacity_kg then 'n'
else 'y'
end as 'ff'
from cte
)
select lift_id,string_agg(passenger_name,', ') as passengers from cte2 where ff'n' group by lift_id
Hi can you explain about the cursor
How many experience he had on this sql
when i using my phone and iam sleeping ,iam struggled a lot in my area they are create sleeping and they are too much struggle for me and my son.
with cte as(
select *,sum(weight_kg) over(partition by id order by weight_kg) as cum_sum,if(capacity_kg>=sum(weight_kg) over(partition by id order by weight_kg),1,0)as flag
from lift2 as e
join lift_passengers2 as f
on e.id=f.lift_id)
select lift_id,string_agg(passengers_name,',') from cte
where flag=1
group by lift_id;
string_aggregate is not working th sql workbench ,
can we use any alternative other than string_agg() function?
in MYSQL group_concat with group by
Justice for riti 😮
justice for Riti 😢
I am a beginner but in know thw concepts but i am not able to implement plz help me
for MySql
with cte as
(select * ,
sum(weight_kg) over(partition by id order by id,weight_kg) as cummulative_sum,
case when capacity_kg>= sum(weight_kg) over(partition by id order by id,weight_kg)
then 1
else 0
end as flag
from lifts
join lift_passengers
on id=lift_id
order by id,weight_kg)
SELECT lift_id, GROUP_CONCAT(passenger_name ORDER BY passenger_name SEPARATOR ', ') AS passenger
from cte
where flag=1
group by lift_id;
Can any one help me on which SQL, this problem is solved, because i tried with Oracle Sql not able to solve it.
Here is mysolution using MYSQL
with cte as (select lp.passenger_name,lp.lift_id,sum(lp.weight_kg) over (partition by lp.lift_id order by lp.weight_kg) as wt,l.capacity_kg
from lift_passengers lp
left join lifts l on lp.lift_id=l.id)
select lift_id,group_concat(passenger_name) as passenger_name from cte where wt
here is my solution
with CTE as (
select name, weight , liftid, capacity from lift_passengers a join lifts b on a.liftid = b.id)
,CTE2 as (
select name, weight, liftid, capacity, sum(weight) over(partition by liftid order by weight) cumm_weight from CTE)
select liftid, string_agg(name, ',') as passengers from CTE2 where cumm_weight
dataset ?
I am bit late to post answer but enjoy your problems.
i didn't still watch full but here it is my answer:
with cte_1 as (
Select * from (
Select Name,ID,capacity,total, flag_sum,case when flag_sum>capacity then 'N' else 'Y' end as flag from(
Select Name,ID,capacity,total,sum(total) over (partition by id,capacity order by total) as flag_sum from (
Select Name,ID,capacity,weight as total from Lift_passenger join lift
on lift_id=id ) A)B)C where Flag'N')
--Select ID,Name from cte_1;
Select distinct A.ID,STUFF((Select distinct ', '+B.Name from CTE_1 B where A.id=B.id
for XML path (''),TYPE
).value('.', 'NVARCHAR(MAX)'),1,2,'') Passenger from CTE_1 A
Happy Learning☺
Arey ye zindaa hain
with cte as
(select *, sum(weight) over(partition by liftid order by weight) as r
from lift_pas)
select cte.liftid, string_agg(cte.name,',') as pas from cte join lift l on cte.liftid = l.id
and cte.r
Bhai Jan Hindi mein kyon Nahin content banate ho Hindi mein
create table details as (with a as (select * , row_number() over(partition by lift_id order by weight asc) as ranks from passenger)
select * , sum(weight) over(partition by lift_id order by ranks) as running_weight from a );
select * from details;
with a as (
select passenger_name , lift_id from detailsss where running_weight
My solution :-
1)On ms sql server
with t1 as
(select li.id, sum(weight_kg)over(partition by lift_id order by weight_kg) as cum_sum,passenger_name,li.capacity_kg from lift_passengers as lp
join lifts as li on lp.lift_id=li.id)
select id, STRING_AGG(passenger_name,',') as passengers
from t1 where cum_sum
select lp.passenger_name+',', lp.lift_id from lift_passengers lp join lift l on lp.lift_id=l.id where sum(weight_kg)
My Solution
with cte as (
select lp.*, l.capacity_kg
, sum(weight_kg) over(partition by lift_id order by weight_kg) as rollsum
from lift_passengers lp
left join lifts l
on lp.lift_id = l.id )
select lift_id , string_agg(passenger_name, ',')
from cte where capacity_kg >= rollsum
group by lift_id ;
MySql Solution: with cte as (
select passenger_name, weight_kg, lift_id, capacity_kg, sum(weight_kg) over (partition by lift_id rows between unbounded preceding and current row) as cum_sum
from lift_passengers as lp
inner join lifts as l on l.id = lp.lift_id
)
,cte2 as (
select * from cte
where cum_sum < capacity_kg )
select lift_id, group_concat(passenger_name) as passengers from cte2
group by lift_id
select id, group_concat(name) from (
select *, sum(wt) over(partition by id order by wt) sop from (
select lp.name, lp.wt, l.id, l.capacity from lift_passengers lp inner join lift l on l.id = lp.lift_id
)a)b where sop < capacity
group by 1
for mysql
passenger p on l.id = p.lift_id isnt?
May I know why he didn't mentioned their l. p.but still it worked?
It works fine unless there's any ambiguity in the column names. Since they're different, it's okay not to mention the table names.
@@Nnirvana Eye opener, where do I get to know these minor quirks?
@@office4321 I'm not aware of any particular resource, I think it comes with experience. I remember myself too getting amazed at some things which were new to me.
with cte as(
select lift_id,passenger_name from
(select lift_id,capacity_kg, passenger_name,sum(weight_kg)
over( partition by lift_id order by weight_kg)r from lift_passengeres
inner join lift on id=lift_id)
where r
with cte_running_weight as (
select *, sum(weight_kg) over(partition by lift_id order by weight_kg) as cumulative_weight
from lift_passengers lp
)
select lift_id, string_agg(passenger_name,', ' order by weight_kg) as passengers
from cte_running_weight cte
join lifts l on l.id = cte.lift_id and l.capacity_kg>=cumulative_weight
group by lift_id;
WITH tab AS(
SELECT id, passenger_name, capacity_kg,
SUM(weight_kg) OVER(PARTITION BY lift_id ORDER BY weight_kg ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS tot FROM lift_passengers T
JOIN lifts L ON T.lift_id = L.id),
cte_2 AS(
SELECT * FROM (
SELECT id,
CASE WHEN tot< capacity_kg THEN passenger_name ELSE NULL END AS passenger_name FROM tab) AS X
WHERE passenger_name IS NOT NULL)
SELECT id, group_concat(passenger_name) AS passenger_name FROM cte_2
GROUP BY id;
Soln in MYSQL:-
SELECT LIFT_ID , group_concat(PASSENGER_NAME SEPARATOR ' , ') AS PASSENGERS
FROM
(SELECT *,
SUM(WEIGHT_KG) OVER (partition by LIFT_ID order by WEIGHT_KG ) as r_sum
FROM lift_passengers p
JOIN lift l on
p.LIFT_ID = l.id)h
where r_sum < capacity
GROUP BY LIFT_ID;
With CTE AS
(SELECT B.lift_id,B.passenger_name,B.weight_kg,
Sum(B.weight_kg)over(partition by A.id order by B.lift_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as RN
from lifts A left Join lift_passengers B on A.id = B.lift_id),
CTE2 as
(Select lift_id,
(case when RN < 300 and lift_id = 1 then passenger_name
WHEN RN < 350 and lift_id = 2 then passenger_name else NULL END) as RT2 from CTE)
SELECT lift_id, String_agg(RT2, ',') as logo from CTE2
group by lift_id;
with cte as (
select id, lift_id, passenger_name,capacity_kg, weight_kg,sum(weight_kg) over(partition by id order by id, weight_kg rows between unbounded preceding and current row)
as cum_sum
from lift_tst L join lift_passengers_tst P on l.id=p.lift_id
order by id, weight_kg)
select lift_id,listagg(passenger_name,',') as passengers from cte
where cum_sum
Sir i want to talk to you send me your contact. regarding sql queries and sql concepts
with cte as (select e.passenger_name, e.weight_kg,e.lift_id,w.capacity_kg
from lift_passengers as e join lift as w on e.lift_id=w.id
order by lift_id asc),
cte2 as(
select passenger_name, weight_kg,lift_id, sum(weight_kg) over(partition by lift_id order by lift_id desc) as total_weight, capacity_kg,
row_number() over(partition by lift_id order by weight_kg desc) as max_rn,
case when sum(weight_kg) over(partition by lift_id order by lift_id desc)>capacity_kg and row_number() over(partition by lift_id order by weight_kg desc)=1
then 0 else 1 end as flag
from cte group by passenger_name, weight_kg,lift_id, capacity_kg)
--select*from cte2
select lift_id, string_agg(passenger_name,', ') as final_names
from cte2
where flag0
group by lift_id
with new as (select passenger_name, weight_kg, lift_id, capacity_kg,
row_number() over(partition by lift_id order by weight_kg) as rown,
sum(weight_kg) over(partition by lift_id order by weight_kg) cumsum
from lift_passengers lp
join lifts l on l.id = lp.lift_id)
select lift_id, group_concat(passenger_name order by rown SEPARATOR ', ') as passenger_name
from new
where
capacity_kg > cumsum
group by lift_id
WITH cte1 AS (SELECT passenger_name, sum(weight_kg) OVER(PARTITION BY lift_id ORDER BY weight_kg) as cum_sum,lift_id
FROM lift_passengers as lp)
SELECT lift_id, group_concat(passenger_name) FROM cte1 as c
INNER JOIN lifts as l
ON c.lift_id=l.id
WHERE cum_sum
with cte as (
select *,sum(weight_kg) over (partition by lift_id order by weight_kg) chk from lift_passengers a left join lifts b on a.lift_id=b.id)
select id,STRING_AGG(passenger_name,',') as Outputs from cte where chk