You are on page 1of 18

Lab Chapter # 9

Total Marks: 10

Student Name : Kovid Behl____________________________________________________

Student Id #: N01579154______________________________________________________

DO NOT ZIP THE FILE, JUST SEND AS A WORD DOCUMENT THROUGH BLACKBOARD I
WILL NOT ACCEPT SUBMISSION THROUGH EMAIL.

Hands-On Assignments
To perform the following assignment, refer to the tables created in the JLDB_Build_9.sql script
at the beginning of the chapter. Give the SQL statements and output for the following data
requests:

Generate and test two SQL queries for each of the following tasks:
a) the SQL statement needed to perform the stated task with the traditional approach, and
b) the SQL statement needed to perform the stated task with the JOIN keyword.
Apply table aliases in all queries.

1. Create a list that displays the title of each book and the name and phone number of the
contact at the publisher’s office for reordering each book.
a)
SELECT BOOKS.TITLE AS BOOK_TITLE,
PUBLISHER.NAME AS PUBLISHER_NAME,
PUBLISHER.PHONE AS PUBLISHER_NAME FROM BOOKS,
PUBLISHER WHERE BOOKS.PUBID=PUBLISHER.PUBID;

b)
SELECT BOOKS.TITLE AS BOOK_TITLE,
PUBLISHER.NAME AS PUBLISHER_NAME,
PUBLISHER.PHONE AS PUBLISHER_NAME
FROM BOOKS JOIN PUBLISHER USING(PUBID);
2. Determine which orders haven’t yet shipped and the name of the customer who placed
the order. Sort the results by the date on which the order was placed.
a)
SELECT Order#, LastName, FirstName
FROM ORDERS O, CUSTOMERS C
WHERE O.Customer#= C.Customer#
AND ShipDate IS NULL
ORDER BY OrderDate;
b)
SELECT Order#, LastName, FirstName
FROM ORDERS O JOIN CUSTOMERS C
USING(Customer#)
WHERE ShipDate IS NULL
ORDER By OrderDate;
3. Produce a list of all customers who live in the state of Florida and have ordered books
about computers.
a)
SELECT C.CUSTOMER#,
C.FIRSTNAME ||' '|| C.LASTNAME AS NAME,O.ORDER#, OI.ISBN, B.CATEGORY, C.STATE
FROM CUSTOMERS C, ORDERS O, BOOKS B, ORDERITEMS OI
WHERE C.CUSTOMER# = O.CUSTOMER# AND O.ORDER# = OI.ORDER#
AND OI.ISBN = B.ISBN AND STATE = 'FL' AND B.CATEGORY = 'COMPUTER';
b)
SELECT customer#, c.firstname||' '||c.lastname as name, order#, ISBN, b.category,
c.state
FROM customers c JOIN orders o USING (customer#)
JOIN orderitems oi USING (order#) JOIN books b USING (ISBN)
WHERE state = 'FL' AND category = 'COMPUTER';
4. Determine which books customer Jake Lucas has purchased. Perform the search using
the customer’s name, not the customer number. If he has purchased multiple copies of
the same book, unduplicate the results.
a)
SELECT DISTINCT C.FIRSTNAME|| ' ' ||
C.LASTNAME "CUSTOMER NAME",
B.TITLE FROM CUSTOMERS C,
ORDERS O, ORDERITEMS OI, BOOKS B
WHERE (C.FIRSTNAME|| ' ' ||
C.LASTNAME) = 'JAKE LUCAS' AND
C.CUSTOMER# = O.CUSTOMER#
AND OI.ORDER# = O.ORDER#
AND B.ISBN = OI.ISBN;
b)
SELECT DISTINCT c.firstname|| ' ' ||c.lastname "NAME",
b.title FROM customers c INNER JOIN orders o
ON c.customer# = o.customer# INNER JOIN
orderitems oi ON oi.order# = o.order#
LEFT JOIN books b ON b.ISBN = oi.ISBN
WHERE (c.firstname|| ' ' ||c.lastname) = 'JAKE LUCAS';
5. Determine the profit of each book sold to Jake Lucas, using the actual price the
customer paid (not the book’s regular retail price). Sort the results by order date. If
more than one book was ordered, sort the results by profit amount in descending order.
Perform the search using the customer’s name, not the customer number.
a)
SELECT BOOKS.TITLE AS BOOK_TITLE,
SELECT orders.orderdate, books.title, (orderitems.paideach-books.cost) "PROFIT"
FROM CUSTOMERS NATURAL JOIN orders NATURAL JOIN orderitems
NATURAL JOIN books WHERE customers.firstname = 'JAKE'
AND customers.lastname = 'LUCAS' ORDER BY profit DESC, orderdate;PUBLISHER.PHONE
AS PUBLISHER_NAME FROM BOOKS,
PUBLISHER WHERE BOOKS.PUBID=PUBLISHER.PUBID;

b)
SELECT o.orderdate, b.title, (oi.paideach-b.cost) "PROFIT"
From CUSTOMERS c JOIN orders o USING (customer#)
JOIN orderitems oi USING (order#) JOIN books b USING (ISBN)
WHERE c.firstname = 'JAKE' AND c.lastname = 'LUCAS' ORDER BY profit DESC, orderdate;
6. Which books were written by an author with the last name Adams? Perform the search
using the author’s name.
a)
SELECT B.TITLE FROM BOOKS B, BOOKAUTHOR BA, AUTHOR A
WHERE B.ISBN = BA.ISBN AND BA.AUTHORID = A.AUTHORID AND A.LNAME = 'ADAMS';
b)
SELECT title FROM books
JOIN bookauthor ON books.isbn = bookauthor.isbn
JOIN author ON bookauthor.authorid = author.authorid WHERE lname = 'ADAMS';
7. What gift will a customer who orders the book Shortest Poems receive? Use the actual
book retail value to determine the gift.
a)
SELECT p.gift FROM books, promotion p
WHERE retail BETWEEN minretail
AND maxretail AND title = 'SHORTEST POEMS';
b)
SELECT p.gift FROM books
JOIN promotion p ON retail BETWEEN minretail
AND maxretail WHERE title = 'SHORTEST POEMS';

8. Identify the authors of the books Becca Nelson ordered. Perform the search using the
customer’s name.
a)

SELECT a.lname, a.fname, b.title FROM books b, orders o, orderitems i, customers c,


bookauthor t, author a
WHERE c.customer# = o.customer#
AND firstname = 'BECCA'
AND lastname = 'NELSON'
AND o.order# = i.order#
AND i.isbn = b.isbn
AND b.isbn = t.isbn
AND t.authorid = a.authorid;
b)

SELECT a.lname, a.fname, b.title FROM customers c JOIN orders o ON c.customer# =


o.customer#
JOIN orderitems i ON o.order# = i.order#
JOIN books b ON i.isbn = b.isbn JOIN bookauthor t ON b.isbn = t.isbn JOIN author a ON
t.authorid = a.authorid
WHERE firstname = 'BECCA' AND lastname = 'NELSON';

9. Display a list of all books in the BOOKS table. If a book has been ordered by a customer,
also list the corresponding order number and the state in which the customer resides.
a)
SELECT b.title, o.order#, c.state
FROM books b, orders o, orderitems i, customers c
WHERE c.customer#(+) = o.customer#
AND i.isbn(+) = b.isbn
AND o.order#(+) = i.order#;
b)
SELECT title, o.order#, c.state
FROM books b
LEFT OUTER JOIN orderitems i ON i.isbn = b.isbn
LEFT OUTER JOIN orders o ON o.order# = i.order#
LEFT OUTER JOIN customers c
ON c.customer# = o.customer#
10. An EMPLOYEES table was added to the JustLee Books database to track employee
information. Display a list of each employee’s name, job title, and manager’s name. Use
column aliases to clearly identify employee and manager name values. Include all
employees in the list and sort by manager name.
a)
SELECT e.fname || ' ' || e.lname "Employee Name", e.job,m.fname || ' ' || m.lname
"Manager Name"
FROM employees e, employees m
WHERE e.mgr = m.empno (+)
ORDER BY "Manager Name";
b)
SELECT e.fname || ' ' || e.lname "Employee Name", e.job,m.fname || ' ' || m.lname
"Manager Name"
FROM employees e
LEFT OUTER JOIN employees m
ON e.mgr = m.empno
ORDER BY "Manager Name";

Advanced Challenge
To perform this activity, refer to the tables in 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 free 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
advertisements, JustLee Books is reimbursed a certain percentage of the advertisement costs.
To determine the projected costs of this year’s sales promotion, the Marketing Department
needs the publisher’s name, profit amount, and free gift description for each book in the
JustLee Books inventory.
Also, the Marketing Department is analyzing books that don’t sell. A list of ISBNs for all books
with no sales recorded is needed. Use a set operation to complete this task.

Create a document that includes a synopsis of these requests, the necessary SQL
statements, and the output requested by the Marketing Department.
SELECT title, publisher.name publisher_name,
TO_CHAR(books.retail-books.cost,'$999.99') as profit, promotion.gift
FROM books, publisher, promotion
WHERE books.pubid =publisher.pubid
AND books.retail BETWEEN promotion.minretail AND promotion.maxretail
ORDER BY profit;
SELECT title, publisher.name publisher_name,
TO_CHAR(books.retail-books.cost,'$999.99') as profit, promotion.gift
FROM books JOIN publisher
ON books.pubid =publisher.pubid
JOIN promotion
ON books.retail
BETWEEN promotion.minretail AND promotion.maxretail
ORDER BY profit;
Query to get only ISBNs for all books with no sales:
SELECT isbn FROM books
MINUS
SELECT isbn FROM orderitems;

Query to get ISBN and title for all books with no sales:
SELECT isbn, title FROM books
MINUS
SELECT orderitems.isbn, title FROM orderitems, books
WHERE orderitems.isbn = books.isbn;

You might also like