You are on page 1of 5

LAB ASSIGNMENT

SQL

LAB ASSIGNMENT – I

Table : Employee

Field Name Data Type Size Constraint


Employee_no Number 6 Primary Key
Employee_name Varchar2 30 Not Null
Address Varchar2 30
DateofBirth Date
Basic Number 8,2 >=10,000
HRA Number 6,2
Department Varchar2 6

Write queries to perform following operations:

1. Insert few records in it.


2. Assign 10% of Basic to HRA of all the employees
3. Display the records of those employees who are getting more than Rs.30000.
4. Display the names of all the employees who belong to Accounts dept.
5. Display the records of all the employees in ascending order of their names.
6. Delete the records of those employees where salary is between 20,000 and 25,000.
7. Add a new column in the table : Designation with a default value “Staff”
8. Display the Basic, HRA and Department of those employees where names start with ‘R’ and ends
with ‘S’.
9. Display the total salary of all the employees of department wise.
10. Display the names of employees with their date of birth in yyyy-mm-dd format.
11. Display the details of all the employees with modified (or different) column names such as
Employee_Name can be displayed as EName etc.
12. Display the names of all those employee who work in Sunita’s department.
13. Display the name of the employee along with his department who is taking the maximum salary.
14. Display the details of those employees who live in the same city as that of “Radhika” and getting
less salary than her.
15. Display the name, address and department of employee with lowest basic salary.
LAB ASSIGNMENT – II

Table : Book

Field Name Data type Size Constraint


Book_Code Varchar2 6 Primary key/ start with ‘B’
ISBN No. Varchar2 15 Not Null
Book_Name Varchar2 20 Unique
Publisher Varchar2 30 Default value
Price Varchar2 5

Write queries to perform the following:

1. Insert few records in it.


2. Increase the size of the Book_Name to 30 characters.
3. Display the total price of all the books of “TMH’ publishing house.
4. Display the details of all the books of author “Yashwant Kanetkar” and “Robert Lafore”.
5. Delete all the book details of “PHI” publisher.
6. Create a new table “Author” with author details such as author id, name of the author, subject,
address, research area etc.
7. Add a new column “Author_Name” in “Book” table and make it a foreign key on “author id” of
“Author” table.
8. Display the records of the books where book name starts with ‘C’ or ‘G’ or ‘K’.
9. Create a view for the user to access Book Code, Name and Price of books which are published by
‘Pearson Education’.
10. Create a simple Index on book table.

LAB ASSIGNMENT – III

Table : Student

Field Name Data type Size Constraint


Roll_no Number 2 Primary Key
Name Varchar2 20 Not Null
Address Varchar2 50
City Varchar2 30
Marks_Sub1 Number 5,2 >=0 and <=100
Marks_sub2 Number 5,2 >=0 and <=100
Marks_sub3 Number 5,2 >=0 and <=100
Total Number 5,2 >=0 and <=300

Write SQL queries for :

1. Insert few records except total marks.


2. Calculate total marks of each student and save it.
3. Display the name of the student who has got the highest marks.
4. Display the records of those students who have got marks more than the marks of ‘Kiran’.
5. Display the percentage of all the students with their names.
6. Display the names of those students who have got maximum marks in sub1 and more than 50 in
sub2 and sub3.
7. Update the marks of all those students who are fail, with grace marks 5. (fail means less than 40)
8. Delete the records of those students who are fail in all three subjects.
9. Display the average total marks of all the students city wise excluding the students of ‘Gurgaon’.
10. Get the names of students with Roll_no less than 30 and whose total marks is more than the total
marks of at least one student with Roll_no greater than or equal to 30.
11. Create a composite index on Student table.
12. Create a view on the table to access roll no, name (in capitals), Marks of all 3 subjects and
Percentage of all the students.

LAB ASSIGNMENT – IV

Joins

Consider the following three relations:


Primary Keys are indicated by #
Foriegn Keys are underlined.

Employee (Eid#, EName, Address, DateOfJoining, Department)


Project (Pid#, PName, StartDate, TerminationDate)
AssignedTo (Eid, Pid)

Now write queries for the following in relational algebra as well as in SQL:

1. Find the employees working on ‘Banking’ project.


2. Find the projects assigned to the employees of D01 or D02.
3. Find the employees who belong to Delhi and work on either ‘University’ project or
‘ShareMarket’ project
4. Find the employees who do not work on ‘University’ project.
5. Find the employees who work on all projects.
6. List the employees who have not been assigned any project.
7. Find the employees who joined the department after the commencement of ‘Bank’ project.
8. Display the start and termination date of projects which are allotted to ‘Jai Prakash’.

Consider the following relations:

Primary Keys are indicated by #


Foriegn Keys are underlined.

Student (Roll#, SName, City, Age, Gender)


Course (Cid#, CName, Semester, Credits, Fee)
Enrollment (Roll, Cid, DateOfReg)

Now write SQL queries for the following:

1. Find the details of students registered in MCA course.


2. Display the total number of students enrolled in MBA.
3. Display the names of students who are enrolled in the course in which “Anita” is registered.
4. Display the names of all the students with their date of enrollment in the courses.
5. Display the name of the course in which “Kunal” is registered.
6. Display the names of boys-students enrolled in MCA.
7. Display the number of students registered in MCA course who live outside Delhi.
Consider the following relations:

Primary Keys are indicated by #


Foriegn Keys are underlined.

Customer (PAN#, CName, Phone, Emailid, Address)


Vehicle (SerialNumber#, Name, Type, Model, Manufacturer)
Registration (PAN, SNo, DateofReg)
Vehicle_Service (SNo, ServiceNo, DateofService, Description, Charges) –
Primary Key: (SNo, ServiceNo)

Now write SQL queries for the following:

1. List the names of all customers who have had their vehicle serviced that cost more than Rs.
1000.
2. List the names and email ids of all customers who have registered a vehicle of ‘Suzuki’.
3. List the names of all customers who have registered a vehicle but who have not had that
vehicle serviced.
4. List the customers who have serviced their vehicle at least once.
5. List the vehicle name and model which are serviced more than two times.

LAB ASSIGNMENT – V

Write an example of the following functions :

Sqrt() Power() Ceil()


Substr() Max() min()
Round() to_date() to_char()
Length() avg() count()
Exp() mod() lower()
Initcap() instr() months_between()

LAB ASSIGNMENT – VI

PL/SQL:

 Write a PL/SQL code that accepts the account number from the user and check the users balance.
If it is greater than 1000 , only then deduct Rs. 200/- from the balance . The process is fired on the
Acct_Mstr table.

 Write a PL/SQL code to calculate the area of a circle for a value of radius varying from 3 to 7.
Store the radius and the corresponding values of calculated area in an empty table named Areas ,
consisting of two columns Radius and Area.

 Write a PL/SQL code that first withdraws an amount of Rs. 1500/-. Then deposits an amount of Rs.
50,000.Update the current balance . Then check to see that the sum of the current balance of all the
accounts in the bank does not exceed Rs. 60,00,000. If the balance exceeds Rs 60,00,000 then undo
the deposit just made.
 Write a PL/SQL code that will reverse a given number like 12345 to 54321

Use of Cursors :

 The bank manager of Delhi branch decides to mark the status of all those accounts as ‘Inactive’ on
which there are no transactions performed in last 6 months. Whenever any such update takes place
the corresponding record is inserted in another table ‘InactiveAccounts’ with the name and
account no of the account holder and his balance.
(Table : Account (AccNo, Name, Address, PANNo, Mobile, Status)

 Write a PL/SQL code that updates the balance of the account holder of ‘delhi’ branch and displays
the number of records updated. (Use implicit cursor)

Cursor and Error Handing

 Write a Cursor( PL/SQL code) to display the Employee_name, Dateofbirth ,Designation whose
basic salary is greater than 15000, if not found then show the proper error message. (Use Exception
handing)

Procedures and Functions

 Create a procedure that accepts the emp id from the calling procedure and displays his/her record.
 Create a function that accepts emp id from the calling procedure and returns his salary.
 Write a PL/SQL function ODDEVEN to return value TRUE if the number passed to it is EVEN
else will return FALSE.
 Write a PL/SQL procedure called MULTI_TABLE that takes two numbers as parameter and
displays the multiplication of the first parameter till the second parameter.

Trigger

 Create a Trigger in PL/SQL which will work before deletion or updation in employee_master table
and inserts a duplicate copy of the record in another table for future reference.

 Create a Trigger in PL/SQL which will work after update or insert on employee _master and store
the old data and deleted data in a table named as backup_employee_master .

You might also like