source: liacs/db/opdr2/ex2.sql@ 10

Last change on this file since 10 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

  • Property svn:executable set to *
File size: 4.7 KB
RevLine 
[2]1--- 1a
2--- Note: RTRIM is used to remove trailing whitespace
3SELECT S.sname
4FROM Sportsman S
5WHERE RTRIM(sname) LIKE 'S%m%e';
6
7--- 1b
8SELECT S.sname
9FROM Sportsman S, Coaches T
10WHERE S.sid = T.sid AND S.sage > 23;
11
12
13--- 1c
14SELECT C.cname
15FROM Coach C, Coaches T, Sportsman S
16WHERE C.cid = T.cid AND
17 T.sid = S.sid AND
18 C.ccountry = S.scountry AND
19 C.cage < ( SELECT MAX( C2.cage )
20 FROM Coach C2);
21
22--- 1d
23SELECT S.sname, C.cname
24FROM Sportsman S, Coach C, Coaches T
25WHERE S.sid = T.sid AND
26 T.cid = C.cid AND
27 ABS(S.sage - C.cage) < 10;
28
29--- 1e1
30SELECT UNIQUE C.cname
31FROM Coach C, Coaches T1, Coaches T2
32WHERE C.cid = T1.cid AND
33 T1.cid = T2.cid AND
34 NOT T1.sid = T2.sid;
35
36--- 1e2
37SELECT C.cname
38FROM Coach C
39WHERE C.cid = ( SELECT T.cid
40 FROM Coaches T
41 WHERE T.cid = C.cid
42 GROUP BY T.cid
43 HAVING COUNT(*) > 1 );
44
45--- 1f
46SELECT C.cname
47FROM Coach C
48WHERE C.cid IN ( SELECT T.cid
49 FROM Coaches T, Sportsman S
50 WHERE T.sid = S.sid AND S.scountry = 'nl') AND
51 C.cid IN ( SELECT T2.cid
52 FROM Coaches T2, Sportsman S2
53 WHERE T2.sid = S2.sid AND S2.scountry = 'de'
54 GROUP BY T2.cid
55 HAVING COUNT(*) = 1);
56
57--- 1g
58SELECT C.cname
59FROM Coach C
60WHERE C.cid IN ( SELECT T.cid
61 FROM Coaches T, Sportsman S
62 WHERE T.sid = S.sid AND S.scountry = 'nl'
63 GROUP BY T.cid
64 HAVING COUNT(*) = 1) AND
65 C.cid NOT IN ( SELECT T2.cid
66 FROM Coaches T2, Sportsman S2
67 WHERE T2.sid = S2.sid AND S2.scountry = 'de');
68
69--- 1h
70SELECT C.cname C
71FROM Coach C
72WHERE C.cid NOT IN ( SELECT T.cid
73 FROM Coaches T, Sportsman S
74 WHERE T.sid = S.sid AND S.scountry = 'fr');
75
76--- 1i
77SELECT C.cname C
78FROM Coach C
79WHERE C.cid IN ( SELECT T.cid
80 FROM Coaches T, Sportsman S
81 WHERE T.sid = S.sid AND S.scountry != 'fr');
82
83--- 2a
84SELECT R.Branch, S.sname, R.NumHours
85FROM Sleep R, Sportsman S, (SELECT R2.Branch, MAX(R2.NumHours) AS max
86 FROM Sleep R2
87 GROUP BY R2.Branch) SleepB
88WHERE S.sid = R.sid AND R.Branch = SleepB.Branch AND R.NumHours = SleepB.max;
89
90
91--- 2b
92SELECT S.sname, Person.avg AS avg_sleep, Person2Branch.Branch
93FROM Sportsman S, (SELECT R.Branch, AVG(R.NumHours) AS avg
94 FROM Sleep R
95 GROUP BY R.Branch) Branch,
96 (SELECT DISTINCT R2.sid, R2.Branch
97 FROM Sleep R2) Person2Branch,
98 (SELECT R3.sid, AVG(R3.NumHours) AS avg
99 FROM Sleep R3
100 GROUP BY R3.sid) Person
101WHERE S.sid = Person.sid AND
102 S.sid = Person2Branch.sid AND
103 Person2Branch.Branch = Branch.Branch AND
104 Person.avg < Branch.avg;
105
106
107--- 3a
108--- nothing done, when deleting a country from Members
109DROP TABLE Votes;
110DROP TABLE Members;
111CREATE TABLE Members(
112 name char(20),
113 status char(20) NOT NULL,
114 PRIMARY KEY ( name ),
115 CHECK( (status = 'permanent') OR (status = 'elected') )
116);
117
118CREATE TABLE Votes(
119 res char(20),
120 country char(20),
121 vote char(20) NOT NULL,
122 PRIMARY KEY ( res, country ),
123 FOREIGN KEY ( country ) REFERENCES Members( name ),
124 CHECK( (vote = 'yes') OR ( vote = 'no' ) OR ( vote = 'abstrain' ) )
125);
126
127
128--some bogus data
129INSERT INTO Members VALUES ('dk', 'elected' );
130INSERT INTO Members VALUES ('nl', 'elected' );
131INSERT INTO Members VALUES ('de', 'permanent' );
132INSERT INTO Members VALUES ('en', 'permanent' );
133INSERT INTO Members VALUES ('fr', 'permanent' );
134
135INSERT INTO Votes VALUES ('oud', 'nl', 'no' );
136INSERT INTO Votes VALUES ('oud', 'dk', 'yes' );
137INSERT INTO Votes VALUES ('oud', 'en', 'yes' );
138INSERT INTO Votes VALUES ('nieuw', 'nl', 'yes' );
139INSERT INTO Votes VALUES ('oud', 'de', 'no' );
140INSERT INTO Votes VALUES ('oud1', 'de', 'no' );
141
142SELECT V.res
143FROM Votes V
144GROUP BY V.res
145HAVING 0 = ( SELECT COUNT(*)
146 FROM Votes V2, Members M
147 WHERE V.res = V2.res AND
148 V2.country = M.name AND
149 M.status = 'permanent' AND
150 V2.vote = 'no' ) AND
151 14 < ( SELECT COUNT(*)
152 FROM Votes V3
153 WHERE V3.res = V.res AND
154 V3.vote = 'yes');
155
156---3c
157--this is not a correct answer, cause it don't give value to countries with no veto votes, but it lists all the 'permanent countries'
158SELECT M.name, Vote.veto
159FROM Members M, ( SELECT V.country, Count (*) AS veto
160 FROM Votes V
161 WHERE V.vote = 'no'
162 GROUP BY V.country
163 ) Vote
164WHERE M.status = 'permanent' AND M.name = Vote.country (+) ;
165
Note: See TracBrowser for help on using the repository browser.