Skip to content

Commit cc7b23f

Browse files
authored
Add files via upload
1 parent f983cf1 commit cc7b23f

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
-- Question 110
2+
-- Table Person:
3+
4+
-- +----------------+---------+
5+
-- | Column Name | Type |
6+
-- +----------------+---------+
7+
-- | id | int |
8+
-- | name | varchar |
9+
-- | phone_number | varchar |
10+
-- +----------------+---------+
11+
-- id is the primary key for this table.
12+
-- Each row of this table contains the name of a person and their phone number.
13+
-- Phone number will be in the form 'xxx-yyyyyyy' where xxx is the country code (3 characters) and yyyyyyy is the
14+
-- phone number (7 characters) where x and y are digits. Both can contain leading zeros.
15+
-- Table Country:
16+
17+
-- +----------------+---------+
18+
-- | Column Name | Type |
19+
-- +----------------+---------+
20+
-- | name | varchar |
21+
-- | country_code | varchar |
22+
-- +----------------+---------+
23+
-- country_code is the primary key for this table.
24+
-- Each row of this table contains the country name and its code. country_code will be in the form 'xxx' where x is digits.
25+
26+
27+
-- Table Calls:
28+
29+
-- +-------------+------+
30+
-- | Column Name | Type |
31+
-- +-------------+------+
32+
-- | caller_id | int |
33+
-- | callee_id | int |
34+
-- | duration | int |
35+
-- +-------------+------+
36+
-- There is no primary key for this table, it may contain duplicates.
37+
-- Each row of this table contains the caller id, callee id and the duration of the call in minutes. caller_id != callee_id
38+
-- A telecommunications company wants to invest in new countries. The country intends to invest in the countries where the average call duration of the calls in this country is strictly greater than the global average call duration.
39+
40+
-- Write an SQL query to find the countries where this company can invest.
41+
42+
-- Return the result table in any order.
43+
44+
-- The query result format is in the following example.
45+
46+
-- Person table:
47+
-- +----+----------+--------------+
48+
-- | id | name | phone_number |
49+
-- +----+----------+--------------+
50+
-- | 3 | Jonathan | 051-1234567 |
51+
-- | 12 | Elvis | 051-7654321 |
52+
-- | 1 | Moncef | 212-1234567 |
53+
-- | 2 | Maroua | 212-6523651 |
54+
-- | 7 | Meir | 972-1234567 |
55+
-- | 9 | Rachel | 972-0011100 |
56+
-- +----+----------+--------------+
57+
58+
-- Country table:
59+
-- +----------+--------------+
60+
-- | name | country_code |
61+
-- +----------+--------------+
62+
-- | Peru | 051 |
63+
-- | Israel | 972 |
64+
-- | Morocco | 212 |
65+
-- | Germany | 049 |
66+
-- | Ethiopia | 251 |
67+
-- +----------+--------------+
68+
69+
-- Calls table:
70+
-- +-----------+-----------+----------+
71+
-- | caller_id | callee_id | duration |
72+
-- +-----------+-----------+----------+
73+
-- | 1 | 9 | 33 |
74+
-- | 2 | 9 | 4 |
75+
-- | 1 | 2 | 59 |
76+
-- | 3 | 12 | 102 |
77+
-- | 3 | 12 | 330 |
78+
-- | 12 | 3 | 5 |
79+
-- | 7 | 9 | 13 |
80+
-- | 7 | 1 | 3 |
81+
-- | 9 | 7 | 1 |
82+
-- | 1 | 7 | 7 |
83+
-- +-----------+-----------+----------+
84+
85+
-- Result table:
86+
-- +----------+
87+
-- | country |
88+
-- +----------+
89+
-- | Peru |
90+
-- +----------+
91+
-- The average call duration for Peru is (102 + 102 + 330 + 330 + 5 + 5) / 6 = 145.666667
92+
-- The average call duration for Israel is (33 + 4 + 13 + 13 + 3 + 1 + 1 + 7) / 8 = 9.37500
93+
-- The average call duration for Morocco is (33 + 4 + 59 + 59 + 3 + 7) / 6 = 27.5000
94+
-- Global call duration average = (2 * (33 + 3 + 59 + 102 + 330 + 5 + 13 + 3 + 1 + 7)) / 20 = 55.70000
95+
-- Since Peru is the only country where average call duration is greater than the global average, it's the only recommended country.
96+
97+
-- Solution
98+
with t1 as(
99+
select caller_id as id, duration as total
100+
from
101+
(select caller_id, duration
102+
from calls
103+
union all
104+
select callee_id, duration
105+
from calls) a
106+
)
107+
select name as country
108+
from
109+
(select distinct avg(total) over(partition by code) as avg_call, avg(total) over() as global_avg, c.name
110+
from
111+
((select *, coalesce(total,0) as duration, substring(phone_number from 1 for 3) as code
112+
from person right join t1
113+
using (id)) b
114+
join country c
115+
on c.country_code = b.code)) d
116+
where avg_call > global_avg

0 commit comments

Comments
 (0)