You are on page 1of 20

Author name : Suhel Daimi

Question 1.

Using the following database :-


college ( cname , city, address, phone, afdate)
staffs (sid, sname, saddress, contects)
staffjoins(sid, cname, dept, DOJ, post, salary)
teachings (sid, class, paperid, fsession, tsession)
subjects (paperid, subject, paperno. , papername)

write SQL statement for the following :-

a. Create the above tables with the given specifications and constraints.

Table no. 1

Query:

Create table Colleges ( cname varchar2(20) primary key, city varchar2(20),


Address varchar2(20), phone number(11), afdate date);

Desc Colleges;

Table no. 2

Page-1
Author name : Suhel Daimi

Query:

CREATE TABLE Staffs( sid NUMBER(20) PRIMARY KEY,


sname VARCHAR2(30),
saddress VARCHAR2(50),
contacts NUMBER(15));

DESC Staffs;

Table no. 3

Page-2
Author name : Suhel Daimi

Query:
CREATE TABLE StaffJoins(sid NUMBER(10),
cname VARCHAR2(20),
dept VARCHAR2(20),
DOJ DATE,
post VARCHAR2(20),
salary NUMBER(10) );

DESC StaffJoins;

Table no. 4

Page-3
Author name : Suhel Daimi

Query:

create table Teachings ( sid number(10), class varchar2(10), paperid number(10), fsession date ,
tsession date );

desc Teachings;

Table no. 5

Page-4
Author name : Suhel Daimi

Query:

create table Subjects ( paperid number(10) primary key, subject varchar2(10), paperno
number(10), papername varchar2(10));

desc Subjects;

Page-5
Author name : Suhel Daimi

Question 2. Insert about 10 rows as are appropriate to solve the following queries.
Table-colleges

Query:

insert all

into colleges values ('pcps','Raipur','aashirwad tower',07712645788,'06-jul-09')

into colleges values ('Science college','Raipur','G.E. ROAD',0878676723,'30-jun-98')

into colleges values ('amity','Raipur','Randri',07712645344,'12-feb-11')

into colleges values ('durga','Raipur','Aamapara',07712645987,'07-jun-89')

into colleges values ('daga','Raipur','Devandra nagar',07712698765,'06-jul-98')

into colleges values ('princess college','Raipur','',07712645788,'06-jul-09')

into colleges values ('disha','Raipur','Chankhuri',07712645456,'06-jul-10')

into colleges values ('BIT college','Durg','Civil line',07712645456,'06-jul-09')

into colleges values ('Central college','Durg','Civil line',6790765752,'30-jun-02')

into colleges values ('Genesis college','Dhamtari','Sadar',8798098789,'06-dec-02')

select * from dual;

Page-6
Author name : Suhel Daimi

10 row(s) inserted

select * from colleges;

Table – staffs

Query:

insert all

into staffs values (1101,'Abhay','Raipur',9076780967)

into staffs values (1102,'Sonam','Dhamtari',9809876787)

into staffs values (1103,'Lavanya','Raigarh',7898909898)

into staffs values (1104,'Raghu','Raipur',6789898790)

into staffs values (1105,'Rahul','Kanker',7898797365)

into staffs values (1106,'Vijay','Durg',6745356798)

into staffs values (1107,'Madhu','Raipur',8976453212)

into staffs values (1108,'Suman','Dhamtari',8945098765)

into staffs values (1109,'Ravi','Durg',9078563412)

into staffs values (1110,'Heena','Bhilai',9087654321)

Page-7
Author name : Suhel Daimi

select * from dual;

10 row(s) inserted.

select * from staffs;

Table - staffjoins

Query:

insert all

into staffjoins values (1101,'amity','science','01-jul-00','HOD',50000)

into staffjoins values (1102,'daga','maths','05-jun-02','professor',30000)

into staffjoins values (1103,'durga','computer','06-nov-08','asst. prof.',40000)

into staffjoins values (1104,'disha','computer','07-jul-99','HOD',45000)

into staffjoins values (1105,'Science college','computer','07-jul-99','professor',25000)

into staffjoins values (1106,'Central college','maths','11-jun-05','professor',30000)

into staffjoins values (1107,'Genesis college','science','13-aug-17','professor',30000)

into staffjoins values (1108,'pcps','electronics','01-jul-16','HOD',45000)

into staffjoins values (1109,'BIT college','commerce','27-nov-01','asst. prof.',35000)

Page-8
Author name : Suhel Daimi

into staffjoins values (1110,'Science college','maths','30-jul-09','professor',25000)

select * from dual;

10 row(s) inserted

select * from staffjoins;

Table-Teaching

Query:

insert all

into Teachings values (1101,'bca-1',1101,'01-jul-00','02-mar-01')

into Teachings values (1102,'bca-3',1103,'01-jun-01','15-sep-01')

into Teachings values (1103,'bsc-1',1102,'01-jan-02','01-oct-02')

into Teachings values (1104,'bca-2',2202,'01-apr-03','15-sep-03')

into Teachings values (1105,'bsc-2',3301,'15-jun-99','15-may-99')

into Teachings values (1106,'bsc-3',3302,'02-oct-04','20-dec-04')

into Teachings values (1107,'bsc-1',3303,'01-jan-98','01-jan-99')

Page-9
Author name : Suhel Daimi

into Teachings values (1108,'bca-2',2201,'02-sep-06','01-jan-07')

into Teachings values (1109,'bcom-1',2203,'01-jan-00','20-dec-00')

into Teachings values (1110,'bca-1',2205,'05-dec-04','25-apr-05')

select * from dual;

select * from teachings;

Table – Subjects

Query:

insert all

into Subjects values (1101,'DBMS',08,'internals')

into Subjects values (1103,'C++',18,'internals')

into Subjects values (1102,'DBMS',28,'internals')

into Subjects values (2202,'C',13,'internals')

into Subjects values (3301,'C++',10,'internals')

into Subjects values (3302,'DBMS',12,'internals')

Page-10
Author name : Suhel Daimi

into Subjects values (3303,'C++',24,'internals')

into Subjects values (2201,'OS',27,'internals')

into Subjects values (2203,'C',14,'internals')

into Subjects values (2205,'OS',21,'internals')

select * from dual;

10 row(s) inserted

select * from Subjects;

Page-11
Author name : Suhel Daimi

Question 3. List the name of the teachers teaching computer subject.


Query:

select sname from staffs where sid in (select sid from Teachings where paperid in (select
paperid from Subjects where subject in ('DBMS')));

Question 4 List the name and cities of all staffs working in your college.
Query:

select staffs.sname,staffs.saddress from colleges,staffs,staffJoins where colleges.cname =


'Science college' and colleges.cname = staffJoins.cname and staffJoins.sid = staffs.sid;

Page-12
Author name : Suhel Daimi

Question 5 . List the name and cities of all staffs working in your college who earn
more than 15,000
Query:

select staffs.sname,staffs.saddress from colleges,staffs,staffjoins where colleges.cname =


'Science college' and colleges.cname = staffJoins.cname and staffJoins.sid = staffs.sid and
staffJoins.salary > 15000;

Question 6. find the staffs whose name start with 'm' or 'r' and ends with 'a' and\ or 7
character long.
Query:

select sname from staffs where sname like 'M%a' or sname like 'R%a' or vsize(sname) = 7;

Page-13
Author name : Suhel Daimi

Question 7 .Find the staffs whose date of joining is 2005.


Query:
select sname from Staffs,StaffJoins where Staffs.sid = StaffJoins.sid and StaffJoins.DOJ like '%
%-%%%-05';

Question 8. Modify the database so that staffs N1 now works in C2 college.


Query:
update staffJoins set cname = 'daga' where sid = 1101;

Page-14
Author name : Suhel Daimi

Question 10. List the name of subjects, which T1 teacher in this session or all the
sessions.
Query:
select subject from subjects,teachings where teachings.sid = 1102 and ( teachings.fsession like
'%%-%%%-%%' or teachings.fsession like '%%-%%-19');

Question 11. Find the classes that T1 do not teach at present session.
Query :
select class from teachings where class <>(select class from teachings where sid=1);

Page-15
Author name : Suhel Daimi

Question 12. Find the college who have most number of staffs.
Query:

create view clg_staffs as (select cname,count(sid) staffs from staffjoins group by cname);

create view min_clg as (select distinct(c1.cname) from clg_staffs c1,clg_staffs c2


where c1.staffs<c2.staffs);

select * from clg_staffs c1,clg_staffs c2 where c1.staffs<c2.staffs;

Page-16
Author name : Suhel Daimi

select cname from clg_staffs where cname not in (select cname from min_clg);

final output:

Page-17
Author name : Suhel Daimi

Question 13. Find the staffs that earn a higher salary who earn greater than average salary
of their college.
Query:

create view avg_salary as (select cname,avg(salary) avg_sal from StaffJoins group by cname);

select * from avg_salary;

Select st.sid,st.cname,st.salary,av.avg_sal from StaffJoins st,avg_salary av where st.cname =


av.cname and st.salary>av.avg_sal;

Select sname from staffs where sid in (select st.sid from StaffJoins st, avg_salary av where
st.cname =av.cname and st.salary > av.avg_sal);

Page-18
Author name : Suhel Daimi

Question 14. Find the college whose average salary is more than average salary of C2
college.
Query:

Select cname from avg_salary where avg_sal > (select avg_sal from avg_salary where

cname = 'Science college' );

Question 15. Find the college that has the smallest payroll

Query:

select cname,sum(salary) sal from staffJoins group by cname;

create view payroll as(select cname,sum(salary) sal from staffJoins group by cname);

Page-19
Author name : Suhel Daimi

select distinct(y.cname) from payroll x,payroll y where x.sal < y.sal;

Select cname from payroll where cname not in(select distinct(y.cname) from payroll x,payroll y
where x.sal < y.sal);

Page-20

You might also like