LeetCode 1699: Number of Calls Between Two Persons [SQL]

preview_player
Показать описание
Solution and walkthrough of leetcode database problem 1699: Number of Calls Between Two Persons. I'm using MySQL but this solution should work in any SQL dialect such as PostgreSQL, MS SQL Server, Oracle etc.

Playlists:

Рекомендации по теме
Комментарии
Автор

This was asked of me in interview and cost me. Now, I know! Thx Fred

ThinkBigToday
Автор

Hey Fred
I have finished watching the entire Leetcode SQL playlist now, this a gold mine for SQL prep. simplified explanation and huge variety of queries
Thanks a ton!! Excellent job done here

mandeeplakra
Автор

Maaaan, that was a neat little solution. Thank you! And thank you for all these SQL exercises you've been sharing. Super useful and quite fun. ^^

ZzZzZiggy
Автор

Hey Fred
Thanks for sharing this method, I just a little bit confused about the order of execution, why we can use person 1 and person 2 in select clause as condition of group by? cause as far as I know, sql should execute from and group then last for SELECT is that right?

yuxinyang
Автор

this is very helpful, thanks for sharing

sachin-bcm
Автор

They asked me the same question in my interview and i failed to answer… but happy to know now…Thanks, , , ,

yourtechiefriend
Автор

Two self joins one on from and to, other between from=to and to= from and then aggregating might work

ajaxaj
Автор

Great to know those two functions!!
btw, I just finished your previous video, calculation salaries, and does it work for this question too by create one column like below?
I dont have Leetcode prime, do you mind take a look? Thanks!

Select from_id as person1, to_id as person2, count(selectedcalls.selected) as call_count, sum(duration) as total_duration
From
(Select from_id, to_id, duration, If(from_id < to_id, yes, no) as selected
From Calls
Group by from_id, to_id) selectedcalls
Where selected = ‘yes’

haowang
Автор

Would you be able to also solve it with UNION?

malikabaymuradova
Автор

This was really nice, and thanks for those two new functions :)

akritisaxena
Автор

ooo didn't know about those functions either. thanks man!

golammuhaimeen