Skip to content

Commit b8c2334

Browse files
committed
Added readme and some questions/solutions
1 parent fdce580 commit b8c2334

13 files changed

+276
-1
lines changed

README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,70 @@ This repository contains a sample PostgreSQL database aimed at helping people ne
3030

3131
## Schema
3232

33+
```mermaid
34+
erDiagram
35+
36+
athlete {
37+
id integer
38+
name varchar
39+
sex sex
40+
height integer
41+
weight integer
42+
}
43+
44+
sport {
45+
id integer
46+
name varchar
47+
}
48+
49+
country {
50+
code char
51+
name varchar
52+
alternative_name varchar
53+
}
54+
55+
games {
56+
code char
57+
name varchar
58+
year integer
59+
season season "Summer or Winter"
60+
city varchar
61+
}
62+
63+
event {
64+
id integer
65+
sport_id integer
66+
name varchar
67+
sex sex
68+
}
69+
70+
appearance {
71+
athlete_id integer
72+
country_code char
73+
games_code char
74+
event_id integer
75+
age integer
76+
medal medal
77+
}
78+
79+
80+
athlete ||--|{ appearance : competes
81+
country ||--|{ appearance : "represents"
82+
games ||--|{ appearance : "is made at"
83+
sport ||--|{ event : "belongs to"
84+
event ||--|{ appearance : "in"
85+
```
86+
3387
## Questions
88+
89+
1. How many gymnastics events are there?
90+
2. When and where were the first Winter Olympic Games?
91+
3. Which athlete taller than 2m has the last name alphabetically?
92+
4. Who is the youngest ever competitor at any games?
93+
5. Which five events have the longest names?
94+
6. Which sports did France win medals at in teh 1952 summer games?
95+
7. Which country won the most biathlon medals between 1990 and 2010?
96+
8. Which sports were included before 1920 but not after?
97+
9. Which ten countries have the most gold medals in table tennis?
98+
10. How many games did Linford Christie compete at?
99+
11. How many athletes have competed at both the summer and winter Olympic games?

ddl/appearance.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
drop table if exists appearance;
22

3-
create type medal as enum ('Gold', 'Silver', 'Bronze');
3+
create type medal as enum ('Bronze', 'Silver', 'Gold');
44

55
create table appearance (
66
athlete_id integer not null references athlete(id),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- how many gymnastic events are there?
2+
3+
select
4+
count(*)
5+
from sport s
6+
inner join event e
7+
on s.id = e.sport_id
8+
where
9+
s.name = 'Gymnastics';
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- when and where were the first Winter Olympic Games held?
2+
3+
select
4+
year,
5+
city
6+
from
7+
games
8+
where
9+
season = 'Winter'
10+
order by
11+
year asc
12+
limit
13+
1
14+
;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- which athlete taller than 2m has the last name when sorted alphabetically?
2+
3+
select
4+
name
5+
from
6+
athlete
7+
where
8+
height >= 200
9+
order by
10+
name desc
11+
limit
12+
1;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- who is the youngest ever competitor?
2+
3+
select
4+
ath.name,
5+
c.name as country
6+
from
7+
appearance ap
8+
inner join
9+
athlete ath
10+
on ap.athlete_id = ath.id
11+
inner join
12+
country c
13+
on ap.country_code = c.code
14+
order by
15+
age asc
16+
limit
17+
1;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- which five events have the longest names?
2+
3+
select
4+
name,
5+
length(name) size
6+
from
7+
event
8+
order by
9+
size desc
10+
limit
11+
5;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- which sports did France win medals at in the 1952 summer games?
2+
3+
select
4+
distinct(s.name)
5+
from
6+
appearance a
7+
inner join
8+
event e
9+
on a.event_id = e.id
10+
inner join
11+
sport s
12+
on e.sport_id = s.id
13+
inner join
14+
games g
15+
on a.games_code = g.code
16+
where
17+
g.year = 1952
18+
and
19+
g.season = 'Summer'
20+
and
21+
a.country_code = 'FRA';
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- which country won most biathlon medals between 1990 and 2010
2+
3+
select
4+
c.name,
5+
count(*) quantity
6+
from
7+
appearance a
8+
inner join
9+
event e
10+
on a.event_id = e.id
11+
inner join
12+
sport s
13+
on e.sport_id = s.id
14+
inner join
15+
country c
16+
on a.country_code = c.code
17+
inner join
18+
games g
19+
on a.games_code = g.code
20+
where
21+
s.name = 'Biathlon'
22+
and
23+
a.medal is not null
24+
and
25+
g.year between 1990 and 2010
26+
group by
27+
c.name
28+
order by
29+
quantity desc
30+
limit
31+
1
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- which sports were played before 1920 but not after?
2+
3+
with sports_played_after_1920 as (
4+
select
5+
distinct(s.id)
6+
from
7+
appearance a
8+
inner join
9+
event e
10+
on a.event_id = e.id
11+
inner join
12+
sport s
13+
on e.sport_id = s.id
14+
inner join
15+
games g
16+
on a.games_code = g.code
17+
where
18+
g.year >= 1920
19+
)
20+
select
21+
name
22+
from
23+
sport
24+
where
25+
id not in (select id from sports_played_after_1920);

0 commit comments

Comments
 (0)