Vous êtes sur la page 1sur 5

SQL Assignments

Table Structure:
Course

Student

CourseID
CourseName
Duration
Fees
StudID
FirstName
LastName
Street
City
DOB

Registration
CourseID
StudID
DOJ

NUMBER(4) - Primary Key


VARCHAR2(20)
NUMBER (In Months)
NUMBER(7, 2)
NUMBER(4) - Primary Key
VARCHAR2(20)
VARCHAR2(20)
VARCHAR2(20)
VARCHAR2(20)
DATE
NUMBER(4) - Foreign Key
NUMBER(4) - Foreign Key
DATE

Primary key (CourseID, StudID)


Records:
Course:
CourseID

CourseName

1001
1002
1003
1004
1005

Java
C++
Linux and C
Oracle
Web

Duration

Fees
4
2
3
2
6

5000.00
4000.00
4000.00
3000.00
10000.00

Student:
StudID FirstName
3001
3002
3003
3004
3005
3006
3007
3008
3009

Dileep
Anand
Bala
Gowri
Priya
Prem
Rahul
John
Abdul

LastName

Street

Kumar
Kumar

DOB

AnnaNagar
Bangalore
10-MAR-1979
IndiraNagar
Bangalore
19-JAN-1978
Krishnan
KPNagar
Chennai 03-JAN-1980
Shankar AnnaNagar
Coimbatore
22-DEC-1977
Menon
JPNagar Cochin
12-FEB-1980
Kumar
MGStreet
Coimbatore
17-MAY-1977
Dravid
KPNagar
Chennai 08-OCT-1977
David
NGColony
Chennai 15-SEP-1980
Rahman JBNagar Bangalore
19-JAN-1978

Registration:
CourseID
1001
1001
1002
1002
1002
1003
1003
1003
1004
1004
1004
1005

City

StudID DOJ
3004
3005
3002
3003
3008
3001
3004
3006
3005
3007
3008
3004

10-MAR-1999
10-MAR-1999
18-APR-1999
18-APR-1999
18-APR-1999
06-JUN-1999
06-JUN-1999
06-JUN-1999
10-MAR-1999
10-MAR-1999
10-MAR-1999
25-MAY-1999

1005

3005

25-MAY-1999

Note:
Create the above specified tables with the constraints and insert the specified records.
Queries:
1)
2)
3)
4)
5)

Write a SQL query to add a field - Age NUMBER(3) on to the STUDENT table.
Write a SQL query to add a constraint in COURSE table to check the fees entered is greater than zero.
Write a SQL query to calculate the value of the Age field using the DOB field.
Write a SQL query to decrease the fees by Rs.500 whose duration is lesser than or equal to 3 months.
Write a SQL query to delete the student record whose lastName is David and the City is Chennai.

6)
Write a SQL query to display all the records from the COURSE table.
7)
Write a SQL query to display the firstName, lastName and concat them to display as a fullName from the
STUDENT table.
8)
Write a SQL query to display the details of the course C++ from the COURSE table.
9)
Write a SQL query to display the details from the COURSE table which has a fees of more than 4000.
10)
Write a SQL query to display the details from the COURSE table which has a duration of 2 to 4 months.
11)
Write a SQL query to display the students details whose birthday lies in the months between APR and SEP.
12)
Write a SQL query to display the details from the COURSE table which has the duration of 2 Months and has
a fees of more than 3000.
13)
Write a SQL query to display the details of the students whose Ids are 3002, 3004, and 3005 from the
STUDENT table
14)
Write a SQL query to display the details from the STUDENT table who has the same lastName as Kumar
15)
Write a SQL query to display the details from the STUDENT table whose firstName starts with A.
16)
Write a SQL query to display the details from the STUDENT table those who come from the city Bangalore
and has the lastName as Kumar
17)
Write a SQL query to display the details from the STUDENT table whose street name ends with Nagar.
18)
Write a SQL query to display the details of the students Dileep and Abdul from the STUDENT table.
19)
Write a SQL query to display the eldest student.
20)
Write a SQL query to display the details of the students whose ID is other than 3004, 3006 from the
STUDENT table.
21)
Write a SQL query to display the maximum, minimum and the avergae fees from the COURSE table.
22)
Write a SQL query to display the total number of students from the STUDENT table.
23)
Write a SQL query to display the cities from the STUDENT table and each city should be displayed only once.
24)
Write a SQL query to display all the student details in the order of their DOB in descending order.
25)
Write a SQL query to display the total number of students who joined on 10-MAR-1999
26)
Write a SQL query to display the firstName in Uppercase, Lowercase and in ProperCase.
27)
Write a SQL query to display the last 3 characters from the firstName.
28)
Write a SQL query to display the total number of students for each course.
29)
Write a SQL query to display the StudIDs who have joined for more than one course.
30)
Write a SQL query to display all the student details whose firstName is of 4 characters length.
Sub Queries and Join:
1)
Write a SQL query to display the course details whose fees is lesser than the maximum fees.
2)
Write a SQL query to display the students who are elder than BalaKrishnan
3)
Write a SQL query to calculate the total fees collected from the students who are coming from the city
Bangalore.
4)
Write a SQL query to display the students firstName and the Course Name for each student.
**5)
Write a SQL query to find the courses which has the same duration.
**6)
7)

Write a SQL query to display the completing date of each course using DOJ
Write a SQL query to display the second maximum fees.

and duration.

8)
**9)
10)
11)

Write a SQL query to display the studid and the total fees paid by each student.
Write a SQL query to display the courseid,coursename and the total fees
collected from each course.
Write a SQL query to display the course details of the student Priya.
Write a SQL query to display the least two fees from the COURSE table.

1.
Create a view StudView which has the fields Studid, StudName from STUDENT table and CourseID,
CourseName from COURSE table.

2.
3.

Create a Sequence StudCount which starts with 4001 and has the maximum value as 5000.
Insert the Sequence count value as the StudID with your own other details on to the STUDENT table.

PL/SQL Assignments
1. Write a program to accept a number between 1 to 5 and display it in words.
2. Write a program to accept a number and check whether it is prime or not.
3. Write a program to generate the fibobacci series upto n terms.
4. Write a program to accept a number and check whether it is armstrong or not.
5. Write a program to accept a number and convert it into binary number.
6. Write a program to accept a string and reverse it.
7. Write a program to accept a string and check whether it is palindrome or not.
8. Write a program to store the sysdate in any variable and display the following :
Today's Date is 24-OCT-01
Table: EMP
ENO
NUMBER(3)
ENAMEVARCHAR2(10)
SALARY
NUMBER(5)
Without Cursors: (Tables Used : COURSE - STUDENT - REGISTRATION - EMP)
1. Write a program to accept a CourseID and display the details using ROWTYPE.
2. Write a program to accept a String and display the String in Uppercase, Lowercase
and Propercase.
3. Write a program to accept the choice[I/U/D] from the User and do the Corresponding
table.
If Choice is 'I' - Insert a new record by accepting the data's from the
User.
If Choice is 'U' - Accept an Employee number and the details. Then update
the table with the new data.
If Choice is 'D' - Accept an Employee number and Delete the corresponding
record from the table.
4. Write a program to accept only the EmployeeName and Salary from the user and
generate the new Employee Number automatically and then insert into the table.
(Eg. If the last EmpNo is 1005 then the New EmployeeNo is 1006. If table is
Empty, then it should insert 1000 as new EmployeeNo)
With Cursors: (Tables Used : COURSE - STUDENT - REGISTRATION - EMP)
1. Write a program to display the contents of the EMP table.
2. Write a program to display the maximum and minimum salary from the EMP table
without using inbulid functions.
3. Write a program to display the last 3 rows of the EMP table.
4. Write a program to display the 3rd maximum salary from the EMP table.
5. Write a program to accept a Employee Number. If found display the record else
display the corresponding message.
6. Write a program to accept a Employee Number and the choice[U-Update/D-Delete].
If Choice is 'U' - Accept the details. Then update the table with the new data.
If Choice is 'D' - Delete the corresponding record from the table.
If Record not found then Display the Corresponding Message.
7. Write a program to update the Employee Table :
If Salary < 5000 - Increment by 10%.
If between 5000 and 10000 - Increment by 15%.
- else increment by 20%.
8. Create a table with the Following Structure:
Table: EMPLOG

operation in the EMP

ENO
USER
DONEON

NUMBER(3)
VARCHAR2(10)
DATE

Write a program to accept a Employee Number and delete the record. Before deleting the record, insert the
information onto the EMPLOG table. The information to be inserted are:
EMPNO - Which you are going to delete.
USER
- The current working username.
DONEON - The system date.
9. Write a program to calculate the Total fees paid by each student.
10.Write a program to display the CourseID, CourseName, StudID, StudName, DOJ.
********************************************************************
1.
Write a Procedure to accept two numbers as an argument and use a OUT variable to set the MOD value of
those two numbers without using MOD function.
2.
Write a Procedure to calculate the sum of all the salaries from the EMP table without using SUM function.
Pass the salary onto the procedure one by one from the calling program.
3.
Write a Procedure to accept the FirstName and CourseID as an argument and check whether that student has
registered for that course. If registered, then display appropriate messages.
4.
Write a Procedure to accept the CourseID and the maximum number of students for that courseID. Check
whether 50% of the class is full or not. Accept the values from the calling program and pass as an argument to the
procedure.
5.
Write a Function to accept a number as an argument and reverse it. Then return the value.
6.
Write a Function to accept the total mark as an argument and return the grade to the calling program.(Max
Total : 500)
If Total is less than 50% Then Grade is Fail
If between 50% and 60% Then Grade is Third
If between 60% and 70% Then Grade is Second
If between 70% and 80% Then Grade is First
Above 80% is Distinction.
7.
Write a Function to accept the CourseName as an argument and return the total number of students registered
for that course.
8.
Write a Function to accept the CourseID as an argument and return the total fees collected from that course.
9.
Write a program to create a RECORD type to hold the record from the EMP table and display the contents
from that RECORD.
10.
Write a program to create a ARRAY type to hold the salaries from the EMP table and to find the average of
the salaries from the ARRAY type.
11.
Write a trigger on EMP table so that the user cannot delete records on Saturday.
12.
Write a trigger on EMP table to display the total number of employees whenever a record is inserted.
13.
Write a trigger on STUDENT table so that the user cannot update the FirstName if the FirstName is Rahul.
14.
Write a trigger on EMP table so that the user cannot insert a duplicate EMPID.
15.
Alter the EMPLOG table by adding a field : OType CHAR(1).
Write a trigger on EMP table so as to maintain the Log information. If any record is inserted or deleted or updated,
insert the EmpID, UserName, DoneOn and OType. (If Inserting, then OType = I, Update=U Delete=D).
16.
Write a program to accept two numbers and divide it. If the second number is zero then handle the exception.
17.
Write a program to display all the EMP records. Use cursors. First OPEN the cursor and then use FOR loop
cursor to print the records. If any exception, then handle it.
18.
Write a program to accept CourseID, StudID and DOJ. Check the CourseID whether that Course has
maximum number of students. If so, raise application error with an error message. If not, check whether the StudID is
existing or not. If not existing, raise no data found exception. Or else insert the record onto the REGISTRATION
table. (Assume Max Students : 3)
19.
Write a UserDefinedException to handle Negative Salaries not to be inserted onto the EMP table.
20.

Write a package to have the following:

a.
An ARRAY which can hold the Student ID list.
b.
A Procedure which takes a CourseID as an IN parameter and an Object of the above specified ARRAY as an
OUT Parameter. The procedure fills up the ARRAY with StudID who have joined for that course.
c.
A Procedure which takes the above ARRAY object and displays the record of the students in that ARRAY.
d.
A Function which takes the above ARRAY Object and returns the average age of the students.

Advanced SQL Queries


1.
2.
3.
4.
5.

Write a SQL query to delete only the the duplicate rows from the EMP table.
Write a SQL query to display the Nth Maximum Salary from the EMP table.
Write a SQL query to display the last N rows from the EMP table.
Write a SQL query to display only the Nth row from the EMP table.
Write a SQL query to delete the Nth row from the EMP table.

Vous aimerez peut-être aussi