INFO I308 Lab 5: Advanced SQL Queries

40 points

Learning Objectives

Increased facility with SQL, including:

Overview

This assignment uses the same database schema as in Lab 4 for the bookworm database. It is divided into two parts. The first part requires select queries that are more complex than those in the previous lab. For example, a problem may require the group by, having, and/or order by clauses nested queries (“subselect”) or aggregation with grouping. The second part requires you to modify a copy of the database, using insert, update, and delete statements.

Advanced SELECT Queries

Solve the problems by connecting to the bookworm database on merlin.iue.edu. Use one SQL statement for each query. Nested queries count as a single SQL statement. Do not create any additional tables or views.

Problems

  1. For each author, find the author’s first and last names and the total number of pages of books written by the author. Present the results sorted by total pages in descending order.

  2. For each publisher, show the publisher’s name and the average price of books published by the publisher.

  3. For each publisher, show the publisher’s name and the average price per page of books published by the publisher. Average price per page here means the total price divided by the total number of pages for the set of books; it is not the average of (price/number of pages). Present the results sorted by average price per page in ascending order.

  4. Find the number of authors of each country.

  5. Find the names of authors who have written three or more books.

Modifying the Database

For the problems in this section, each student is to create and use his or her individual database on merlin.iue.edu. Each individual database is (initially) a copy of the bookworm database. The name of each individual database is formed by substituting the individual’s user name for USER in USER_bookworm. For example, user janedoe has database janedoe_bookworm. Do not attempt to modify the bookworm database used in earlier exercises, because your account does not have the privileges needed to do so.

Instructions for creating and using your database (substitute your own user name for USER):

General Hints

Problems

  1. Increase the price of each book published after 1960 by ten percent, using only one update statement.

  2. Reduce the prices of all of Charles Dickens’s books by 20 percent, using only one update statement.

  3. Delete all books published after 1985 (deleting tuples from both the author_book and book tables, using only two delete statements.)
  4. Insert tuples to represent the facts that we now have the book Induction by John H. Holland, Keith J. Holyoak, Richard E. Nisbett, and Paul R. Thagard; Thagard is Canadian, and the other authors are US citizens, of unknown dates; the book has 416 pages and was published by MIT Press in 1989; and its price is $38.00.
  5. After solving all the preceding problems, verify all changes made by executing the following queries exactly as shown:

    USER_bookworm-> select * from author 
                    order by alastname asc, afirstname asc;
    
    USER_bookworm-> select * from book
                    order by btitle asc;
    
    USER_bookworm-> select * from author_book
                    order by aid, bid;

What to Turn in

Turn in your SQL queries and the result of each query. The queries and results can be combined in one listing, or listed separately (one listing of queries, one listing of results).

As in Lab 4, identify each query and query result with its problem number (1 to 10). Do not include erroneous versions of your queries or commands. Just turn in the versions that worked.

Grading

10 problems, 4 points each, total 40 points.

Revisions