You are on page 1of 14

Lab Chapter # 2 (Due Date: Thrusday, Septemper 22nd @ 11:55pm)

Total Marks: 10

Student Name : Kovid Behl____________________________________________________

Student Id #: N01579154______________________________________________________

Lab exercises You need to be present in the class for your lab to be graded and are to be
submitted within 3 Hours after the class, failing to do the will result in ZERO grade.

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

PLEASE MAKE SURE THAT YOU CAPUTURE THE SQL STATEMENT AND THE
RESULT/OUTPUT OF EACH QUESTION AND SUBMIT ALL OF THEM AS ONE
DOCUMENT. FOLLOW THE EXAPLE OF QUESTION # 1.
Review Questions
1. What is a data dictionary?
A data dictionary is a repository of metadata, or information about a database. It is vital
as it holds details such as the database's contents, who has access to it, and the location of
the database. The data dictionary is typically managed by database administrators and is
not typically accessed by general users.

2. What are the two required clauses for a SELECT statement?


The two required clauses for SELECT statement are SELECT and FROM.

3. What is the purpose of the SELECT statement?


The purpose of SELECT statements is to retrieve data from the database. A SELECT
statement is also referred to as a query

4. What does the use of an asterisk (*) in the SELECT clause of a SELECT statement
represent?
This is used to retrieve every column in the table.

5. What is the purpose of a column alias?


We use column aliases to display the another column name in select query output without
changing actual column name in the database.
We list the column alias after the column heading for which we need the column alias.

6. How do you indicate that a column alias should be used?


you can indicate that a column alias should be used by using the "AS" keyword followed
by the desired alias name after the column name in a SELECT statement.

7. When is it appropriate to use a column alias?


It is appropriate to use column alias if the column name is too long or complex to read
easily in the query results. Or the column name is not very descriptive, and you want to
give it a more meaningful name. Or the column name is the same as another column
name in the query and you want to avoid confusion.

8. What are the guidelines to keep in mind when using a column alias?
We can enclose the column alias in double quotes if:
- It contains blank spaces.
- It contains special symbols.
- To retain case.

9. How can you concatenate columns in a query?


We can concatenate two columns using || operator between two column heading that we
want to concatenate for eg.
SELECT firstname || lastname
FROM user

10. What is a NULL value?


A null value is a special marker used to indicate that a data value does not exist in the
database. It is used to represent missing or unknown data. A null value is not the same as
an empty string or a zero value. It is a separate marker that indicates that the value of the
column is unknown or not applicable.

Create the JustLee Books database by executing the JLDB_Build.sql script provided with lab
file on the blackboard. Make sure to save the file in your course folder.
In SQL Developer:
1. From the menu, click File, Open and navigate to the JLDB_Build.sql file.
2. Click the file, and then click the Open button. You should see the script. statements in the work
area.
3. From the file menu choose select All

4. Click the Run Script button above the work area to execute the statements. You might be
prompted to select a connection.

Hands-On Assignments:

SQL CODE IS TO BE PROPERLY USED. A NEW LINE FOR EACH CLAUSE IS


REQUIRED.

KEYWORDS IN UPPERCASE, AND ANY REFERENCE TO TABLES, COLUMNS IS TO


BE IN LOWERCASE
To determine the exact name of fields used in the tables for these exercises, refer to the tables in the
JustLee Books database as created in SQL Developer, or use the DESCRIBE table name command to
view the table’s structure as shown in example below

1. Display a list of all data contained in the BOOKS table.

2. List the title only of all books available in inventory, using the BOOKS table.

SELECT title
FROM books;
3. List the title and publication date for each book in the BOOKS table. Use the column heading
“Publication Date” for the Pubdate field.
SELECT title, pubdate AS "Publication Date"
FROM books;
4. List the customer number for each customer in the CUSTOMERS table, along with the city and
state in which the customer lives.
SELECT customer#, firstname, lastname, city, state
FROM customers;
5. Create a list containing the publisher’s name, the person usually contacted, and the publisher’s
telephone number. Rename the contact column “Contact Person” in the displayed results. (Hint:
Use the PUBLISHER table.)
SELECT name, contact AS "Contact Person", Phone
FROM publisher;
6. Determine which categories are represented in the current book inventory. List each category
only once.
SELECT DISTINCT category
FROM books;

7. List the customer number from the ORDERS table for each customer who has placed an order
with the bookstore. List each customer number only once.
SELECT DISTINCT customer#
FROM orders;
8. Create a list of each book title stored in the BOOKS table and the category in which each book
belongs. Reverse the sequence of the columns so that the category of each book is listed first.
SELECT category, title
FROM books;
9. Create a list of authors that displays the last name followed by the first name for each author. The
last names and first names should be separated by a comma and a blank space.
SELECT lname || ',' || fname
FROM author;
10. List all information for each order item. Include an item total, which can be calculated by
multiplying the Quantity and Paideach columns. Use a column alias for the calculated value to
show the heading “Item Total” in the output.
SELECT orderitems.*, quantity*paideach AS "Item Total"
FROM orderitems;
Advanced Challenge

The management of JustLee Books has submitted two requests. The first is for a mailing list of all
customers stored in the CUSTOMERS table. The second is for a list of the percentage of profit generated
by each book in the BOOKS table. The requests are as follows:

1. Create a mailing list from the CUSTOMERS table. The mailing list should display the name, address,
city, state, and zip code for each customer. Each customer’s name should be listed in order of last
name followed by first name, separated with a comma, and have the column header “Name.” The city
and state should be listed as one column of output, with the values separated by a comma and the
column header “Location.”
SELECT orderitems.*, quantity*paideach AS "Item Total"
FROM orderitems;
2. To determine the percentage of profit for a particular item, subtract the item’s cost from the retail
price to calculate the dollar amount of profit, and then divide the profit by the item’s cost. The
solution is then multiplied by 100 to determine the profit percentage for each book. Use a SELECT
statement to display each book’s title and percentage of profit. For the column displaying the
percentage markup, use “Profit %” as the column heading. Required: Determine the SQL statements
needed to perform the two required tasks. Each statement should be tested to ensure its validity.
Submit documentation of the commands and their results, using the format specified by your
instructor.
SELECT title, ((retail-cost)/cost)*100 AS "Profit%"
FROM books;

You might also like