|
Solutions for Section 6.1
Solutions for Section 6.2
Solutions for Section 6.3
Solutions for Section 6.4
Solutions for Section 6.5
Solutions for Section 6.6
Solutions for Section 6.7
SELECT address FROM Studio WHERE name = 'MGM';
SELECT starName FROM StarsIn WHERE movieYear = 1980 OR movieTitle LIKE '%Love%';However, another reasonable interpretation is that we want the word Love as a word by itself. The query above returns stars of a movie like The Cook, the Thief, His Wife, and Her Lover. To identify only titles that have Love as a word by itself, either at the beginning, the middle, the endor as the entire title, we need to use four patterns. The following query works; notice the judiciously placed blanks in the patterns.
SELECT starName FROM StarsIn WHERE movieYear = 1980 OR movieTitle LIKE 'Love %' OR movieTitle LIKE '% Love %' OR movieTitle LIKE '% Love' OR movieTitle = 'Love';
SELECT model, speed, hd FROM PC WHERE price < 1200;The result:
model | speed | hd |
---|---|---|
1001 | 700 | 10 |
1004 | 866 | 10 |
1008 | 700 | 30 |
1010 | 750 | 30 |
1012 | 350 | 7 |
SELECT model, speed AS megahertz, hd AS gigabytes FROM PC WHERE price < 1200;
SELECT * FROM Printer WHERE color;The result:
model | color | type | price |
---|---|---|---|
3001 | true | ink-jet | 231 |
3002 | true | ink-jet | 267 |
3004 | true | ink-jet | 439 |
3005 | true | bubble | 200 |
3006 | true | laser | 1999 |
SELECT name FROM MovieStar, StarsIn WHERE gender = 'M' AND name = starName AND movieTitle = 'Terms of Endearment';
SELECT M1.title FROM Movie AS M1, Movie AS M2 WHERE M2.title = 'Gone With the Wind' AND M1.length > M2.length;
SELECT maker, speed FROM Product, Laptop WHERE hd >= 30 AND Product.model = Laptop.model;
(SELECT Product.model, price FROM Product, PC WHERE Product.model = PC.model AND maker = 'B') UNION (SELECT Product.model, price FROM Product, Laptop WHERE Product.model = Laptop.model AND maker = 'B') UNION (SELECT Product.model, price FROM Product, Printer WHERE Product.model = Printer.model AND maker = 'B');
FROM R1 AS T1, R2 AS T2,...,Rn AS Tn.Now, build the WHERE clause from C by replacing every reference to some attribute A of Ri by Ti.A. Also, build the SELECT clause from list of attributes L by replacing every attribute A of Ri by Ti.A.
SELECT maker FROM Product WHERE model IN (SELECT model FROM PC WHERE speed >= 1200); SELECT maker FROM Product WHERE EXISTS (SELECT * FROM PC WHERE speed >= 1200 AND Product.model = model);Notice that the second solution uses a correlated subquery, and ``model'' refers to the more local PC.model unless we explicitly say that the ``model'' of the outer query is wanted by Product.model.
SELECT class FROM Ships WHERE name IN (SELECT ship FROM Outcomes WHERE result = 'sunk'); SELECT class FROM Ships WHERE EXISTS (SELECT * FROM Outcomes WHERE Ships.name = Outcomes.ship AND result = 'sunk');
SELECT name, address FROM MovieStar WHERE gender = 'F' AND (name, address) IN (SELECT name, address FROM MovieExec WHERE netWorth > 10000000);
Once we have this outerjoin, we can join it with Product. There are two problems.
(SELECT maker, model, type AS productType FROM Product) RIGHT NATURAL OUTER JOIN ((PC FULL NATURAL OUTER JOIN Laptop) FULL NATURAL OUTER JOIN Printer);
FROM R, S
SELECT AVG(speed) FROM PC;
SELECT maker, AVG(screen) FROM Product, Laptop WHERE Product.model = Laptop.model GROUP BY maker;
SELECT speed, AVG(price) FROM PC WHERE speed > 800 GROUP BY speed;Notice that the condition about speed is not a property of a group, so we do not need a HAVING clause.
INSERT INTO Classes VALUES('Nelson', 'bb', 'Gt. Britain', 9, 16, 34000); INSERT INTO Ships VALUES('Nelson', 'Nelson', 1927); INSERT INTO Ships VALUES('Rodney', 'Nelson', 1927);
DELETE FROM Ships WHERE name IN (SELECT ship FROM Outcomes WHERE result = 'sunk');
UPDATE Classes SET bore = bore * 2.5, displacement = displacement/1.1;
CREATE TABLE Movie ( title VARCHAR(255), year INTEGER, length INTEGER, inColor BIT(1), studioName CHAR(50), producerC# INTEGER ); CREATE TABLE StarsIn ( movieTitle VARCHAR(255), movieYear INTEGER, starName CHAR(30) ); CREATE TABLE MovieExec ( name CHAR(30), address VARCHAR(255), cert# INTEGER, netWorth INTEGER ); CREATE TABLE Studio ( name CHAR(50), address VARCHAR(255), presC# INTEGER );
CREATE TABLE Laptop ( model INTEGER, speed INTEGER, ram INTEGER, hd FLOAT, screen FLOAT, price INTEGER );
ALTER TABLE Laptop ADD cd CHAR(5) DEFAULT 'none';
CREATE VIEW RichExec AS SELECT * FROM MovieExec WHERE netWorth >= 10000000;
SELECT RichExec.name FROM RichExec, StudioPres WHERE RichExec.name = StudioPres.name;