You are on page 1of 12

CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

Upload and run the script JL_BOOKSTORE downloaded from BBLearn. Write an SQL statement to
answer each of the following questions.

1. Create sequence for populating the Customer# column of the CUSTOMERS table. When setting
the start and increment values keep in mind that data already exists in this table.

CREATE SEQUENCE JL_CUST#_SEQ START WITH 1021;

2. Add a new customer using the above sequence. The only data currently available for the customer
is as follows:

Last name = Shoulders


First name = Frank
Zip = 23567

INSERT INTO JL_CUSTOMERS


(CUSTOMER#, LASTNAME, FIRSTNAME, ZIP)
VALUES
(JL_CUST#_SEQ.NEXTVAL, 'SHOULDERS', 'FRANK', 23567);

1|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

3. Drop the above sequence from the database.

DROP SEQUENCE JL_CUST#_SEQ;

4. Create an index on CUSTOMERS table to speed up queries that search for customers based on
their zip code.

CREATE INDEX JL_CUST_ZIP_IX ON JL_CUSTOMERS(ZIP);

Note: A UNIQUE index is automatically created on Primary Key column when you
add a Primary Key constraint.

5. Remove the above index from the database.

DROP INDEX JL_CUST_ZIP_IX;

Note: When you DROP a table, all indexes created on the table
are also dropped.

6. Modify the zip code on order 1017 to 33222.

UPDATE JL_ORDERS
SET SHIPZIP = 33222
WHERE ORDER# = 1017;

2|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

7. Set the cost of the book with ISBN 1059831198 to $20.00.

UPDATE JL_BOOKS
SET COST = 20.00
WHERE ISBN = 1059831198;

8. Delete order #1005. You need to delete both the master record and the related detail records.

JL_ORDERS table:

DELETE
FROM JL_ORDERS
WHERE ORDER# = 1005;

JL_ORDERITEMS table:

NOTE: As there is FOREIGN KEY relationship (dependency between ORDERITEMS and ORDERS tables), therefore
you cannot delete an ORDER if there are existing ITEMS in the ORDER. Therefore you have first delete the ITEMS
from the JL_ORDERITEMS table and then delete the ORDER from JL_ORDERS table.

DELETE
FROM JL_ORDERITEMS
WHERE ORDER# = 1005;

DELETE
FROM JL_ORDERS
WHERE ORDER# = 1005;

3|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

9. Create a new table containing these four columns from the BOOKS table: ISBN, COST,
RETAIL, CATEGORY. Name the New table as BOOK_PRICING.

CREATE TABLE BOOK_PRICING


AS
SELECT ISBN, COST, RETAIL, CATEGORY FROM JL_BOOKS ;

10. Truncate the table BOOK_PRICING.

TRUNCATE TABLE BOOK_PRICING;

11. Drop the table BOOK_PRICING permanently so that it is NOT moved to the recyclebin.

DROP TABLE BOOK_PRICING PURGE;

12. List the last name, first name and state of customers who live in New Jersey.

SELECT LASTNAME, FIRSTNAME, STATE


FROM JL_CUSTOMERS
WHERE STATE = 'NJ';

13. Which orders were shipped after April 1, 2009? List each order number and the date it shipped.

SELECT ORDER#, SHIPDATE


FROM JL_ORDERS
WHERE SHIPDATE > TO_DATE('April 1, 2009', 'Month DD, YYYY');

Or

SELECT ORDER#, SHIPDATE


FROM JL_ORDERS
WHERE SHIPDATE > '01-APR-2009';

4|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

14. Display the book title and category for all books in the children and Cooking Categories.
Create 3 different queries:
a. Use a search pattern operation hint: Use LIKE operator
b. Use a logical operator hint: Use either AND,OR,NOT Logical operators
c. Another operator not used in (a) or (b) hint: Use IN operator

SELECT TITLE, CATEGORY


FROM JL_BOOKS
WHERE CATEGORY LIKE 'C%N%';

SELECT TITLE, CATEGORY


FROM JL_BOOKS
WHERE CATEGORY = 'CHILDREN' OR CATEGORY = 'COOKING';

SELECT TITLE, CATEGORY


FROM JL_BOOKS
WHERE CATEGORY IN ('CHILDREN','COOKING');

15. List all customers who were referred to the bookstore by another customer. List each customer’s
last name, and the number of customer who made the referral.

SELECT LASTNAME, REFERRED


FROM JL_CUSTOMERS
WHERE REFERRED IS NOT NULL;

5|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

Instead of listing the ID’s of the referrals, the following code displays their lastnames. The following code
shows the JOIN condition to handle a UNARY relationship (also called as RECURSIVE relationship).

SELECT C.LASTNAME, R.LASTNAME AS REFERRED_BY


FROM JL_CUSTOMERS C, JL_CUSTOMERS R
WHERE C.REFERRED IS NOT NULL AND
C.REFERRED = R.CUSTOMER#;

16. Create a new table called NOREFERRALS with the following columns: customer#, lastname,
firstname, city. The new table should contain only those customers who have NOT been referred
by any other customer.

CREATE TABLE NOREFERRALS


AS
SELECT CUSTOMER#, LASTNAME, FIRSTNAME, CITY
FROM JL_CUSTOMERS
WHERE REFERRED IS NULL;

6|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

17. Drop the table NOREFERRALS so that it can be restored. Restore the NOREFERRALS table
and verify that it’s available again.

DROP TABLE NOREFERRALS;

FLASHBACK TABLE NOREFERRALS TO BEFORE DROP;

 Table is recovered from RECYCLEBIN

7|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

18. 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.

SELECT TITLE, CONTACT, PHONE


FROM JL_BOOKS B, JL_PUBLISHER P
WHERE B.PUBID = P.PUBID;

Note: The JOIN condition when you have more than 1 table in the FROM clause of the SELECT statement.
The above JOIN condition is an EQUI-JOIN since we are using the ‘=’ operator to join the FOREIGN
KEY column value of the child table (BOOKS) with the PRIMARY KEY column value in the parent table
(PUBLISHER).

OR

SELECT TITLE, CONTACT, PHONE


FROM JL_BOOKS NATURAL JOIN JL_PUBLISHER;

OR

SELECT TITLE, CONTACT, PHONE


FROM JL_BOOKS JOIN JL_PUBLISHER ON PUBID;

OR

SELECT TITLE, CONTACT, PHONE


FROM JL_BOOKS B JOIN JL_PUBLISHER P
ON (B.PUBID = P.PUBID); -- preferred --

8|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

19. Determine which book customer Jake Lucas has purchased. Perform the search using customer
name and not the customer number. If he has purchased multiple copies of the same book, un-
duplicate the results.

SELECT DISTINCT B.TITLE


FROM JL_CUSTOMERS C JOIN JL_ORDERS O
ON (C.CUSTOMER# = O.CUSTOMER#)
JOIN JL_ORDERITEMS I ON (O.ORDER# = I.ORDER#)
JOIN JL_BOOKS B ON (I.ISBN = B.ISBN)
WHERE C.FIRSTNAME = 'JAKE' AND C.LASTNAME = 'LUCAS';

Note: The keyword DISTINCT in the SELECT clause suppresses the duplicates.

9|P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

20. Identify the authors of the books Becca Nelson ordered. Perform the search using the customer
name.

Customer:
BECCA NELSON

10 | P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

21. List how many orders have been placed by each customer. Do not include in the
results any customer who has not placed any order with the bookstore.

SELECT CUSTOMER#, COUNT(*)


FROM JL_ORDERS
GROUP BY CUSTOMER#;

22. From the above list, print only those customers who have placed at least 2
orders.

23. List the names of those customers who have placed 2 or more orders.

A more detailed query would be:

SELECT C.FIRSTNAME || ' ' || C.LASTNAME AS CUSTOMER_NAME,


COUNT(*) AS NUMBER_OF_ORDERS
FROM JL_CUSTOMERS C JOIN JL_ORDERS O
ON (C.CUSTOMER# = O.CUSTOMER#)
GROUP BY C.FIRSTNAME || ' ' || C.LASTNAME
HAVING COUNT(*) >= 2 ;

11 | P a g e Habeeb Shah Khan Semester 1, 2019-20


CIA 4203 Enterprise Application Development SQL Practice (JL Bookstore)

Note: The above query can be saved as a view

CREATE VIEW CUST_ORD_VW


AS
SELECT C.FIRSTNAME || ' ' || C.LASTNAME AS CUSTOMER_NAME,
COUNT(*) AS NUMBER_OF_ORDERS
FROM JL_CUSTOMERS C JOIN JL_ORDERS O
ON (C.CUSTOMER# = O.CUSTOMER#)
GROUP BY C.FIRSTNAME || ' ' || C.LASTNAME
HAVING COUNT(*) >= 2 ;

Now instead of running a query from the tables, you can run a query on a view. A View is more like a
virtual table. But the benefits are that it hides the complexity of the query.

Also another benefit of the view is that the user will be given access on the view and not the tables.

SELECT *
FROM CUST_ORD_VW;

24. Create a view that lists the name and phone number of the contact person at each publisher. Don’t
include the publisher’s ID in the view. Name the view CONTACT.

CREATE VIEW CONTACT_VW


AS
SELECT NAME, CONTACT, PHONE
FROM JL_PUBLISHER ;

12 | P a g e Habeeb Shah Khan Semester 1, 2019-20

You might also like