Skip to content

Commit 5feae5b

Browse files
authored
Added Medium Solutions
1 parent 832a224 commit 5feae5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2407
-41
lines changed

Medium/Active Businesses.sql

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,43 @@ on e.event_type = b.event) c
5151
where c.occurences>c.average
5252
group by c.business_id
5353
having count(*) > 1
54+
55+
56+
-- My Solution:
57+
**Schema (MySQL v8.0)**
58+
59+
CREATE TABLE Events (
60+
`business_id` INTEGER,
61+
`event_type` VARCHAR(10),
62+
`occurences` INTEGER
63+
);
64+
65+
INSERT INTO Events
66+
(`business_id`, `event_type`, `occurences`)
67+
VALUES
68+
('1', 'reviews', '7'),
69+
('3', 'reviews', '3'),
70+
('1', 'ads', '11'),
71+
('2', 'ads', '7'),
72+
('3', 'ads', '6'),
73+
('1', 'page views', '3'),
74+
('2', 'page views', '12');
75+
76+
---
77+
78+
**Query #1**
79+
80+
select business_id from (
81+
select business_id, event_type, (case when occurences > (select avg(s.occurences) as avg_occurence from Events s
82+
where event_type = e.event_type) then 1 else 0 end ) as num_occurence
83+
from Events e ) s1
84+
group by business_id
85+
having sum(num_occurence) > 1 ;
86+
87+
| business_id |
88+
| ----------- |
89+
| 1 |
90+
91+
---
92+
93+
[View on DB Fiddle](https://www.db-fiddle.com/f/szNMC8LUuNaZhta4mKSaqQ/1)

Medium/Active Users.sql

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,56 @@ select distinct a.id, a.name from t1
7474
inner join accounts a
7575
on t1.id = a.id
7676
where datediff(t1.date_5,login_date) = 4
77-
order by id
77+
order by id
78+
79+
-- My Solution:
80+
**Schema (MySQL v8.0)**
81+
82+
CREATE TABLE Accounts (
83+
`id` INTEGER,
84+
`name` VARCHAR(8)
85+
);
86+
87+
INSERT INTO Accounts
88+
(`id`, `name`)
89+
VALUES
90+
('1', 'Winston'),
91+
('7', 'Jonathan');
92+
93+
CREATE TABLE Logins (
94+
`id` INTEGER,
95+
`login_date` DATETIME
96+
);
97+
98+
INSERT INTO Logins
99+
(`id`, `login_date`)
100+
VALUES
101+
('7', '2020-05-30'),
102+
('1', '2020-05-30'),
103+
('7', '2020-05-31'),
104+
('7', '2020-06-01'),
105+
('7', '2020-06-02'),
106+
('7', '2020-06-02'),
107+
('7', '2020-06-03'),
108+
('1', '2020-06-07'),
109+
('7', '2020-06-10');
110+
111+
---
112+
113+
**Query #1**
114+
115+
With cte as
116+
(select id, login_date, lead(login_date, 4) over(partition by id order by login_date) as lead_date
117+
from ( select distinct * from Logins ) d)
118+
119+
select cte.id, a.name
120+
from cte left join Accounts a on cte.id = a.id
121+
where datediff(cte.lead_date, cte.login_date) = 4;
122+
123+
| id | name |
124+
| --- | -------- |
125+
| 7 | Jonathan |
126+
127+
---
128+
129+
[View on DB Fiddle](https://www.db-fiddle.com/f/6ibFyqXzwhQdpgiZc7Dtmh/1)

Medium/Activity Participants.sql

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,57 @@ select activity
8585
from t1 right join t2
8686
on t1.total = t2.total
8787
where t1.total is null
88-
88+
89+
90+
-- My Solution:
91+
**Schema (MySQL v8.0)**
92+
93+
CREATE TABLE Friends (
94+
`id` INTEGER,
95+
`name` VARCHAR(11),
96+
`activity` VARCHAR(12)
97+
);
98+
99+
INSERT INTO Friends
100+
(`id`, `name`, `activity`)
101+
VALUES
102+
('1', 'Jonathan D.', 'Eating'),
103+
('2', 'Jade W.', 'Singing'),
104+
('3', 'Victor J.', 'Singing'),
105+
('4', 'Elvis Q.', 'Eating'),
106+
('5', 'Daniel A.', 'Eating'),
107+
('6', 'Bob B.', 'Horse Riding');
108+
109+
CREATE TABLE Activities (
110+
`id` INTEGER,
111+
`name` VARCHAR(12)
112+
);
113+
114+
INSERT INTO Activities
115+
(`id`, `name`)
116+
VALUES
117+
('1', 'Eating'),
118+
('2', 'Singing'),
119+
('3', 'Horse Riding');
120+
121+
---
122+
123+
**Query #1**
124+
125+
with cte as ( select f.activity, cnt
126+
from Friends f left join
127+
(select activity, count(1) as cnt from Friends
128+
group by activity) s
129+
on f.activity = s.activity )
130+
131+
select distinct activity from cte
132+
where cnt != (select min(cnt) from cte)
133+
and cnt != (select max(cnt) from cte);
134+
135+
| activity |
136+
| -------- |
137+
| Singing |
138+
139+
---
140+
141+
[View on DB Fiddle](https://www.db-fiddle.com/f/6aH4JYJuAYagoD9VghhH84/1)

Medium/All people report to the given manager.sql

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,51 @@ where manager_id = any (select employee_id
6969
from employees
7070
where manager_id = any (select employee_id
7171
from employees
72-
where manager_id = 1 and employee_id != 1))
72+
where manager_id = 1 and employee_id != 1))
73+
74+
-- My Solution:
75+
**Schema (MySQL v8.0)**
76+
77+
CREATE TABLE Employees (
78+
`employee_id` INTEGER,
79+
`employee_name` VARCHAR(6),
80+
`manager_id` INTEGER
81+
);
82+
83+
INSERT INTO Employees
84+
(`employee_id`, `employee_name`, `manager_id`)
85+
VALUES
86+
('1', 'Boss', '1'),
87+
('3', 'Alice', '3'),
88+
('2', 'Bob', '1'),
89+
('4', 'Daniel', '2'),
90+
('7', 'Luis', '4'),
91+
('8', 'Jhon', '3'),
92+
('9', 'Angela', '8'),
93+
('77', 'Robert', '1');
94+
95+
---
96+
97+
**Query #1**
98+
99+
with cte as
100+
(
101+
select employee_id from Employees where manager_id in (
102+
select employee_id from Employees where manager_id in (
103+
select employee_id from Employees where manager_id = 1 )
104+
)
105+
)
106+
107+
select distinct * from cte
108+
where employee_id != 1;
109+
110+
| employee_id |
111+
| ----------- |
112+
| 2 |
113+
| 4 |
114+
| 7 |
115+
| 77 |
116+
117+
---
118+
119+
[View on DB Fiddle](https://www.db-fiddle.com/f/fFWPuXXsQE4AnByAjuJXGb/1)

Medium/Apples & Oranges.sql

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,48 @@ join
5959
(select sale_date as sale, fruit, sold_num as sold
6060
from sales
6161
where fruit = 'oranges') b
62-
on a.sale_date = b.sale)
62+
on a.sale_date = b.sale)
63+
64+
-- My Solution:
65+
**Schema (MySQL v8.0)**
66+
67+
CREATE TABLE Sales (
68+
`sale_date` DATETIME,
69+
`fruit` VARCHAR(7),
70+
`sold_num` INTEGER
71+
);
72+
73+
INSERT INTO Sales
74+
(`sale_date`, `fruit`, `sold_num`)
75+
VALUES
76+
('2020-05-01', 'apples', '10'),
77+
('2020-05-01', 'oranges', '8'),
78+
('2020-05-02', 'apples', '15'),
79+
('2020-05-02', 'oranges', '15'),
80+
('2020-05-03', 'apples', '20'),
81+
('2020-05-03', 'oranges', '0'),
82+
('2020-05-04', 'apples', '15'),
83+
('2020-05-04', 'oranges', '16');
84+
85+
---
86+
87+
**Query #1**
88+
89+
with cte as (
90+
select sale_date, sold_num - lead(sold_num) over(partition by sale_date order by sale_date) as diff
91+
from Sales )
92+
93+
94+
select * from cte
95+
where diff is not null;
96+
97+
| sale_date | diff |
98+
| ------------------- | ---- |
99+
| 2020-05-01 00:00:00 | 2 |
100+
| 2020-05-02 00:00:00 | 0 |
101+
| 2020-05-03 00:00:00 | 20 |
102+
| 2020-05-04 00:00:00 | -1 |
103+
104+
---
105+
106+
[View on DB Fiddle](https://www.db-fiddle.com/f/kAcURAmMWtQWMB4dctdbMH/1)

Medium/Article Views 2.sql

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,45 @@ select distinct viewer_id as id#, count(distinct article_id) as total
4545
from views
4646
group by viewer_id, view_date
4747
having count(distinct article_id)>1
48-
order by 1
48+
order by 1
49+
50+
-- My Solution:
51+
**Schema (MySQL v8.0)**
52+
53+
CREATE TABLE Views (
54+
`article_id` INTEGER,
55+
`author_id` INTEGER,
56+
`viewer_id` INTEGER,
57+
`view_date` DATETIME
58+
);
59+
60+
INSERT INTO Views
61+
(`article_id`, `author_id`, `viewer_id`, `view_date`)
62+
VALUES
63+
('1', '3', '5', '2019-08-01'),
64+
('3', '4', '5', '2019-08-01'),
65+
('1', '3', '6', '2019-08-02'),
66+
('2', '7', '7', '2019-08-01'),
67+
('2', '7', '6', '2019-08-02'),
68+
('4', '7', '1', '2019-07-22'),
69+
('3', '4', '4', '2019-07-21'),
70+
('3', '4', '4', '2019-07-21');
71+
72+
---
73+
74+
**Query #1**
75+
76+
select viewer_id
77+
from Views
78+
group by viewer_id, view_date
79+
having count(distinct article_id) > 1
80+
order by 1;
81+
82+
| viewer_id |
83+
| --------- |
84+
| 5 |
85+
| 6 |
86+
87+
---
88+
89+
[View on DB Fiddle](https://www.db-fiddle.com/f/5rD5RTwUAwrVMJZu8fADFX/1)

Medium/Calculate Salaries.sql

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,59 @@ case when t1.maximum<1000 then t1.sa
7171
when t1.maximum between 1000 and 10000 then round(t1.sa*.76,0)
7272
else round(t1.sa*.51,0)
7373
end as salary
74-
from t1
74+
from t1
75+
76+
-- My Solution:
77+
**Schema (MySQL v8.0)**
78+
79+
CREATE TABLE Salaries (
80+
`company_id` INTEGER,
81+
`employee_id` INTEGER,
82+
`employee_name` VARCHAR(11),
83+
`salary` INTEGER
84+
);
85+
86+
INSERT INTO Salaries
87+
(`company_id`, `employee_id`, `employee_name`, `salary`)
88+
VALUES
89+
('1', '1', 'Tony', '2000'),
90+
('1', '2', 'Pronub', '21300'),
91+
('1', '3', 'Tyrrox', '10800'),
92+
('2', '1', 'Pam', '300'),
93+
('2', '7', 'Bassem', '450'),
94+
('2', '9', 'Hermione', '700'),
95+
('3', '7', 'Bocaben', '100'),
96+
('3', '2', 'Ognjen', '2200'),
97+
('3', '13', 'Nyancat', '3300'),
98+
('3', '15', 'Morninngcat', '1866');
99+
100+
---
101+
102+
**Query #1**
103+
104+
with cte as
105+
(select company_id, max(salary) as max_sal from Salaries
106+
group by company_id)
107+
108+
select s.company_id, s.employee_id, s.employee_name, round((case when cte.max_sal < 1000 then s.salary
109+
when cte.max_sal between 1000 and 10000 then s.salary - (24/100) * s.salary
110+
when cte.max_sal > 10000 then s.salary - (49/100) * s.salary end)) as salary
111+
from Salaries s left join cte
112+
on s.company_id = cte.company_id;
113+
114+
| company_id | employee_id | employee_name | salary |
115+
| ---------- | ----------- | ------------- | ------ |
116+
| 1 | 1 | Tony | 1020 |
117+
| 1 | 2 | Pronub | 10863 |
118+
| 1 | 3 | Tyrrox | 5508 |
119+
| 2 | 1 | Pam | 300 |
120+
| 2 | 7 | Bassem | 450 |
121+
| 2 | 9 | Hermione | 700 |
122+
| 3 | 7 | Bocaben | 76 |
123+
| 3 | 2 | Ognjen | 1672 |
124+
| 3 | 13 | Nyancat | 2508 |
125+
| 3 | 15 | Morninngcat | 1418 |
126+
127+
---
128+
129+
[View on DB Fiddle](https://www.db-fiddle.com/f/gdYTR3RBd5P92nxtzygseW/1)

0 commit comments

Comments
 (0)