SELECT itemname, itemcolor FROM qitem WHERE itemcolor = 'Green' AND itemtype = 'C';
SELECT DISTINCT itemname FROM qdel WHERE itemname NOT IN (SELECT DISTINCT itemname FROM qdel WHERE deptname = 'Books');
SELECT deptname FROM qsale WHERE itemname = 'Compass' AND deptname IN (SELECT deptname FROM qsale GROUP BY deptname HAVING COUNT(DISTINCT itemname) > 2);
MS Access reports a syntax error for this SQL command because it cannot handle COUNT(DISTINCT column-name)
SELECT emp.empfname FROM qemp AS emp, qemp AS boss WHERE emp.bossno = boss.empno AND emp.deptname <> boss.deptname;
SELECT qitem.itemname FROM qitem WHERE itemcolor = 'Green' AND itemname NOT IN (SELECT itemname FROM qsale, qdept WHERE qsale.deptname = qdept.deptname AND deptFLOOR = 2);
SELECT DISTINCT itemname FROM qdel GROUP BY itemname HAVING COUNT (DISTINCT splno) >= 2;
MS Access reports a syntax error for this SQL command because it cannot handle COUNT(DISTINCT column-name)
SELECT itemname FROM qsale GROUP BY itemname HAVING COUNT(deptname) >= 2;
SELECT itemname FROM qitem WHERE NOT EXISTS (SELECT * FROM qdept WHERE deptname NOT IN ('Management', 'Marketing', 'Personnel', 'Accounting', 'Purchasing') AND NOT EXISTS (SELECT * FROM qdel WHERE qdel.itemname = qitem.itemname AND qdel.deptname=qdept.deptname));
SELECT empfname, empsalary FROM qemp WHERE empsalary < (SELECT .90*AVG(empsalary) FROM qemp);
SELECT splname FROM qspl WHERE splno NOT IN (SELECT splno FROM qdel WHERE itemname = 'Compass' OR itemname = 'Geopositioning System');
SELECT COUNT(qsale.itemname) FROM qsale, qitem, qdept WHERE qitem.itemname = qsale.itemname AND qdept.deptname = qsale.deptname AND qdept.deptFLOOR = 3 AND qitem.itemtype = 'A';
The result is 0. If you try items of type 'C' you will get 1.
SELECT qitem.itemname FROM qitem WHERE qitem.itemcolor = 'Green' AND NOT EXISTS (SELECT * FROM qspl WHERE NOT EXISTS (SELECT * FROM qdel WHERE qdel.itemname = qitem.itemname AND qdel.splno = qspl.splno));
There are no items satisfying this query. What about for khaki?
Don't forget to exclude the administrative departments, which don't sell items.
SELECT splname FROM qspl WHERE NOT EXISTS (SELECT * FROM qdept WHERE deptname NOT IN ('Management', 'Marketing', 'Personnel', 'Accounting', 'Purchasing') AND NOT EXISTS (SELECT * FROM qdel WHERE qdel.splno = qspl.splno AND qdel.deptname = qdept.deptname));
SELECT DISTINCT splname FROM qdel, qspl WHERE qdel.splno = qspl.splno AND qdel.splno NOT IN (SELECT splno FROM qdel WHERE itemname = 'Compass');
SELECT qdept.deptname, qdept.deptfloor, AVG(empsalary) FROM qemp, qdept WHERE qdept.deptname = qemp.deptname GROUP BY qdept.deptname, qdept.deptfloor;
This query needs to be done in two parts (see 52 on page 303). First create a a view
CREATE VIEW avgdeptsal(deptname, dpavgsal) AS SELECT deptname, AVG(empsalary) AS dpavgsal FROM qemp GROUP BY deptname;
Then complete the query
SELECT empfname, empsalary-dpavgsal FROM qemp, avgdeptsal WHERE qemp.deptname = avgdeptsal.deptname;
SELECT deptname FROM qdept WHERE deptfloor = 2;
SELECT DISTINCT splname, itemname FROM qspl, qdel WHERE qdel.splno = qspl.splno ORDER BY splname, itemname;
SELECT deptname, COUNT(deptname) FROM qemp GROUP BY deptname;
SELECT empfname FROM qemp WHERE bossno IN (SELECT empno FROM qemp WHERE empfname = 'Todd');
SELECT empfname FROM qemp WHERE empno IN (SELECT bossno FROM qemp WHERE empfname = 'Sophie');
SELECT boss.empfname, emp.empfname FROM qemp AS emp, qemp AS boss WHERE emp.bossno = boss.empno ORDER BY boss.empfname, emp.empfname;
SELECT empfname, empsalary FROM qemp WHERE empsalary < (SELECT MIN(empsalary) FROM qemp WHERE deptname = 'Marketing');
Very similar to 35 on page 300.
SELECT DISTINCT deptname, itemname FROM qsale sale1 WHERE itemname NOT IN (SELECT itemname FROM qsale sale2 WHERE sale1.deptname <> sale2.deptname);
CREATE VIEW aavgdeptsal(deptname, dpavgsal) AS SELECT deptname, AVG(empsalary) AS dpavgsal FROM qemp group BY deptname;
Then complete the query:
SELECT deptname FROM avgdeptsal WHERE dpavgsal = (SELECT MAX(dpavgsal) FROM avgdeptsal);
SELECT deptname FROM qsale WHERE itemname = 'Compass' AND deptname IN (SELECT deptname FROM qsale GROUP BY deptname HAVING COUNT(DISTINCT itemname) > 2);
MS Access reports a syntax error for this SQL command because it cannot handle COUNT(DISTINCT column-name)
SELECT emp.empfname FROM qemp AS emp, qemp AS boss WHERE emp.bossno = boss.empno AND emp.deptname <> boss.deptname;
SELECT qitem.itemname FROM qitem WHERE itemcolor = 'Green' AND itemname NOT IN (SELECT itemname FROM qsale, qdept WHERE qsale.deptname = qdept.deptname AND deptFLOOR = 2);
SELECT DISTINCT itemname FROM qdel GROUP BY itemname HAVING COUNT (DISTINCT splno) >= 2;
MS Access reports a syntax error for this SQL command because it cannot handle COUNT(DISTINCT column-name)
SELECT itemname FROM qsale GROUP BY itemname HAVING COUNT(deptname) >= 2;
SELECT itemname FROM qitem WHERE NOT EXISTS (SELECT * FROM qdept WHERE deptname NOT IN ('Management', 'Marketing', 'Personnel', 'Accounting', 'Purchasing') AND NOT EXISTS (SELECT * FROM qdel WHERE qdel.itemname = qitem.itemname AND qdel.deptname=qdept.deptname));
SELECT empfname, empsalary FROM qemp WHERE empsalary < (SELECT .90*AVG(empsalary) FROM qemp);
SELECT splname FROM qspl WHERE splno NOT IN (SELECT splno FROM qdel WHERE itemname = 'Compass' OR itemname = 'Geopositioning System');
SELECT COUNT(qsale.itemname) FROM qsale, qitem, qdept WHERE qitem.itemname = qsale.itemname AND qdept.deptname = qsale.deptname AND qdept.deptFLOOR = 3 AND qitem.itemtype = 'A';
The result is 0. If you try items of type 'C' you will get 1.
SELECT qitem.itemname FROM qitem WHERE qitem.itemcolor = 'Green' AND NOT EXISTS (SELECT * FROM qspl WHERE NOT EXISTS (SELECT * FROM qdel WHERE qdel.itemname = qitem.itemname AND qdel.splno = qspl.splno));
There are no items satisfying this query. What about for khaki?
Don't forget to exclude the administrative departments, which don't sell items.
SELECT splname FROM qspl WHERE NOT EXISTS (SELECT * FROM qdept WHERE deptname NOT IN ('Management', 'Marketing', 'Personnel', 'Accounting', 'Purchasing') AND NOT EXISTS (SELECT * FROM qdel WHERE qdel.splno = qspl.splno AND qdel.deptname = qdept.deptname));
SELECT DISTINCT splname FROM qdel, qspl WHERE qdel.splno = qspl.splno AND qdel.splno NOT IN (SELECT splno FROM qdel WHERE itemname = 'Compass');
SELECT qdept.deptname, qdept.deptfloor, AVG(empsalary) FROM qemp, qdept WHERE qdept.deptname = qemp.deptname GROUP BY qdept.deptname, qdept.deptfloor;
This query needs to be done in two parts (see 52 on page 303). First create a a view
CREATE VIEW avgdeptsal(deptname, dpavgsal) AS SELECT deptname, AVG(empsalary) AS dpavgsal FROM qemp GROUP BY deptname;
Then complete the query
SELECT empfname, empsalary-dpavgsal FROM qemp, avgdeptsal WHERE qemp.deptname = avgdeptsal.deptname;
SELECT deptname FROM qdept WHERE deptfloor = 2;
SELECT DISTINCT splname, itemname FROM qspl, qdel WHERE qdel.splno = qspl.splno ORDER BY splname, itemname;
SELECT deptname, COUNT(deptname) FROM qemp GROUP BY deptname;
SELECT empfname FROM qemp WHERE bossno IN (SELECT empno FROM qemp WHERE empfname = 'Todd');
SELECT empfname FROM qemp WHERE empno IN (SELECT bossno FROM qemp WHERE empfname = 'Sophie');
SELECT boss.empfname, emp.empfname FROM qemp AS emp, qemp AS boss WHERE emp.bossno = boss.empno ORDER BY boss.empfname, emp.empfname;
SELECT empfname, empsalary FROM qemp WHERE empsalary < (SELECT MIN(empsalary) FROM qemp WHERE deptname = 'Marketing');
Very similar to 35 on page 300.
SELECT DISTINCT deptname, itemname FROM qsale sale1 WHERE itemname NOT IN (SELECT itemname FROM qsale sale2 WHERE sale1.deptname <> sale2.deptname);
CREATE VIEW aavgdeptsal(deptname, dpavgsal) AS SELECT deptname, AVG(empsalary) AS dpavgsal FROM qemp group BY deptname;
Then complete the query:
SELECT deptname FROM avgdeptsal WHERE dpavgsal = (SELECT MAX(dpavgsal) FROM avgdeptsal);
SELECT deptname FROM qsale WHERE itemname = 'Compass' AND deptname IN (SELECT deptname FROM qsale GROUP BY deptname HAVING COUNT(DISTINCT itemname) > 2);
MS Access reports a syntax error for this SQL command because it cannot handle COUNT(DISTINCT column-name)
SELECT emp.empfname FROM qemp AS emp, qemp AS boss WHERE emp.bossno = boss.empno AND emp.deptname <> boss.deptname;
SELECT qitem.itemname FROM qitem WHERE itemcolor = 'Green' AND itemname NOT IN (SELECT itemname FROM qsale, qdept WHERE qsale.deptname = qdept.deptname AND deptFLOOR = 2);
SELECT DISTINCT itemname FROM qdel GROUP BY itemname HAVING COUNT (DISTINCT splno) >= 2;
MS Access reports a syntax error for this SQL command because it cannot handle COUNT(DISTINCT column-name)
SELECT itemname FROM qsale GROUP BY itemname HAVING COUNT(deptname) >= 2;
SELECT itemname FROM qitem WHERE NOT EXISTS (SELECT * FROM qdept WHERE deptname NOT IN ('Management', 'Marketing', 'Personnel', 'Accounting', 'Purchasing') AND NOT EXISTS (SELECT * FROM qdel WHERE qdel.itemname = qitem.itemname AND qdel.deptname=qdept.deptname));
SELECT empfname, empsalary FROM qemp WHERE empsalary < (SELECT .90*AVG(empsalary) FROM qemp);
SELECT splname FROM qspl WHERE splno NOT IN (SELECT splno FROM qdel WHERE itemname = 'Compass' OR itemname = 'Geopositioning System');
SELECT COUNT(qsale.itemname) FROM qsale, qitem, qdept WHERE qitem.itemname = qsale.itemname AND qdept.deptname = qsale.deptname AND qdept.deptFLOOR = 3 AND qitem.itemtype = 'A';
The result is 0. If you try items of type 'C' you will get 1.
SELECT qitem.itemname FROM qitem WHERE qitem.itemcolor = 'Green' AND NOT EXISTS (SELECT * FROM qspl WHERE NOT EXISTS (SELECT * FROM qdel WHERE qdel.itemname = qitem.itemname AND qdel.splno = qspl.splno));
There are no items satisfying this query. What about for khaki?
Don't forget to exclude the administrative departments, which don't sell items.
SELECT splname FROM qspl WHERE NOT EXISTS (SELECT * FROM qdept WHERE deptname NOT IN ('Management', 'Marketing', 'Personnel', 'Accounting', 'Purchasing') AND NOT EXISTS (SELECT * FROM qdel WHERE qdel.splno = qspl.splno AND qdel.deptname = qdept.deptname));
SELECT DISTINCT splname FROM qdel, qspl WHERE qdel.splno = qspl.splno AND qdel.splno NOT IN (SELECT splno FROM qdel WHERE itemname = 'Compass');
SELECT qdept.deptname, qdept.deptfloor, AVG(empsalary) FROM qemp, qdept WHERE qdept.deptname = qemp.deptname GROUP BY qdept.deptname, qdept.deptfloor;
This query needs to be done in two parts (see 52 on page 303). First create a a view
CREATE VIEW avgdeptsal(deptname, dpavgsal) AS SELECT deptname, AVG(empsalary) AS dpavgsal FROM qemp GROUP BY deptname;
Then complete the query
SELECT empfname, empsalary-dpavgsal FROM qemp, avgdeptsal WHERE qemp.deptname = avgdeptsal.deptname;
SELECT deptname FROM qdept WHERE deptfloor = 2;
SELECT DISTINCT splname, itemname FROM qspl, qdel WHERE qdel.splno = qspl.splno ORDER BY splname, itemname;
SELECT deptname, COUNT(deptname) FROM qemp GROUP BY deptname;
SELECT empfname FROM qemp WHERE bossno IN (SELECT empno FROM qemp WHERE empfname = 'Todd');
SELECT empfname FROM qemp WHERE empno IN (SELECT bossno FROM qemp WHERE empfname = 'Sophie');
SELECT boss.empfname, emp.empfname FROM qemp AS emp, qemp AS boss WHERE emp.bossno = boss.empno ORDER BY boss.empfname, emp.empfname;
SELECT empfname, empsalary FROM qemp WHERE empsalary < (SELECT MIN(empsalary) FROM qemp WHERE deptname = 'Marketing');
Very similar to 35 on page 300.
SELECT DISTINCT deptname, itemname FROM qsale sale1 WHERE itemname NOT IN (SELECT itemname FROM qsale sale2 WHERE sale1.deptname <> sale2.deptname);
CREATE VIEW aavgdeptsal(deptname, dpavgsal) AS SELECT deptname, AVG(empsalary) AS dpavgsal FROM qemp group BY deptname;
Then complete the query:
SELECT deptname FROM avgdeptsal WHERE dpavgsal = (SELECT MAX(dpavgsal) FROM avgdeptsal);
This page is part of the promotional and support material for Data Management (open edition) by Richard T. Watson |