CREATE TABLE player ( playerno SMALLINT, pfname VARCHAR(15), plname VARCHAR(20), pdob DATE, PRIMARY KEY (playerno)); CREATE TABLE competition ( compno SMALLINT, location VARCHAR(20), comptitle VARCHAR(20), datebegin DATE, PRIMARY KEY (compno)); CREATE TABLE field ( playerno SMALLINT, compno SMALLINT, PRIMARY KEY (playerno, compno), CONSTRAINT fkplayer1 FOREIGN KEY(playerno) REFERENCES player(playerno), CONSTRAINT fkcomp1 FOREIGN KEY(compno) REFERENCES competition(compno)); CREATE TABLE golfround( roundno SMALLINT, rounddate DATE, compno SMALLINT, PRIMARY KEY (roundno, compno), CONSTRAINT fkcomp2 FOREIGN KEY(compno) REFERENCES competition(compno)); CREATE TABLE hole ( holeno SMALLINT, length SMALLINT, par SMALLINT, roundno SMALLINT, compno SMALLINT, PRIMARY KEY (holeno, roundno, compno), CONSTRAINT fkround FOREIGN KEY(roundno, compno) REFERENCES golfround(roundno, compno)); CREATE TABLE score ( strokes SMALLINT, holeno SMALLINT, roundno SMALLINT, compno SMALLINT, playerno SMALLINT, PRIMARY KEY (holeno, roundno, compno, playerno), CONSTRAINT fkhole FOREIGN KEY(holeno, roundno, compno) REFERENCES hole(holeno, roundno, compno), CONSTRAINT fkplayer2 FOREIGN KEY(playerno) REFERENCES player(playerno));
SELECT itemname FROM item JOIN lineitem ON item.itemno = lineitem.itemno WHERE lineqty > 1;
itemname | lineqty |
---|---|
Safari chair | 50 |
Hammock | 50 |
Tent - 8 person | 8 |
SELECT itemname, saledate, SUM(lineqty*lineprice)
FROM item JOIN lineitem
ON item.itemno = lineitem.itemno
JOIN sale
ON sale.saleno = lineitem.salemno
GROUP BY itemname, saledate;
itemname |
expr101 |
---|---|
Geo positioning system | 2 |
Hammock | 50 |
Pocket knife - Avon | 5 |
Safari chair | 50 |
Tent - 2 person | 2 |
Tent - 8 person | 8 |
SELECT itemname FROM item
WHERE itemtype = 'F'
AND EXISTS (SELECT * FROM lineitem
WHERE lineitem.itemno = item.itemno); SELECT DISTINCT(itemname) FROM item JOIN lineitem
ON item.itemno = lineitem.itemno
WHERE itemtype = 'F';
itemname |
---|
Hammock |
Safari chair |
Tent - 2 person |
Tent - 8 person |
SELECT DISTINCT(itemname) FROM item
WHERE itemtype = 'F'
AND NOT EXISTS
(SELECT * FROM lineitem
WHERE item.itemno = lineitem.itemno); SELECT DISTINCT(itemname) FROM item
WHERE itemtype = 'F'
AND itemname NOT IN
(SELECT itemname FROM item JOIN lineitem
ON item.itemno = lineitem.itemno WHERE itemtype = 'F');
SELECT saleno, SUM(lineqty * lineprice)
FROM lineitem
GROUP BY saleno;
saleno | sum |
---|---|
1 |
4.50 |
2 |
72.25 |
3 |
502.25 |
4 |
712.25 |
5 |
5109.00 |
One has to create a third entity to store facts about the m:m relationship.
The plus sign near a relationship arc signifies that the identifier of the one-end of the relationship is part of the identifier for the entity at the many-end.
The main difference between EXIST and other clauses in an SQL statement is that EXIST returns the value true or false instead of a table. As long as at least one row can be found that satisfies the EXIST condition, true is returned. Otherwise false is returned.
SELECT dphone FROM donor
WHERE dlname = 'Hays' OR dlname = 'Jefts';;
dphone |
---|
1352 |
8103 |
SELECT COUNT (*) FROM donor;
SELECT COUNT(donorno) FROM gift WHERE year = 1999;
expr1000 |
---|
10 |
SELECT dlname, dfname FROM donor JOIN gift
ON donor.donorno = gift.donorno
WHERE year = 1999
AND amount = (SELECT MAX(amount) FROM gift WHERE year = 1999);
SELECT SUM (amount) FROM gift WHERE year = 2000;
expr1000 |
---|
$6,768.00 |
SELECT dlname, dfname FROM donor WHERE NOT EXISTS (SELECT * FROM year WHERE NOT EXISTS (SELECT * FROM gift WHERE donor.donorno = gift.donorno AND year.year = gift.year));
SELECT dlname, dfname, AVG(amount)
FROM donor JOIN gift
ON donor.donorno = gift.donorno
GROUP BY dlname, dfname
HAVING AVG(amount) > (SELECT 2*AVG(amount) FROM gift);
dfname |
dlname |
expr1002 |
---|---|---|
Gulsen | Beckman | $2,405.00 |
SELECT dlname, dfname, SUM(amount)
FROM donor JOIN gift
ON donor.donorno = gift.donorno
GROUP BY dlname, dfname
ORDER BY dlname, dfname;
SELECT DSTATE, SUM(amount)
FROM donor JOIN gift
ON donor.donorno = gift.donorno
WHERE year = 2001 GROUP BY dstate;
dstate | expr1001 |
---|---|
AK | $332.00 |
AZ | $155.00 |
GA | $671.00 |
MA | $499.00 |
MN | $835.00 |
NC | $966.00 |
ND | $345.00 |
WA | $5,208.00 |
SELECT year.year, yeargoal AS Goal, SUM(amount) AS Total
FROM year JOIN gift
ON year.year = gift.year
WHERE yeargoal < (SELECT SUM(amount)
FROM gift
WHERE year.year = gift.year)
GROUP BY year.year
Nutrition facts | ||
Serving size 1 cup (30g) | ||
Servings per container about 17 | ||
Amount per serving | Cereal | with 1/2 cup
of skim milk |
Calories | 110 | 150 |
Calories from Fat | 10 | 10 |
% Daily Value | ||
Total Fat 1g | 1% | 2% |
Saturated Fat 0g | 0% | 0% |
Polyunsaturated Fat 0g | ||
Monounsaturated Fat 0g | ||
Cholesterol 0mg | 0% | 1% |
Sodium 220mg | 9% | 12% |
Potassium 105 mg | 3% | 9% |
Total Carbohydrate 24g | 8% | 10% |
Dietary Fiber 3g | 13% | 13% |
Sugars 4g | ||
Other Carbohydrate 17g | ||
Protein 3g | ||
Vitamin A | 10% | 15% |
Vitamin C | 10% | 10% |
Calcium | 2% | 15% |
Iron | 45% | 45% |
Vitamin D | 10% | 25% |
Thiamin | 50% | 50% |
Riboflavin | 50% | 50% |
Niacin | 50% | 50% |
Vitamin B12 | 50% | 60% |
Phosphorus | 10% | 20% |
Magnesium | 8% | 10% |
Zinc | 50% | 50% |
Copper | 4% | 4% |
This page is part of the promotional and support
material for Data Management (open edition) by Richard T. Watson For questions and comments please contact the author |