Different SQL Queries
Different SQL Queries
Exercises
Skill builders
Instructor
Slide exercises
Support
SQL Playbook
All the queries in the SQL Playbook reference are available for copying AND pasting so that you can run them
without needing to retype.
2. Skinning a cat
Find the items sold by a department on the second floor.
2.1 Join
SELECT DISTINCT itemname FROM qsale, qdept
WHERE qdept.deptname = qsale.deptname
AND deptfloor = 2;
open in browser PRO version
pdfcrowd.com
2.2 In
SELECT DISTINCT itemname FROM qsale
WHERE deptname IN
(SELECT deptname FROM qdept WHERE deptfloor = 2);
2.3 Correlated subquery
SELECT DISTINCT itemname FROM qsale
WHERE deptname IN
(SELECT deptname FROM qdept
WHERE qdept.deptname = qsale.deptname
AND deptfloor = 2);
2.4 Exists
SELECT DISTINCT itemname FROM qsale
WHERE EXISTS
(SELECT * FROM qdept
WHERE qsale.deptname = qdept.deptname
AND deptfloor = 2);
pdfcrowd.com
5. Dividing
Find the items sold by all departments on the second floor.
SELECT DISTINCT itemname FROM qitem
WHERE NOT EXISTS
(SELECT * FROM qdept WHERE deptfloor = 2
AND NOT EXISTS
(SELECT * FROM qsale
WHERE qsale.itemname = qitem.itemname
AND qsale.deptname = qdept.deptname));
SELECT qsale.itemname FROM qsale, qdept
WHERE qsale.deptname = qdept.deptname
AND qdept.deptfloor = 2
GROUP BY qsale.itemname
HAVING count(distinct qdept.deptname) =
(SELECT count(distinct deptname)
from qdept WHERE deptfloor = 2);
pdfcrowd.com
Find the items sold by at least two departments on the second floor.
SELECT itemname FROM qsale, qdept
WHERE qsale.deptname = qdept.deptname AND deptfloor = 2
GROUP BY itemname
HAVING COUNT(DISTINCT qdept.deptname) > 1;
pdfcrowd.com
pdfcrowd.com
List the departments WHERE the average salary of the employees of each manager is more than $25,000.
SELECT wrk.deptname, AVG(wrk.empsalary)
FROM qemp wrk
WHERE wrk.empno NOT IN
(SELECT qdept.empno FROM qdept
WHERE wrk.empno = qdept.empno
AND wrk.deptname = qdept.deptname)
GROUP BY wrk.deptname
HAVING AVG(wrk.empsalary) > 25000;
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
List the suppliers that deliver all AND only the items sold by the Equipment department.
SELECT splname FROM qspl
WHERE NOT EXISTS
(SELECT * FROM qsale
WHERE deptname = 'Equipment'
AND itemname NOT IN
(SELECT itemname FROM qdel
WHERE qdel.splno = qspl.splno))
AND NOT EXISTS
(SELECT * FROM qdel
WHERE qdel.splno = qspl.splno
AND itemname NOT IN
(SELECT itemname FROM qsale
WHERE deptname = 'Equipment'));
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
For each item, give its type, the departments that sell the item, AND the floor location of these
departments.
SELECT qitem.itemname, itemtype, qdept.deptname, deptfloor
FROM qitem, qsale, qdept
WHERE qsale.itemname = qitem.itemname
AND qsale.deptname = qdept.deptname;
pdfcrowd.com
pdfcrowd.com
List the items delivered by Nepalese Corp. or sold in the Navigation department.
SELECT DISTINCT itemname FROM qitem
WHERE itemname IN
(SELECT itemname FROM qdel, qspl
WHERE qdel.splno = qspl.splno
AND splname = 'Nepalese corp.')
OR itemname IN
(SELECT itemname FROM qsale
WHERE deptname = 'Navigation');
43. Intersection/union
List the departments selling items of type E that are delivered by Nepalese Corp. AND/or which are sold by
the Navigation department.
SELECT DISTINCT deptname FROM qsale
WHERE itemname IN
(SELECT qitem.itemname FROM qitem, qdel, qspl
WHERE qitem.itemname = qdel.itemname
AND qdel.splno = qspl.splno
AND splname = 'Nepalese corp.'
AND itemtype = 'e')
or itemname in
(SELECT itemname FROM qsale
WHERE deptname = 'Navigation');
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
pdfcrowd.com
List the employees in the Accounting department AND the difference between their salaries AND the
average salary of all the departments.
CREATE VIEW v53(allavgsal) AS
SELECT AVG(empsalary) FROM qemp;
SELECT empfname, (empsalary - allavgsal) FROM v53, qemp
WHERE deptname = 'Accounting';
pdfcrowd.com
pdfcrowd.com
61. No Booleans
Is it true that all the departments that sell items of type C are located on the third floor? (The result can be
a Boolean 1 or 0, meaning yes or no.)
SELECT COUNT(*) FROM qdept
WHERE deptfloor <> 3
AND exists
(SELECT * FROM qsale, qitem
WHERE qsale.itemname = qitem.itemname
AND qsale.deptname = qdept.deptname
open in browser PRO version
pdfcrowd.com
pdfcrowd.com