Lab 5 deals with writing queries using multiple tables as covered in the reading and lecture material this week. In some cases, you will be given the option to write a query using either the TRADITIONAL method of joining tables or using the JOIN key word. In some cases, you will be asked to use a specific approach. Using the incorrect process when the process is specified will result in points being taken off for that problem.
The SELECT statement is the primary means of extracting data from database tables, and allows you to determine exactly which data you want to extract by means of different comparison operators used in the WHERE clause. This includes the use of specific "wild card" characters which allow you to search for character or number patterns within the data. You can also perform mathematical expressions within the SELECT statement to create derived output. The ORDER BY clause allows you to sort the output data in either ascending (the default) or descending order. Lab #5 will explore all of these applications of the SELECT statement.
General Lab Information and Considerations
This lab will utilize a set of tables found in the script file ([url removed, login to view]) found in the Doc Sharing area of the website. If you have not yet downloaded this file from Doc Sharing then do so now. Once you have downloaded the script file, import and run the script in SQL*Plus.
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.
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, but 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 on to create the document you can turn in with your lab. If using a Spool Session, be sure to type "SPOOL OFF" AT THE SQL> after your Script Stops Spooling!
Lab Do's and Don't's
Do Not include the [url removed, login to view] 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.
SELECT * FROM TAB;
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. Submitting ONLY your spooled output will result in a 10-point deduction.
An output file created using SET ECHO ON showing both the SQL code and the results.
Both documents must be zipped into a single file before submitting to the iLab Dropbox for Week 5.
L A B S T E P S
Using the BOOKS and PUBLISHER tables, create a query using the traditional join method that will return a list containing the book title, publisher contact person, and publisher phone number for all publishers whose publisher name starts with an ‘R'.
Using the DUAL table, create a query that will return the day of the week, hour, minutes, and seconds of the current date setting on a computer. The day should be in all upper case.
Using the BOOK_CUSTOMER and BOOK_ORDER tables, create a query using the NATURAL JOIN method that will return a list containing the customer first name and last name and the order number for all orders that have been shipped. Give the customer name column an alias of "Customer Name" and order the output by the customer number in the BOOK_ORDER table in ascending order.
Using the BOOK_ORDER table, create a query that will return the order number, order date, shipping address, city, state, and zip code for all orders going to Atlanta or Austin. Format the order date to display as Month DD, YYYY and give the column an alias of "Date Ordered". Be sure the month begins with a capital letter.
Using the BOOK_ORDER table, create a query using the correct function to return the order number, the date ordered, the date shipped, and a column representing the number of months between the two dates for all columns where a date shipped exists. Format the number returned from the function to display only two decimals, and give the column an alias of "Months Between".
NOTE: Be sure that all of the numbers in the fourth column are positive numbers.
Using the correct tables in your schema, create a query using either join operation that will list the title of each book and author name(s). Give the title column an alias of "Book Title". Sort the results by title and then by author last name.
Using the BOOKS table, create a query that will return the book title, cost, and retail price for all books with a title starting with the letter ‘H'. Use the correct conversion function to format the cost and retail columns to show dollars and cents with a dollar sign (e.g., a price of $25 would display $25.00 in the result set).
Using the BOOK_ORDER, ORDER_ITEMS, and BOOKS tables, create a query using an OUTER JOIN operation that will list the book title, order date, and order number for all books in the BOOKS table. Order your output in descending order by ORDER_ITEMS.BOOKID. There are three books that have never been ordered which should show up at the top of your listing.
Using the correct tables, create a query using the traditional join operation that will list the customer first and last name, book title, and order date (formatted as MM/DD/YYYY with an alias of “Order Date”) for all the customers who have purchased books published by 'PRINTING IS US'.
Using the BOOKS and ORDER_ITEMS table, write a query using the correct Relational Set Operator that will show all of the Book IDs in the BOOKS table that have not been ordered.
Using the BOOK_CUSTOMER, BOOK_ORDER, ORDER_ITEMS, and BOOKS tables, create a query using traditional join conditions based on comparisons between primary and foreign keys that will list the customer number, first and last name, and book title. Limit your listing to only those books in the ‘FITNESS' category.
Using the BOOKS, ORDER_ITEMS, and BOOK_ORDER tables, create a query that will list the title, retail, quantity, and order date for all books ordered after April 30, 2009.
Using the correct tables, create a query using either join operation you wish that will list the order id, order date, quantity ordered, and retail price for every book that has been ordered. Format the date as MM/DD/YYYY with an alias of “Order Date” and format the retail price column using the correct function to show dollars and cents with a dollar sign ( $ ) and a column alias of “Retail”.