Skip to content

Commit 330abc9

Browse files
authored
Add files via upload
1 parent f8c2085 commit 330abc9

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed

Medium/Activity Participants.sql

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
-- Question 77
2+
-- Table: Friends
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | name | varchar |
9+
-- | activity | varchar |
10+
-- +---------------+---------+
11+
-- id is the id of the friend and primary key for this table.
12+
-- name is the name of the friend.
13+
-- activity is the name of the activity which the friend takes part in.
14+
-- Table: Activities
15+
16+
-- +---------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------+---------+
19+
-- | id | int |
20+
-- | name | varchar |
21+
-- +---------------+---------+
22+
-- id is the primary key for this table.
23+
-- name is the name of the activity.
24+
25+
26+
-- Write an SQL query to find the names of all the activities with neither maximum, nor minimum number of participants.
27+
28+
-- Return the result table in any order. Each activity in table Activities is performed by any person in the table Friends.
29+
30+
-- The query result format is in the following example:
31+
32+
-- Friends table:
33+
-- +------+--------------+---------------+
34+
-- | id | name | activity |
35+
-- +------+--------------+---------------+
36+
-- | 1 | Jonathan D. | Eating |
37+
-- | 2 | Jade W. | Singing |
38+
-- | 3 | Victor J. | Singing |
39+
-- | 4 | Elvis Q. | Eating |
40+
-- | 5 | Daniel A. | Eating |
41+
-- | 6 | Bob B. | Horse Riding |
42+
-- +------+--------------+---------------+
43+
44+
-- Activities table:
45+
-- +------+--------------+
46+
-- | id | name |
47+
-- +------+--------------+
48+
-- | 1 | Eating |
49+
-- | 2 | Singing |
50+
-- | 3 | Horse Riding |
51+
-- +------+--------------+
52+
53+
-- Result table:
54+
-- +--------------+
55+
-- | activity |
56+
-- +--------------+
57+
-- | Singing |
58+
-- +--------------+
59+
60+
-- Eating activity is performed by 3 friends, maximum number of participants, (Jonathan D. , Elvis Q. and Daniel A.)
61+
-- Horse Riding activity is performed by 1 friend, minimum number of participants, (Bob B.)
62+
-- Singing is performed by 2 friends (Victor J. and Jade W.)
63+
64+
-- Solution
65+
with t1 as(
66+
select max(a.total) as total
67+
from(
68+
select activity, count(*) as total
69+
from friends
70+
group by activity) a
71+
union all
72+
select min(b.total) as low
73+
from(
74+
select activity, count(*) as total
75+
from friends
76+
group by activity) b),
77+
t2 as
78+
(
79+
select activity, count(*) as total
80+
from friends
81+
group by activity
82+
)
83+
84+
select activity
85+
from t1 right join t2
86+
on t1.total = t2.total
87+
where t1.total is null
88+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
-- Question 76
2+
-- Table: Scores
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | player_name | varchar |
8+
-- | gender | varchar |
9+
-- | day | date |
10+
-- | score_points | int |
11+
-- +---------------+---------+
12+
-- (gender, day) is the primary key for this table.
13+
-- A competition is held between females team and males team.
14+
-- Each row of this table indicates that a player_name and with gender has scored score_point in someday.
15+
-- Gender is 'F' if the player is in females team and 'M' if the player is in males team.
16+
17+
18+
-- Write an SQL query to find the total score for each gender at each day.
19+
20+
-- Order the result table by gender and day
21+
22+
-- The query result format is in the following example:
23+
24+
-- Scores table:
25+
-- +-------------+--------+------------+--------------+
26+
-- | player_name | gender | day | score_points |
27+
-- +-------------+--------+------------+--------------+
28+
-- | Aron | F | 2020-01-01 | 17 |
29+
-- | Alice | F | 2020-01-07 | 23 |
30+
-- | Bajrang | M | 2020-01-07 | 7 |
31+
-- | Khali | M | 2019-12-25 | 11 |
32+
-- | Slaman | M | 2019-12-30 | 13 |
33+
-- | Joe | M | 2019-12-31 | 3 |
34+
-- | Jose | M | 2019-12-18 | 2 |
35+
-- | Priya | F | 2019-12-31 | 23 |
36+
-- | Priyanka | F | 2019-12-30 | 17 |
37+
-- +-------------+--------+------------+--------------+
38+
-- Result table:
39+
-- +--------+------------+-------+
40+
-- | gender | day | total |
41+
-- +--------+------------+-------+
42+
-- | F | 2019-12-30 | 17 |
43+
-- | F | 2019-12-31 | 40 |
44+
-- | F | 2020-01-01 | 57 |
45+
-- | F | 2020-01-07 | 80 |
46+
-- | M | 2019-12-18 | 2 |
47+
-- | M | 2019-12-25 | 13 |
48+
-- | M | 2019-12-30 | 26 |
49+
-- | M | 2019-12-31 | 29 |
50+
-- | M | 2020-01-07 | 36 |
51+
-- +--------+------------+-------+
52+
-- For females team:
53+
-- First day is 2019-12-30, Priyanka scored 17 points and the total score for the team is 17.
54+
-- Second day is 2019-12-31, Priya scored 23 points and the total score for the team is 40.
55+
-- Third day is 2020-01-01, Aron scored 17 points and the total score for the team is 57.
56+
-- Fourth day is 2020-01-07, Alice scored 23 points and the total score for the team is 80.
57+
-- For males team:
58+
-- First day is 2019-12-18, Jose scored 2 points and the total score for the team is 2.
59+
-- Second day is 2019-12-25, Khali scored 11 points and the total score for the team is 13.
60+
-- Third day is 2019-12-30, Slaman scored 13 points and the total score for the team is 26.
61+
-- Fourth day is 2019-12-31, Joe scored 3 points and the total score for the team is 29.
62+
-- Fifth day is 2020-01-07, Bajrang scored 7 points and the total score for the team is 36.
63+
64+
-- Solution
65+
select gender, day,
66+
sum(score_points) over(partition by gender order by day) as total
67+
from scores
68+
group by 1,2
69+
order by 1,2

0 commit comments

Comments
 (0)