[2] | 1 | --- 1a
|
---|
| 2 | --- Note: RTRIM is used to remove trailing whitespace
|
---|
| 3 | SELECT S.sname
|
---|
| 4 | FROM Sportsman S
|
---|
| 5 | WHERE RTRIM(sname) LIKE 'S%m%e';
|
---|
| 6 |
|
---|
| 7 | --- 1b
|
---|
| 8 | SELECT S.sname
|
---|
| 9 | FROM Sportsman S, Coaches T
|
---|
| 10 | WHERE S.sid = T.sid AND S.sage > 23;
|
---|
| 11 |
|
---|
| 12 |
|
---|
| 13 | --- 1c
|
---|
| 14 | SELECT C.cname
|
---|
| 15 | FROM Coach C, Coaches T, Sportsman S
|
---|
| 16 | WHERE 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
|
---|
| 23 | SELECT S.sname, C.cname
|
---|
| 24 | FROM Sportsman S, Coach C, Coaches T
|
---|
| 25 | WHERE S.sid = T.sid AND
|
---|
| 26 | T.cid = C.cid AND
|
---|
| 27 | ABS(S.sage - C.cage) < 10;
|
---|
| 28 |
|
---|
| 29 | --- 1e1
|
---|
| 30 | SELECT UNIQUE C.cname
|
---|
| 31 | FROM Coach C, Coaches T1, Coaches T2
|
---|
| 32 | WHERE C.cid = T1.cid AND
|
---|
| 33 | T1.cid = T2.cid AND
|
---|
| 34 | NOT T1.sid = T2.sid;
|
---|
| 35 |
|
---|
| 36 | --- 1e2
|
---|
| 37 | SELECT C.cname
|
---|
| 38 | FROM Coach C
|
---|
| 39 | WHERE 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
|
---|
| 46 | SELECT C.cname
|
---|
| 47 | FROM Coach C
|
---|
| 48 | WHERE 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
|
---|
| 58 | SELECT C.cname
|
---|
| 59 | FROM Coach C
|
---|
| 60 | WHERE 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
|
---|
| 70 | SELECT C.cname C
|
---|
| 71 | FROM Coach C
|
---|
| 72 | WHERE 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
|
---|
| 77 | SELECT C.cname C
|
---|
| 78 | FROM Coach C
|
---|
| 79 | WHERE 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
|
---|
| 84 | SELECT R.Branch, S.sname, R.NumHours
|
---|
| 85 | FROM Sleep R, Sportsman S, (SELECT R2.Branch, MAX(R2.NumHours) AS max
|
---|
| 86 | FROM Sleep R2
|
---|
| 87 | GROUP BY R2.Branch) SleepB
|
---|
| 88 | WHERE S.sid = R.sid AND R.Branch = SleepB.Branch AND R.NumHours = SleepB.max;
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | --- 2b
|
---|
| 92 | SELECT S.sname, Person.avg AS avg_sleep, Person2Branch.Branch
|
---|
| 93 | FROM 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
|
---|
| 101 | WHERE 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
|
---|
| 109 | DROP TABLE Votes;
|
---|
| 110 | DROP TABLE Members;
|
---|
| 111 | CREATE 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 |
|
---|
| 118 | CREATE 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
|
---|
| 129 | INSERT INTO Members VALUES ('dk', 'elected' );
|
---|
| 130 | INSERT INTO Members VALUES ('nl', 'elected' );
|
---|
| 131 | INSERT INTO Members VALUES ('de', 'permanent' );
|
---|
| 132 | INSERT INTO Members VALUES ('en', 'permanent' );
|
---|
| 133 | INSERT INTO Members VALUES ('fr', 'permanent' );
|
---|
| 134 |
|
---|
| 135 | INSERT INTO Votes VALUES ('oud', 'nl', 'no' );
|
---|
| 136 | INSERT INTO Votes VALUES ('oud', 'dk', 'yes' );
|
---|
| 137 | INSERT INTO Votes VALUES ('oud', 'en', 'yes' );
|
---|
| 138 | INSERT INTO Votes VALUES ('nieuw', 'nl', 'yes' );
|
---|
| 139 | INSERT INTO Votes VALUES ('oud', 'de', 'no' );
|
---|
| 140 | INSERT INTO Votes VALUES ('oud1', 'de', 'no' );
|
---|
| 141 |
|
---|
| 142 | SELECT V.res
|
---|
| 143 | FROM Votes V
|
---|
| 144 | GROUP BY V.res
|
---|
| 145 | HAVING 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'
|
---|
| 158 | SELECT M.name, Vote.veto
|
---|
| 159 | FROM Members M, ( SELECT V.country, Count (*) AS veto
|
---|
| 160 | FROM Votes V
|
---|
| 161 | WHERE V.vote = 'no'
|
---|
| 162 | GROUP BY V.country
|
---|
| 163 | ) Vote
|
---|
| 164 | WHERE M.status = 'permanent' AND M.name = Vote.country (+) ;
|
---|
| 165 |
|
---|