Professional Documents
Culture Documents
SQL Practice (JL - Books) - Solution
SQL Practice (JL - Books) - Solution
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.
2. Add a new customer using the above sequence. The only data currently available for the customer
is as follows:
4. Create an index on CUSTOMERS table to speed up queries that search for customers based on
their zip code.
Note: A UNIQUE index is automatically created on Primary Key column when you
add a Primary Key constraint.
Note: When you DROP a table, all indexes created on the table
are also dropped.
UPDATE JL_ORDERS
SET SHIPZIP = 33222
WHERE ORDER# = 1017;
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;
9. Create a new table containing these four columns from the BOOKS table: ISBN, COST,
RETAIL, CATEGORY. Name the New table as BOOK_PRICING.
11. Drop the table BOOK_PRICING permanently so that it is NOT moved to the recyclebin.
12. List the last name, first name and state of customers who live in New Jersey.
13. Which orders were shipped after April 1, 2009? List each order number and the date it shipped.
Or
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
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.
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).
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.
17. Drop the table NOREFERRALS so that it can be restored. Restore the NOREFERRALS table
and verify that it’s available again.
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.
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
OR
OR
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.
Note: The keyword DISTINCT in the SELECT clause suppresses the duplicates.
20. Identify the authors of the books Becca Nelson ordered. Perform the search using the customer
name.
Customer:
BECCA NELSON
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.
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.
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.