Lab 6 of 7: Oracle Single Row and Group Functions

Lab 6 of 7: Oracle Single Row and Group Functions

Lab 6 of 7: Oracle Single Row and Group Functions (28 points) 

            L A B O V E R V I E W      


Lab #6 will introduce the various aspects of the Single-Row and Group Functions available in the Oracle Database. Most functions can be used in either the SELECT statement or the WHERE clause, but more commonly are used in the SELECT. Chapters 5 and 6 cover most all of the normal processing you use in these functions. In some cases, you will be asked to used more than one function within a single SELECT statement.

General Lab Information and Considerations

This lab will utilize the same set of tables found in the script file (LeeBooks.SQL) that were used for Labs 4 and 5.

Each query in the script file you will create must be numbered (use --1 comments for numbering) and in order. The SQL for the following exercises should be written using notepad and run in SQL*Plus. Read each problem carefully and follow the directions as stated.

A Clean Script File:

A script file is meant to be like a program. The file can be run every time the code needs to be executed without having to retype the code again each time. For this reason, it is important that there are no errors in the code inside the file. You can go back and forth between notepad and Oracle when creating your script file to check your queries and verify if they work or not. However, you do not want to create your final output file until after you have verified that everything in your script is correct by running it, in its entirety at least once and viewing the output. Once this has been done, you can create your final output file, with echo to create the document, you can turn in with your lab. Remember in using a spool session, you must type "SPOOL OFF" at the SQL prompt after your script stops spooling to capture all of your data!

Lab Do's and Don’t's

Do Not include the LEEBOOKS.SQL as part of your lab script.

Do use Notepad to write your query script file.

Do Not write your queries in Word.

Do test each query before moving on to the next.

Do Not include extra queries for a problem unless the problem explicitly asks for more than one query.

Do test your queries before creating your final output file.

Do Not turn in a script file that has queries with errors.

Do number each query using - -1 comment notation.

Do Not start your query on the same line as the comment.

Do remember to check your final output and script file for accuracy.

Do Not turn in your lab without first checking your output file to verify that it is correct.

Things to keep in mind:

If you are not sure of the table names in your user schema, you can use the following select statement to list them.


If you want to know the name of the columns in a particular table you can use the following command to list them.


Making a script file containing a series of describe statements for each table and then spooling the output will give you a listing of all the tables with column names.

Be sure to review and verify your final output when you are finished. Do Not assume anything.

Write queries for each of the stated problems in the steps below that will return a result set of data to satisfy the requirements. When finished, your script file should have a total of 13 queries, and your resulting output file should show both the query and result set for each.


Submit for grading:

Your script file with the 13 queries in it. Be sure your name, course number, and lab number are in a comment area at the top of your file.

An output file created using SET ECHO ON showing both the SQL code and the results.

Both documents are to be zipped into a single file before submitting to the iLab Dropbox for Week 6.

            L A B S T E P S  

STEP 1:    

The following query creates a left out join between BOOKS and ORDER_ITEMS using the BOOKID attribute.  Books which have never been ordered will have null values in the ORDERID column.  Modify this query so that only books that have never been ordered are in the query results, and use the NVL function to change the value appearing under ORDERID for these books to ‘Never Ordered’.  Change the column heading for this attribute to “Ordered?”

SELECT b.bookid, b.isbn, b.title, o.orderid

FROM books b


 order_items o

ON  b.bookid = o.bookid;

STEP 2:       

Using the PUBLISHER table and the SUBSTR function, create a query that will return all of the fields from the table, but with PHONE numbers re-formatted similar to (###) ###-####. 

For example,  800-714-8321 would display as (800) 714-8321.

STEP 3:    

Using the BOOKS table, create a query using the correct functions to return the publisher id, the total cost of all books per publisher with a column alias of “Total Cost”, and the average cost of all the books per publisher with an alias of “Average Cost” for all publishers with a total cost greater than $20.00.  Format the total cost and average cost columns using the correct function to show dollars and cents with a dollar sign.

STEP 4:      
Using the correct tables in your schema, create a query using either join operation you wish that will list the PUBLISHERNAME and number of books printed by that publisher for publishers that offer more than two books. Give the PUBLISHERNAME column an alias of "Publisher” and the column showing the number of books an alias of "Number of Titles".

STEP 5:      

Using the BOOK_AUTHOR and AUTHOR tables for the upper most query, create a sub query that will return the BOOKID, and the first and last name of the author(s) who have written the book(s) that have been purchased the most. To successfully accomplish this, you will need to first find out the maximum total number of copies (based on quantity) sold of a book (based on bookid) in the ORDER_ITEMS table. This will be done in the bottom most query. The return from this query will be used to compare against the BOOKID in the ORDER_ITEMS table that occurs an equal number of times in the middle query. The return of this query will then be used to compare to a BOOKID related to an Author in the tables of the top most query. (HINT: Be careful, you will need both GROUP BY and HAVING clauses for your solution.)

STEP 6: 

Using the correct tables, create a sub query using either join operation you wish that will list the customer number, first and last name concatenated together, and zip for all customers who have placed an order for the least expensive book (based on retail price). Give the combined customer names column and alias of "Name of Customer".

STEP 7:       

Using the ORDER_ITEMS table, create a query using the correct functions to return the item#, the total quantity of items ordered with an alias of "Total", the average quantity per order with an alias of "Average" (since averages are often in decimals, only show two decimal places), the minimum quantity ordered with an alias of "Minimum", and the maximum quantity ordered with an alias of "Maximum". Your output results set should have five columns and four rows of data.

STEP 8:       

Using the BOOK_ORDER table, create a query using the DECODE function that will display the ordered, customerid, orderdate, shipdate, shipstreet, shipcity, the shipstate (decoded so that the two-character state abbreviation is replaced with the entire name of the state), and the shipzip.  Give the shipstate column an alias of "State", and order the results in ascending order by state.

STEP 9:    

Using the BOOK_CUSTOMER table, create a query that will return a line of output like the one below for each record in the table. Be sure to use the correct functions in the SELECT statement, so that your output looks like the example below.

Dear Bonita Morales of Eastpoint: You may already have won the Publisher’s Sweepstakes!

Be sure to use the correct function to display the first name and last name of the customer, and the customer’s city with only the first letter of each word capitalized.

STEP 10:      

Using the correct tables, create a query using the JOIN ON method to display a listing of all customers who have purchased fewer than three books. List the customer’s last name, the customer’s city and state, and the number of books they have purchased. Give the number purchased column and alias of "# Books Purchased".

STEP 11:    

Using only the BOOK table for the upper most statement, create a sub query that will update the retail price of all books published by PUBLISH OUR WAY by seven percent (retail * 1.07). Remember, all you have is the publisher "name". Use the sub query to find the publisher ID. To verify your update, provide a select statement on the BOOKS table before your update statement, and then a second select on the BOOKS table after the update. Once you have verified the results, you can issue a ROLLBACK statement to return the data to the original state.

STEP 12:         

Using the BOOK_CUSTOMER table, write a sub query that will return the names of those customers who were referred by the same individual who referred Jorge Perez. Do not display Jorge Perez’s name in the returned results (Hint: you should only have two customers displayed and the exclusion for Jorge Perez must be associated with the upper query).

STEP 13:  

Using the BOOKS table inner joined with the PUBLISHER table on PUBID, create a query using the correct functions to return the PUBLISHERNAME, total number books per publisher with a column alias of "Publisher Total", and the total cost of all the books per publisher with an alias of "Cost". Format the cost column using the correct function to show dollars and cents with a dollar sign.
Powered by