Appendix B. Solutions to Exercises

Chapter 3

Exercise 3-1

Retrieve the actor ID, first name, and last name for all actors. Sort by last name and then by first name.

mysql> SELECT actor_id, first_name, last_name
    -> FROM actor
    -> ORDER BY 3,2;
+----------+-------------+--------------+
| actor_id | first_name  | last_name    |
+----------+-------------+--------------+
|       58 | CHRISTIAN   | AKROYD       |
|      182 | DEBBIE      | AKROYD       |
|       92 | KIRSTEN     | AKROYD       |
|      118 | CUBA        | ALLEN        |
|      145 | KIM         | ALLEN        |
|      194 | MERYL       | ALLEN        |
...
|       13 | UMA         | WOOD         |
|       63 | CAMERON     | WRAY         |
|      111 | CAMERON     | ZELLWEGER    |
|      186 | JULIA       | ZELLWEGER    |
|       85 | MINNIE      | ZELLWEGER    |
+----------+-------------+--------------+
200 rows in set (0.02 sec)

Exercise 3-2

Retrieve the actor ID, first name, and last name for all actors whose last name equals 'WILLIAMS' or 'DAVIS'.

 mysql> SELECT actor_id, first_name, last_name     -> FROM actor     -> WHERE last_name = 'WILLIAMS' OR last_name = 'DAVIS'; +----------+------------+-----------+ | actor_id | first_name | last_name | +----------+------------+-----------+ |        4 | JENNIFER   | DAVIS     | |      101 | SUSAN      | DAVIS     | |      110 | SUSAN      | DAVIS     | |       72 | SEAN       | WILLIAMS  | |      137 | MORGAN     | WILLIAMS  | |      172 | GROUCHO    | WILLIAMS  ...

Get Learning SQL, 3rd 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.