-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.self-joins.sql
88 lines (77 loc) · 2.24 KB
/
5.self-joins.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-- What are the names of the coaches?
select
distinct m.firstname || ' ' || m.lastname "Coach Name"
from member c
join member m on c.coach = m.memberid
;
-- How many students has each of the coach ?
select m.coach,
count(m.firstname || ' ' || m.lastname) as Trainee
from member m
-- coach field can be NULL
-- that means a member does not have a coach
where coach is not null
group by 1
;
-- Which members does not have a coach ?
select * from member where coach is null;
-- which members have a coach
select * from member where coach is not null;
-- What is the name of Jane Gilmore's coach?
select c.firstname, c.lastnamefrom member m
join member c on m.coach = c.memberid
where m.firstname = 'Jane' and m.lastname = 'Gilmore'
;
-- Is anyone being coached by someone with a higher handicap?
select
m.firstname || ' ' || m.lastname Trainee,
m.handicap as "Trainee handicap",
c.firstname || ' ' || c.lastname Coach,
c.handicap as "Coach handicap"
from member m
join member c on m.coach = c.memberid
where m.handicap < c.handicap
;
-- Are any women being coached by men?
select
m.firstname || ' ' || m.lastname Trainee,
m.gender,
c.firstname || ' ' || c.lastname Coach,
c.gender
from member m
join member c on m.coach = c.memberid
where m.gender='F' and c.gender='M'
order by 3
;
-- list members and their coaches
select
m.firstname || ' ' || m.lastname Trainee,
c.firstname || ' ' || c.lastname Coach
from member m
join member c on m.coach = c.memberid
;
-- Who Coaches the Coaches ?
-- To answer this we need to make self join three times!
select distinct
c.firstname || ' ' || c.lastname coach,
cc.firstname || ' ' || cc.lastname "his/her coach"
from member m
join member c on m.coach = c.memberid
join member cc on c.coach = cc.memberid
;
/* Question involving "BOTH" */
-- Which members have entered both tournaments 24 and 36?
select distinct m.firstname, m.lastname
from entry a, entry b, member m
where a.memberid = b.memberid
and a.tourid = 24 and b.tourid = 36
and a.memberid = m.memberid
;
-- or
select
distinct m.firstname, m.lastname
from entry a
join entry b using (memberid)
join member m using (memberid)
where a.tourid = 24 and b.tourid = 36
;