azarasi / LeetCode 1501. Countries You Can Safely Invest In

Created Sat, 14 May 2022 14:19:51 +0800 Modified Wed, 18 Sep 2024 14:00:22 +0000

Table Person:

Column NameType
idint
namevarchar
phone_numbervarchar

id is the primary key for this table. Each row of this table contains the name of a person and their phone number. Phone number will be in the form ‘xxx-yyyyyyy’ where xxx is the country code (3 characters) and yyyyyyy is the phone number (7 characters) where x and y are digits. Both can contain leading zeros.

Table Country:

Column NameType
namevarchar
country_codevarchar

country_code is the primary key for this table. Each row of this table contains the country name and its code. country_code will be in the form ‘xxx’ where x is digits.

Table Calls:

Column NameType
caller_idint
callee_idint
durationint

There is no primary key for this table, it may contain duplicates. Each row of this table contains the caller id, callee id and the duration of the call in minutes. caller_id != callee_id

A telecommunications company wants to invest in new countries. The company 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.

Write an SQL query to find the countries where this company can invest.

Return the result table in any order.

The query result format is in the following example.

Example 1:

Input: Person table:

idnamephone_number
3Jonathan051-1234567
12Elvis051-7654321
1Moncef212-1234567
2Maroua212-6523651
7Meir972-1234567
9Rachel972-0011100

Country table:

namecountry_code
Peru051
Israel972
Morocco212
Germany049
Ethiopia251

Calls table:

caller_idcallee_idduration
1933
294
1259
312102
312330
1235
7913
713
971
177

Output:

country
Peru

Explanation: The average call duration for Peru is (102 + 102 + 330 + 330 + 5 + 5) / 6 = 145.666667

The average call duration for Israel is (33 + 4 + 13 + 13 + 3 + 1 + 1 + 7) / 8 = 9.37500

The average call duration for Morocco is (33 + 4 + 59 + 59 + 3 + 7) / 6 = 27.5000

Global call duration average = (2 * (33 + 4 + 59 + 102 + 330 + 5 + 13 + 3 + 1 + 7)) / 20 = 55.70000

Since Peru is the only country where the average call duration is greater than the global average, it is the only recommended country.

Data

Create table If Not Exists Person
(
    id           int,
    name         varchar(15),
    phone_number varchar(11)
);
Create table If Not Exists Country
(
    name         varchar(15),
    country_code varchar(3)
);
Create table If Not Exists Calls
(
    caller_id int,
    callee_id int,
    duration  int
);
Truncate table Person;
insert into Person (id, name, phone_number)
values ('3', 'Jonathan', '051-1234567');
insert into Person (id, name, phone_number)
values ('12', 'Elvis', '051-7654321');
insert into Person (id, name, phone_number)
values ('1', 'Moncef', '212-1234567');
insert into Person (id, name, phone_number)
values ('2', 'Maroua', '212-6523651');
insert into Person (id, name, phone_number)
values ('7', 'Meir', '972-1234567');
insert into Person (id, name, phone_number)
values ('9', 'Rachel', '972-0011100');
Truncate table Country;
insert into Country (name, country_code)
values ('Peru', '051');
insert into Country (name, country_code)
values ('Israel', '972');
insert into Country (name, country_code)
values ('Morocco', '212');
insert into Country (name, country_code)
values ('Germany', '049');
insert into Country (name, country_code)
values ('Ethiopia', '251');
Truncate table Calls;
insert into Calls (caller_id, callee_id, duration)
values ('1', '9', '33');
insert into Calls (caller_id, callee_id, duration)
values ('2', '9', '4');
insert into Calls (caller_id, callee_id, duration)
values ('1', '2', '59');
insert into Calls (caller_id, callee_id, duration)
values ('3', '12', '102');
insert into Calls (caller_id, callee_id, duration)
values ('3', '12', '330');
insert into Calls (caller_id, callee_id, duration)
values ('12', '3', '5');
insert into Calls (caller_id, callee_id, duration)
values ('7', '9', '13');
insert into Calls (caller_id, callee_id, duration)
values ('7', '1', '3');
insert into Calls (caller_id, callee_id, duration)
values ('9', '7', '1');
insert into Calls (caller_id, callee_id, duration)
values ('1', '7', '7');

Solution

SELECT c.name AS country
FROM Calls,
     Person,
     Country c
WHERE (
            caller_id = id
        OR callee_id = id
    )
  AND country_code = LEFT(phone_number, 3)
GROUP BY country_code
HAVING AVG(duration) > (SELECT AVG(duration)
                        FROM Calls);