-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path26-01501-countries-you-can-safely-invest-in.sql
44 lines (36 loc) · 1.31 KB
/
26-01501-countries-you-can-safely-invest-in.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-- id_country- get country name for each person by joining on country code
-- id_duration- duration for each person on each call- each person can have multiple rows
-- final query- join these 2 to calculate avg for each country- group by country
-- use having clause to filter avg for country > global avg (calculated using id_duration cte)
with id_country as
(select p.id, c.name as country
from Person p
join Country c
on left(p.phone_number, 3) = c.country_code),
id_duration as
(select caller_id, duration
from Calls
union all
select callee_id, duration
from Calls)
select c.country
from id_country c
join id_duration d
on c.id = d.caller_id
group by c.country
having avg(duration) > (select avg(duration)
from id_duration)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- same as above but concise
-- first cte as above,
-- then join on Calls using OR
select c.name as country
from Country c
join Person p
on c.country_code = left(p.phone_number, 3)
join calls cl
on cl.caller_id = p.id or cl.callee_id = p.id
group by c.name
having avg(duration) > (select avg(duration)
from Calls)
-- no companies listed