Appendix C. Solutions to Exercises

Chapter 3

3-1

Retrieve the employee ID, first name, and last name for all bank employees. Sort by last name and then by first name.

mysql> SELECT emp_id, fname, lname
    -> FROM employee
    -> ORDER BY lname, fname;
+--------+----------+-----------+
| emp_id | fname    | lname     |
+--------+----------+-----------+
|      2 | Susan    | Barker    |
|     13 | John     | Blake     |
|      6 | Helen    | Fleming   |
|     17 | Beth     | Fowler    |
|      5 | John     | Gooding   |
|      9 | Jane     | Grossman  |
|      4 | Susan    | Hawthorne |
|     12 | Samantha | Jameson   |
|     16 | Theresa  | Markham   |
|     14 | Cindy    | Mason     |
|      8 | Sarah    | Parker    |
|     15 | Frank    | Portman   |
|     10 | Paula    | Roberts   |
|      1 | Michael  | Smith     |
|      7 | Chris    | Tucker    |
|     18 | Rick     | Tulman    |
|      3 | Robert   | Tyler     |
|     11 | Thomas   | Ziegler   |
+--------+----------+-----------+
18 rows in set (0.01 sec)

3-2

Retrieve the account ID, customer ID, and available balance for all accounts whose status equals 'ACTIVE' and whose available balance is greater than $2,500.

mysql> SELECT account_id, cust_id, avail_balance
    -> FROM account
    -> WHERE status = 'ACTIVE'
    ->   AND avail_balance > 2500; +------------+---------+---------------+ | account_id | cust_id | avail_balance | +------------+---------+---------------+ | 3 | 1 | 3000.00 | | 10 | 4 | 5487.09 | | 13 | 6 | 10000.00 | | 14 | 7 | 5000.00 | | 15 | 8 | 3487.19 | | 18 | 9 | 9345.55 | | 20 | 10 | 23575.12 | | 22 | 11 | 9345.55 | | 23 | 12 | 38552.05 | | 24 | 13 | 50000.00 | +------------+---------+---------------+ 10 rows ...

Get Learning SQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.