ACTIVITY 4-2 ANSWERS 1. List all client names with branches that use the "Accounting" service (service ID 1). Breakdown: outer query: client names (IN) inner query: branches (IN) inner query 2: service id mysql> SELECT cname FROM clients WHERE cid IN (SELECT cid FROM branches WHERE bid IN (SELECT bid FROM branches_services WHERE sid = 1)); +-----------------------------+ | cname | +-----------------------------+ | JV Real Estate | | Rabbit Foods Inc | | Sharp Eyes Detective Agency | +-----------------------------+ 3 rows in set (0.00 sec) 2. List all clients with individual branch offices having a monthly bill of $2000 or more Breakdown: outer query: client names (IN) inner query: branches (IN) inner query 2: service fee total $2000 or more (GROUP BY, HAVING, SUM) mysql> SELECT cname FROM clients WHERE cid IN (SELECT cid FROM branches WHERE bid IN (SELECT bid FROM branches_services AS bs, services AS s WHERE bs.sid = s.sid GROUP BY bid HAVING SUM(sfee) >= 2000)); +-----------------------------+ | cname | +-----------------------------+ | JV Real Estate | | Rabbit Foods Inc | +-----------------------------+ 2 rows in set (0.00 sec) 3. Rewrite the query above using the NOT operator. You should get the same answer as the query above. Breakdown: outer query: client names (IN) inner query: branches (NOT IN) inner query 2: branches_service (GROUP BY, HAVING, SUM) mysql> SELECT cname FROM clients WHERE cid IN (SELECT cid FROM branches WHERE bid NOT IN (SELECT bid FROM branches_services AS bs, services AS s WHERE bs.sid = s.sid GROUP BY bid HAVING SUM(sfee) < 2000)); +-----------------------------+ | cname | +-----------------------------+ | JV Real Estate | | Rabbit Foods Inc | +-----------------------------+ 2 rows in set (0.00 sec) 4. List the average services each branch office uses. Breakdown: outer query: inner query: AVG COUNT branches_services HINT: The inner query must first be aliased to a table name, or else MySQL will not know how to refer to columns within it. If you don’t use an alias the result will be: ERROR 1248 (42000): Every derived table must have its own alias SELECT AVG(z.stotal) FROM (SELECT bid, COUNT(sid) AS stotal FROM branches_services GROUP BY bid) AS z; The correct result should be: +---------------+ | AVG(z.stotal) | +---------------+ | 1.7778 | +---------------+ 1 row in set (0.00 sec) 5. Now that you know the average. List the branches where the number of services is above this average. mysql> SELECT bid FROM branches_services GROUP BY bid HAVING COUNT(sid) > 1.7778; +------+ | bid | +------+ | 1011 | | 1031 | | 1041 | | 1042 | +------+ 4 rows in set (0.00 sec) 6. Now combine the above two queries into one. You should get the same result/output as Question #5. Breakdown: outer query: inner query: Query from #5 Query from #4 mysql> SELECT bid FROM branches_services GROUP BY bid HAVING COUNT(sid) > (SELECT AVG(z.stotal) FROM (SELECT bid, COUNT(bid) AS stotal FROM branches_services GROUP BY bid) AS z); +------+ | bid | +------+ | 1011 | | 1031 | | 1041 | | 1042 | +------+ 4 rows in set (0.00 sec) 7. Now, (just because I’m mean), list the client names corresponding to the branches listed above. Breakdown: Add an outer query to the query from #6 (DISTINCT) mysql> SELECT DISTINCT cname FROM clients, branches, (SELECT bid FROM branches_services GROUP BY bid HAVING COUNT(sid) > (SELECT AVG(z.stotal) FROM (SELECT bid, COUNT(bid) AS stotal FROM branches_services GROUP BY bid) AS z)) AS x WHERE clients.cid = branches.cid AND branches.bid = x.bid; +------------------+ | cname | +------------------+ | JV Real Estate | | Rabbit Foods Inc | | DMW Trading | +------------------+ 3 rows in set (0.00 sec) 8. Delete all branches using the "Recruitment" service (service ID 2). Breakdown: outer query: inner query: Branches (DELETE, IN) Branches_services, sid=2 mysql> DELETE FROM branches WHERE bid IN (SELECT bid from branches_services WHERE sid = 2); Query OK, 2 rows affected (0.00 sec) 9. Check the branches to make sure the query worked. mysql> select * from branches; +------+-----+--------------------------------+------+ | bid | cid | bdesc | bloc | +------+-----+--------------------------------+------+ | 1012 | 101 | Accounting Department | NY | | 1013 | 101 | Customer Grievances Department | KA | | 1041 | 104 | Branch Office (East) | MA | | 1042 | 104 | Branch Office (West) | CA | | 1101 | 110 | Head Office | CA | | 1032 | 103 | NE Region HO | CT | | 1033 | 103 | NW Region HO | NY | +------+-----+--------------------------------+------+ 7 rows in set (0.00 sec) 10. Delete all those customers, any of whose branch offices generate service fee revenues of $500 or less Breakdown: outer query: inner query: Clients (DELETE, IN) Branches_services (DISTINCT, GROUP BY, HAVING, SUM) mysql> DELETE FROM clients WHERE cid IN (SELECT DISTINCT b.cid FROM branches AS b, branches_services AS bs, services AS s WHERE b.bid = bs.bid AND bs.sid = s.sid GROUP BY bs.bid HAVING SUM(sfee) <= 500); Query OK, 1 row affected (0.00 sec) 11. Check the clients table to make sure the query worked. mysql> select * from clients; +-----+-----------------------------+ | cid | cname | +-----+-----------------------------+ | 101 | JV Real Estate | | 102 | ABC Talent Agency | | 104 | Rabbit Foods Inc | | 110 | Sharp Eyes Detective Agency | +-----+-----------------------------+ 4 rows in set (0.00 sec) 12. Find out which services are in use in 3 or more branch offices mysql> SELECT sid FROM branches_services GROUP BY sid HAVING COUNT(bid) >= 3; +-----+ | sid | +-----+ | 1 | | 3 | | 4 | +-----+ 3 rows in set (0.00 sec) 13. Now increase the fee for the above services by 25%. Breakdown: outer query: inner query: Services, sfee (UPDATE) Branches_services (GROUP BY, HAVING, COUNT) mysql> UPDATE services SET sfee = sfee + (sfee * 0.25) WHERE sid IN (SELECT sid FROM branches_services GROUP BY sid HAVING COUNT(bid) >= 3); Query OK, 3 rows affected (0.22 sec) Rows matched: 3 Changed: 3 Warnings: 0 14. Change all branches located in California so that they use the "Security" service instead of the "Administration" service Breakdown: outer query: inner query: Branches_services, sfee (UPDATE, SET, IN) Branches (bloc=’CA’) mysql> UPDATE branches_services SET sid = 6 WHERE sid = 4 AND bid IN (SELECT bid FROM branches WHERE bloc = 'CA'); Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0