Sei sulla pagina 1di 5

List of Practical Exercises

Set 1: Querying Single Table


Note: Consider the EMP table for the following queries.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.

Retrieve a list of MANAGERS.


Find out salary of both MILLER and SMITH.
Find out the names and salaries of all employees earning more than 1000 per month.
Display the names and salaries of all employees except JAMES.
List the name and salary of employees who can earn more than 1500 and are in
department 10 or 30. Label the columns Employee and Monthly Salary respectively.
List the name and salary for all employees whose salary is not in the range of 1500 and
2850.
Display the name, job, and salary of all the employees whose job is CLERK or
ANALYST and their salary is not equal to 1000, 3000, or 5000.
Display the name, salary and commission for all employees whose commission amount is
greater than their salary increased by 10%.
Display the name of all employees who have two Ls in their name and are in department
30 or their manager is 7782.
Find out the details of employees whose names begin with S.
Find out the names of all employees that have A anywhere in their name.
Find out the names of all employees that have L as their third character in their name.
Find out the names of the employees whose name begin with A or M.
List all the employees whose commission is NULL.
List employee number, employee name, total salary (i.e. salary + commission). (Note:
Manipulate the NULL values accordingly.)
List the name of the employee and designation of the employee, who does not report to
anybody.
List employee name and yearly salary and arrange the output on the basis of yearly salary
in descending order.
Retrieve the departments in ascending order and their employees in descending order.
Select the name, job, salary, and department number of all employees except
SALESMAN from department number 30.
List different jobs with no duplicates.
Count the total number of employees.
Print the total employees and average salary of each department.
Select the minimum and maximum salary from EMP table.
List the minimum and maximum salaries of each department.
List all departments in which more than 3 employees are working.
Set 2: Querying Multiple Tables

Consider the relations given below:


EMPLOYEE (EmployeeID, EmployeeName, Street, City)
COMPANY (CompanyID, CompanyName, City)

WORKS (EmployeeID, CompanyID, Salary)


MANAGES (EmployeeID, ManagerID)
I.
II.
III.
IV.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.

1.

Create above relations using your own data types and print the structure of the each
relation.
Insert at least 10 records in the relation EMPLOYEE and 5 records in the relation
COMPANY. Insert appropriate records in the relations WORKS and MANAGES.
Print the contents of the each relation.
Give an expression in SQL with output for each of the following queries:
Find the names of all employees who work for First Bank Corporation.
Find the names and cities of residence of all employees who work for the First Bank
Corporation.
Find the names, street, and cities of residence of all employees who work for First Bank
Corporation and earn more than Rs. 10,000/-.
Find the employees who live in the same cities as the companies for which they work.
Find all employees who live in the same cities and on the same streets as do their
managers.
Find all employees who do not work for First Bank Corporation.
Find all employees who earn more than every employee of Small Bank Corporation.
Find all companies located in every city in which Small Bank Corporation is located.
Find all employees who earn more than the average salary of all employees of their
company.
Find the company that has the most employees.
Find the company that has the smallest payroll.
Find those companies whose employees earn a higher salary, on average, than the
average salary at First Bank Corporation.
Set 3: Constraints, Views, etc.
Create the tables based on the following table instance charts. Choose the appropriate
datatypes and be sure to add integrity constraints.

Table Name: MEMBER


Column_
Name
Key Type
NullI/
Unique
Default
Value
Datatype

MEMBER_ID

LAST_NAME

FIRST_NAME

ADDRESS

CITY

PHONE

JOIN_
DATE

PK
NN, U

Number

NN

NN

VARCHAR2

System
Date
Date

VARCHAR2

VARCHAR2 VARCHAR2

VARCHAR2

10

Length

25

25

100

30

15

Table Name: TITLE


Column_
Name
Key Type
NullI/
Unique

TITLE_ID

TITLE

DESCRIPTION

NN

NN

RATING

CATEGORY

RELEASE_ DATE

PK
NN, U

DRAMA, COMEDY,
ACTION, CHILD, SCIFI,
DOCUMENTARY
VARCHAR2
VARCHAR2
4
20
G, PG, R,
NC17, NR

Check
Datatype
Length

Number
10

VARCHAR2
60

VARCHAR2
400

Date

Table Name: TITLE_COPY


Column_ Name

COPY _ID

TITLE_ID

PK
NN, U

PK, FK
NN, U

Number
10

TITLE
TITLE_ID
Number
10

Key Type
NullI/ Unique
Check
FK Ref Table
FK Ref Col
Datatype
Length

STATUS
NN
AVAILABLE, DESTROYED, RENTED, RESERVED

VARCHAR2
15

Table Name: RENTAL


Column_
Name
Key Type
Default Value

BOOK_DATE MEMBER_ID
PK

2.

(a)

TITLE_ID

PK, FK2

PK, FK2

ACT_RET_DATE

MEMBER
Date

EXP_RET_DATE
System Date + 2
Days

System Date

FK Ref Table
FK Ref Col
Datatype
Length

PK, FK1

COPY_ID

MEMBER_ID
Number
10

TITLE_
COPY
COPY_ID
Number
10

TITLE_
COPY
TITLE_ID
Number
10

Date

Date

Add data to the TITLE table. Be sure to use sequence to add the TITLE ID. The
sequence must start with 92.

Title
Willie and
Christmas Too

Description
All of Willies friends make a Christmas list
for Santa, but Willie has yet to add his own

Rating

Category

Release_Date

CHILD

05-OCT-1995

wish list.
Yet another installation of science fiction
Alien Again
history. Can the heroine save the planet from
the alien life form?
A meteor crashes near a small American
The Glob
town and unleashes carnivorous goo in this
classic.
With a little luck and a lot of ingenuity, a
My Day Off
teenager skips school for a day in New York.
A six-year-old has doubts about Santa Claus,
Miracles on Ice
but she discovers that miracles really do exist.
After discovering a cache of drugs, a young
Soda Gang
couple find themselves pitted against a
vicious gang.

(b)

SCIFI

19-MAY-1995

NR

SCIFI

12-AUG-1995

PG

COMEDY

12-JUL-1995

PG

DRAMA

12-SEP-1995

NR

ACTION

01-JUN-1995

Add data to the MEMBER table. Write a script named S3Q2B.SQL to prompt
users for the information. Execute the script. Be sure to use sequence to add the
member number. The sequence must start with 101.

First_ Name Last_Name


Carmen
Velasquez
LaDoris
Ngao

(c)

Address
City
283 King Street Seattle
5 Modrany
Bratislava

Phone
206-899-6666
586-355-8882

Join_Date
08-MAR-1990
08-MAR-1990

Midori
Mark
Audry

Nagayama
68 Via Centrale Sao Paolo
Quick-to-See 6921 King Way Lagos
Ropeburn
86 Chu Street
Hong Kong

254-852-5764
63-559-7777
41-559-87

17-JUN-1991
07-APR-1990
18-JAN-1991

Molly

Urguhart

418-542-9988

18-JAN-1991

3035 Laurier

Quebec

Add the following movie copies in the TITLE_COPY table.


Note: Have the title_id numbers available for this exercise.
Title
Willie and Christmas Too
Alien Again
The Glob
My Day Off

Miracles on Ice
Soda Gang

(d)

COPY_ID
1

Status
AVAILABLE

1
2
1
1
2
3
1
1

AVAILABLE
RENTED
AVAILABLE
AVAILABLE
AVAILABLE
RENTED
AVAILABLE
AVAILABLE

Add the following rentals in the RENTAL table.

Title_Id

Copy_Id

Member_Id

Book_Date

Exp_Ret_Date Act_Ret_Date

92

101

3 days ago.

1 day ago

93
95
97

2
3
I

101
102
106

1 day ago
2 days ago
4 days ago

1 day from now


Today
2 days ago
2 days ago

2 days ago

3.

Create a view named TITLE_AVAIL to show the movie titles and the availability of each
copy and its expected return date if rented (TITLE, COPY_ID, STATUS, and
EXP_RET_DATE). Query all rows from the view. Order the results by title.

4.

Make the changes to data in the tables.

5.

(a)

Add a new title. The movie is Interstellar Wars, which is rated PG and
classified as a SCIFI movie. The release date is 07-JUL-77. The description is
Futuristic interstellar action movie. Can the rebels save the humans from the evil
empire? Be sure to add a title copy record for two copies.

(b)

Enter two reservations. One reservation is for Carmen Velasquez, who wants to
rent Interstellar Wars. The other is for Mark Quick-to-See, who wants to rent
Soda Gang.

(c)

Customer Carmen Velasquez rents the movie Interstellar Wars, copy 1. Remove
her reservation for the movie. Record the information about the rental. Allow the
default value for the expected return date to be used. Verify that the rental was
recorded by using the view you created.

Create a report titled Customer History Report. This report will contain each customers
history of renting videos. Be sure to include the customer name, movie rented, dates of
the rental, and duration of rentals. Total the number of rentals for all customers for the
reporting period. Save the script in a file named S3Q5.SQL.

Set 4: PL/SQL
Consider the tables of Set 3 and perform the following:
1.
2.
3.
4.
5.

Write a procedure to display member id, name and address of all members using cursors.
Write a function to return the availability of given title.
Write a trigger to check whether required title is available before issuing for rent. If the
required copy is not available then raise the appropriate exception and show the message.
Write a trigger to update the status of given copy of a title after returned by the member.
Write a trigger to restrict issue and return on Sundays.

Potrebbero piacerti anche