Professional Documents
Culture Documents
A.RDBMS CONCEPTS:
Exercise Objective(s):
Exercise:
1) Draw an E-R diagram for the following application.
a. A training institute conducts up to 6 courses.
c. A student can enroll for a particular course only once. However a student can enroll for any number
of courses. The date of enrollment of the course is also maintained.
d. A course is described by its course-id, title and duration, course-id being the primary key.
e. A student is described by stud-id, name and address (house number, street or area, town, dist ,
city state). The stud-id is the primary key.
f. There is no student who has not taken any course. There are however courses for which there are
no students.
Exercise Objective(s):
Exercise:
Exercise Objective(s):
Exercise:
Draw an ER diagram for a bus company with following specifications:
a. A Country Bus Company owns a number of busses.
b. Each bus is allocated to a particular route, although some routes may have several busses.
c. Each route passes through a number of towns.
d. One or more drivers are allocated to each stage of a route.
e. A stage corresponds to a journey through some or all of the towns on a route.
f. Some of the towns have a garage where busses are kept and each of the busses are identified
by the registration number and can carry different numbers of passengers, since the vehicles
vary in size and can be single or double-decked.
g. Each route is identified by a route number and information is available on the average
number of passengers carried per day for each route.
Exercise Objective(s):
Exercise:
Draw an ER diagram for application recording information about magazines, writers, and
subscribers, including:
a. For each magazine, its name, its writers, its editor, and the cities to which it is delivered are
to be maintained.
b. For each writer, their id, name is to be maintained. (Note: a writer can write for more than
one magazine, but can only edit, at most, one magazine.)
c. For each subscriber, their name, favorite writers, city or residence, and what magazines
they receive are to be maintained.
d. For all entities and relationships, select and specify keys.
Exercise Objective(s):
Exercise:
Happy Home Super market deals with groceries, household items, stationary products and gift
items. They have announced a summer mela where, for every customer who holds a
membership with Happy Home, any three items can be taken for free. The total price of the three
items should be equal to or below Rs.1000. They record details of these free products given to
their member customers in a table as below.
Exercise Objective(s):
Exercise:
Normalize the given table ensuring no data loss and data redundancy (15mins)
Exercise Objective(s):
Exercise:
Good Work Cooperation is the Computer Programming Services industry in DALLAS, TX. It has
offices throughout North and South India.
Employee fills in the following details at the time of joining : Name, Phone
number, Hiredate, Salary, Commission and Address. He/She is given a unique ID.
Employee is then allocated a project under a LOB. He/She reports to a
Reporting Manager who is also an employee.
Projects have a unique id, name, duration (in months), Project Manager who is
an employee. Every project is under a LOB.
Every LOB has a unique ID, Name and multiple locations(address) from where they
operate.
Design an appropriate set of Normalized Relations to represent this information
Recommended duration: NA
Solution Guidance (if applicable):NA
B.SQL: An introduction
Exercise Objective(s):
Exercise:
1 Shirt 10 600
2 T-Shirt 20 300
3 Kurti 30 350
4 Trousers 40 1000
5 Skirt 13 250
Recommended duration: 20mins
Solution Guidance (if applicable):NA
Exercise Objective(s):
Exercise:
Make sure that you have Student table with following data:
get the names of student who enrolled in the month of February (don’t use any
oracle functions)
get the name and enroll dates of the student whose id is not 5.
Get the dates when the ORL batches where conducted this year.
C.SQL Statements
Exercise Objective(s):
Exercise:
Exercise Objective(s):
Exercise:
*Change the table to add a default for Doj as 1st March 2010.
*Remove defaults for Basic and HRA.
*Remove the column “Remarks”.
*Add a column EName that can have hold at most 30 characters and Gender with *just
one character and set the default gender to ‘M’.
*Remove the Salary_Det table from the database.
*Change the table name Salary into Salary_Det.
Exercise Objective(s):
Exercise:
3.Salary_Det
Enter the following records in the Salary_Det table using INSERT ALL.
Create another table called Salary_DetF with similar structure as Salary_Det.
Insert data with Gender as F into Salary_DetF.
Exercise Objective(s):
Exercise:
Exercise Objective(s):
Exercise:
Using the Salary_Det table write the queries to find the following :
Compute and display name and travel allowance which is 2% of Basic for the employees whose
basic is given. Display the result in sorted order of name and travel allowance.
Count the number of employees who have the gross salary (basic+hra) greater than 60000.
D.SQL CONSTRAINTS
Exercise Objective(s):
Exercise:
Create a table called Leave_Policy with the following fields
1 Casual 3 3 3 3 n
leave
2 Sick 2 2 2 2 n
leave
3 Earned 1 1 1 1 y
Leave
4 Restric 2 2 1 1 n
ted
Leave
Make Empid as primary key for the Salary_Det table that you created.
Exercise Objective(s):
Exercise:
2) Create a table called Employee_Leave(30mins)
Leave_date_from Date
Leave_date_to Date
Loss_of_Pay_Days Number(2) 0
Exercise Objective(s):
Exercise:
E.SQL OPERATORS
Exercise:
Write query to get the following details from the Salary_det table:
1. Update the data with the new basic to be 10% more than the current basic.
2. Display the details of the employees who name begin with alphabet between ‘A/a’ or ‘H/h’ .
3. List the details of the female employees who have joined after JUNE 2009.
5. Get all IDs of the employees whose have taken casual leave for more than 3 days at a stretch.
6. Get the leave details for the employees EA12345 and EA23456.
Exercise Objective(s):
Exercise:
Create another table Salary_Old with the following records
SAP2345 Sudha N F HR
1. Display sapids and names from both the tables with no duplicates.
3. Using the information available in both the tables find sapids of all the employees who
have joined after 2009 but not in HR department.
4. Display sapids and names in the form of “<<name>>(<<sapid>>) who are only in
Salary_Det table.
F.Oracle: Indexes
Exercise Objective(s):
Exercise:
Create client_master table as given description:
ClientName VARCHAR(30)
Address VARCHAR(30)
City VARCHAR(30)
State VARCHAR(30)
BalDue NUMBER(10,2)
Alter client_master table to create composite index for the for the columns highlighted.
ClientName VARCHAR(30)
Address VARCHAR(30)
State VARCHAR(30)
BalDue NUMBER(10,2)
Exercise:
1. Using the Product, Orders and Items table write a query to find all the write a query to display
the name of the product and the count of the number of products that has been ordered.
6. Write about differences you notify in both Execution plan and statistics.
Note : If you have more number of rows(in Thousands) then effect of indexing is shown better.
Exercise Objective(s):
Exercise:
Drop the index created in the last exercise and recreate the index based on the join condition.
Exercise Objective(s):
Exercise:
Create UNIQUE INDEX on the Pname column of the product table.( if you have a
unique constraint define on it, drop it).
Now try to insert a new record using same name product name that was inserted before.
Exercise Objective(s):
Exercise:
Salary_Det
You frequently need to query and find out the names of employees who have the gross salary
(basic+hra) greater than 60000.
Create an index to improve query performance.
G.Oracle: Views
Exercise Objective(s):
Exercise:
Create a view that has data joining the 2 tables Salary_Det and Employee_Leave using
natural join (refer slide 31 of 2.3 SQL Constraints)
Write query on the view to get names of all the employee who have taken leave id 1.
From the view get the names of the employee who id begin with EA and who do not have loss
of pay.
Exercise Objective(s):
Exercise:
Create a view with empid, empname, basic, hra and gross (basic+hra) and net salary (gross –
loss of pay days * basic) using Salary_set and Employee_Leave tables.
Get employees whose gross salary and net salary are different
Exercise Objective(s):
Exercise:
Create a view EA_SAL with all the details in the salary_det table and computed gross
(basic+hra) column for employee who id begin with ‘EA’
Exercise Objective(s):
Exercise:
1. Make the EA_SAL view updatable only where the view conditions is satisfied.
Exercise Objective(s):
Exercise:
Create a view with Employee name and date of joining from Salary_Det table. Make sure
that Employee names are unique and not null.
Test the view by adding duplicate Employee name
Test the view by adding fresh Employee name and date of joining.
H.Oracle: Sequences
Exercise Objective(s):
Exercise:
Create an ascending sequence ASC_Seq starting with 1001 and maximum it go is 9999
after reaching 9999 restarts with 1001.
Creating a descending sequence DSC_Seq starting with 9999 and that end with 0 and does not
cycle
Exercise Objective(s):
Exercise:
The Salary_Det tables requires generation of sequences from 00001 to 90000. The 2
lettered Business Unit name will be given and this has to be prepended to the sequence.
Create a sequence Emp_Seq and use this in INSERT statement to achieve this.
Exercise Objective(s):
Exercise:
Modify the sequence ASC_Seq created in previous exercise and ensure that it now
increments by 5.
Make the last number in sequence of DSC_Seq to 1 instead of 0 and allow DSC_Seq to
start again with maximum value after reaching to 1.
Exercise Objective(s):
Exercise:
Using Salary_Det , Leave_policy and Employee_Leave tables, write a query to get the names
and Employee Ids of the employees who have taken casual leave in from the January 2012 to
September 2012.
Rewrite the same query using NATURAL JOIN.
Note: NATURAL JOIN does not need a qualifier!
Exercise Objective(s):
Exercise:
1. Make sure that all data in the Employee_Leave is for the year 2012.
2. Assuming that employees can take EL(Earned Leave) only once during a quarter
(meaning that there will be only one entry per quarter in the table for EL), write a query to
get the total number of EL left for an employee who have taken Earned Leave in the 1st
quarter (1st JAN- 30th APR). Print Employee ID, Name and EL left.
Leave_policy
Employee_Leave
Exercise Objective(s):
Exercise:
Write a query to get all the employees with the details of the leave if they have taken.
Add a manager column in the Salary_Det table and alter the data as shown below:
Write a query to get the names and salaries of all the managers.
Find all the employees who are not managers
Recommended duration: 30 mins
Solution Guidance (if applicable): NA
Exercise Objective(s):
Exercise:
Create a view to get all the names and ids of all employees with details of the leave that they have taken
Exercise Objective(s):
Exercise:
Look at the tables below. What do you make out of the tables?
CIDCNAME DURATION
1 Java 52
2 RDBMS 30
3 .NET 35
(Note that all the queries must be written based on what is covered so far. Constructs/functions that are
not covered so far should not be used.)
1. Find sids and names of the students have enrolled for at least one course. Make sure there
are no duplicate rows. Write this query using all the possible kinds of join that you learnt.
2. Find the end date for courses in the enrollment table if the course duration per day is 7 hours.
3. Find the names of the students and the names of the course which they have enrolled for.
6. Find names of all students who have enrolled for some courses and whose age above 20.
(Hint: Recall that SYSDATE gives the current system sdate).
7. Find the sids and names of all the students who have taken Java or RDBMS courses.
8. Find the sids of all the students who have taken both Java and RDBMS courses.
9. Find the sids of all the students who have taken Java but not RDBMS courses.
Exercise:
Ensure that you have data that is shown above in the Salary_Det table.
1. Extract the first and the last name of the employee from the Ename.
2. Display all the employee who joined in 2011.
3. For the Empid that have SAP, make the SAP to SP
4. For the Empid that do not have length 7, add 0s at the end to make the length as 7.
(Use RPAD to do this that automatically adds the required number of 0s instead of just
concatenating a single 0).
5. Display the code name for the employees using the first and the last character of the
first and last name.
Exercise:
Exercise:
Salary_Det
Leave_Policy
Employee_Leave
Recommended duration: NA
Exercise Objective(s):
Exercise:
List the over all running total and department-wise (SAP or EA) total for salaries for
employees from Salary_Det table.
Recommended duration: NA
Exercise Objective(s):
Exercise:
List the name of the employees, date of joining and the name of the person who joined
after the current employee.
Recommended duration: NA
K.SUBQUERIES
Exercise Objective(s):
Exercise:
Salary_Det
Get the names of the employees who have salaries more than the average salary of the person in the
“EA” department.
Get all the employees who are in the same department as “'Geetha S”.
Tax
Find all the people who pay the same tax as Hari Prasad.
Recommended duration: NA
Solution Guidance (if applicable): NA
Exercise:
Find all the employees who earn more than people in “SAP” department.
Recommended duration: NA
Solution Guidance (if applicable): NA
Exercise Objective(s):
Exercise:
Add a manager column in the Salary_Det table and alter the data as shown below (same as
done in slide 25 of the join session)
Find the names of people who joined after Mohan V but before Geetha S.
Find the name of the employee who has the 3rd highest tax payer.
Leave_policy
Employee_Leave
Recommended duration: NA
Solution Guidance (if applicable): NA