Inf3707 Tut201 2018 Assignmnet 02solution Semester 01
Inf3707 Tut201 2018 Assignmnet 02solution Semester 01
INF3707/202/01/2018
School of Computing
BARCODE
Question 1: Chapter 5
Currently, the contents of the Category column in the BOOKS table are the actual names for
each category. This structure presents a problem if one user enters COMPUTER for the
Computer category and another user enters COMPUTERS. To avoid this and other problems
that might occur, the database designers have decided to create a CATEGORY table containing
a code and description for each category. The structure for CATEGORY table should be as
follows:
1.2 Populate the Category table with the given data. Save the changes permanently (8
marks)
INF3707
COMMIT;
1.3 Add a column to the BOOKS table called Catcode. Add a FOREIGN KEY constraint that
requires all category codes entered in the BOOKS table to already exist in the
CATEGORY table. (4 marks)
1.4 Set the Catcode values for the rows in the BOOKS table based on each book’s current
Category value. (8 marks)
UPDATE books
SET catcode = 'BUS'
WHERE category = 'BUSINESS';
UPDATE books
SET catcode = 'CHN'
WHERE category = 'CHILDREN';
UPDATE books
SET catcode = 'COK'
WHERE category = 'COOKING';
UPDATE books
SET catcode = 'COM'
WHERE category = 'COMPUTER';
UPDATE books
SET catcode = 'FAL'
WHERE category = 'FAMILY LIFE';
UPDATE books
SET catcode = 'FIT'
WHERE category = 'FITNESS';
UPDATE books
SET catcode = 'SEH'
UPDATE books
SET catcode = 'LIT'
WHERE category = 'LITERATURE';
COMMIT;
Or
UPDATE BOOKS
SET BOOKS.CATCODE = (SELECT CATEGORY.CATCODE
FROM CATEGORY
WHERE CATEGORY.CATDESC = BOOKS.CATEGORY)
WHERE CATCODE IS NULL;
1.5 Verify that the correct categories have been assigned in the BOOKS table, and save
changes permanently. (1 marks)
1.6 Delete the Category column from the BOOKS table. (1 marks)
Question 2 Chapter 8
Give SQL statements for the following requests.
2.1 Which customers live in Georgia or New Jersey? Put the results in ascending order by last
name. List each customer’s customer number, last name and the state. (5 marks)
Answer;
SELECT customer#, lastname, state
FROM customers
WHERE state = 'GA' or state = 'NJ' //(Note that Georgia and New Jersey are
not cities)
ORDER BY lastname;
Or
2.2 List authors whose last name contains the letter pattern “IN”. Put the results in order of last
name, then first name. List each author’s last name and first name. (5 marks)
Answer
SELECT lname, fname
FROM author
4
INF3707
Question 3 Chapter 9
To perform this activity, refer to the tables of the JustLee Books database
The marketing Department of JustLee Books is preparing for its annual sales promotion. Each
customer who places an order during the promotion will receive a gift with each book
purchased. Each gift will be based on the book’s retail price. JustLee Books also participates in
co-op advertising programs with certain publishers. If the publisher’s name is included in the
advertisements, JustLee Books is reimbursed a certain amount of the advertisement cost.
3.1. To determine the projected cost of this year’s sales promotion, the marketing Department
needs the publisher’s name, profit amount and the gift description for each book in the JustLee
Books inventory. Use join operator to create a report for the above request. (3 marks)
Answer
3.2 The Marketing Department is analysing books that do not sell. List the ISBN for books with
no sales recorded. Use a set operator to complete this task. (2 marks)
Answer
SELECT isbn
FROM books
MINUS
SELECT isbn
FROM orderitems;
Question 4: Chapter 10
Give SQL statements for the following requests.
4.1 Determine the amount of total profit generated by the book purchased on order 1002.
Display the book title and profit. The profit should be formatted to display a dollar sign and two
decimal places. Take into account that the customer might not pay the full retail price and each
item ordered can involve multiple copies. (5 marks)
Answer ((make sure that the formula is correct, if wrong do not give marks. Note that other
students may use different formulars, please check if they are correct )
4.2 Display a list of all book titles and percentage of mark-up for each book. The percentage of
mark-up should be displayed as a whole number (that is, multiplied by 100) with no decimal
position, followed by a percent sign (for example, 0.2793 = 28%). The percentage of mark-up
5
should reflect the difference between the retail and cost amounts as a percent of the cost.).
(5 marks)
Answer (make sure that the formula is correct, if wrong do not give marks. Note that some
students may use different formulas from this but correct)
Question 5: Chapter 11
Give SQL statements for the following requests.
5.1 Determine which books cost less than the average cost of other books in the same
category. Use subqueries to answer the question. (5 marks)
Answer (make sure subqueries are used, if not do not give marks. Note that other students
may manualy calculate the average)
SELECT a.title, b.category, a.cost
FROM books a, (SELECT category, AVG(cost) averagecost
FROM books
GROUP BY category) b
WHERE a.category = b.category
AND a.cost < b.averagecost;
5.2 Determine which books were shipped to the same state as order 1014. Use subqueries to
answer the question. (5 marks)
Answer
SELECT order#
FROM orders
WHERE shipstate = (SELECT shipstate
FROM orders
WHERE order# = 1014);