You are on page 1of 18

Information Systems

And Data Management

Practical File Submission

Submitted by-
RAHUL SINGH
323/CO/07
COE-II (C3), III Year
Subject Code – COE317
INDEX

Problem 1 Date :

Q: Student (srno, rollno, dob, age, dept, marks, name)


A) Retrieve names of students by roll numbers.
B) Retrieve names of students whose age is less than 20.
C) Arrange table in ascending order of marks.
D) Retrieve names of students with department MPA and marks greater than 70.
E) Retrieve names of students with marks greater than average marks.

Problem 2 Date :

Q: Student (sid, name, major, credits)


Faculty (fid, fname, rank, dept)
Class (courseno, schedule, fid, room)
Enroll (courseno, sid, grade)
A) Retrieve name, id & number of credits of math major students.
B) Retrieve id and names of students whose course is MPA 304.
C) Retrieve courseno, name and major of students enrolled in course taught by
faculty F110.
D) Retrieve names of students with largest number of credits.
E) Retrieve course in which less than 3 students are enrolled.
F) Change major of student with sid S102 to music.
G) Change room to R211 for courses taught by ABC.

Problem 3 Date :

Q: Student (sname, classno, theorymarks, drivingmarks)


Studentdrivingteacher (sname, drivingteachername)
Teachervehicle (drivingteachername, vehicle, theory, practical)
Vehicle (modelyear, vehicle)
A) Retrieve teachers who teach theory and give driving lessons on all vehicles.
B) Retrieve pairs of students satisfying :
i) Have same marks
ii) Have different theory teachers
iii) Have same driving marks but different driving teachers
C) Find students who are neither taught theory nor driving lesson by johnson.
D) Retrieve names of students with more marks than john in driving and theory.
E) Add age in student table.
F) List students having marks >= average theory class marks of class 8.
Problem 4 Date :

Q: Dept (deptno not null number(2), dname varchar(14), loc varchar2(13))


Emp (empno not null number(4), ename varchar2(10), job varchar2(12), mgr
number(4), hiredate date, sal number(7,2), comm number(7,2), deptno number(2))

MGR is employee number of employee whom the employee reports to. Department
number is foreign key.
A) List all employees who have atleast one person reporting to them.
B) List employee details if and only if more than 5 employees are present in
department number 10.
C) List all employee names with their immediate higher authority.
D) Employees who don’t manage anyone.
E) Employees whose salary > lowest salary of an employee of dept number 20.
F) Employees earning more than highest paid manager.
G) Highest salary paid for each job
H) Most recently hired employee in each department.
I) Year when most people joined the company. Display the year as well as number
of employees.
J) Department having highest renumeration bill.
K) Display a ‘*’ against list of most recently hired employees.
L) Correlated subquerries to list out employees who earn more than avg salary of
their dept.
M) Find nth maximum salary
N) Length of service of each employee.
PROBLEM – 1
Q: Student (srno, rollno, dob, age, dept, marks, name)

A) Retrieve names of students by roll numbers.


B) Retrieve names of students whose age is less than 20.
C) Arrange table in ascending order of marks.
D) Retrieve names of students with department MPA and marks greater than 70.
E) Retrieve names of students with marks greater than average marks.

Solution : To create table and insert values –


create table students ( srno integer , rollno integer primary key, dob
date, age integer, dept varchar(5), marks float, name varchar(20));
insert into students values
( 1, 323, '1989-4-19', 21, 'COE', 78.45, 'A' ),
( 2, 321, '1988-7-9', 22, 'COE', 71.45, 'B' ),
( 3, 322, '1990-8-29', 20, 'IT', 68.45, 'C' ),
( 4, 213, '1990-7-19', 22, 'MPA', 55.45, 'D' ),
( 5, 332, '1992-9-3', 20, 'ECE', 45.85, 'E' ),
( 6, 267, '8-4-1998', 12, 'ICE', 80.5, 'F' ),
( 7, 297, '1988-1-23', 22, 'MPA', 69.67, 'G' ),
( 8, 300, '1987-3-5', 23, 'MPA', 62.31, 'H' ),
( 9, 299, '1991-6-1', 19, 'BT', 7.45, 'I' ),
( 10, 201, '1988-11-18', 22, 'MPA', 78.49, 'J' ),
( 11, 371, '1988-12-12', 22, 'MPA', 79.95, 'K' );
select rollno , name from students order by rollno; 1)
OUTPUT
Rollno name
201 J
213 D
267 F
297 G
299 I
300 H
321 B
322 C
323 A
332 E
371 K

select name from students where age <= 20; 2)


OUTPUT
name
F
I
C
E

select rollno, name, marks from students order by marks; 3)


OUTPUT
rollno name marks
299 I 7.45
332 E 45.85
213 D 55.45
300 H 62.31
322 C 68.45
297 G 69.67
321 B 71.45
323 A 78.45
201 J 78.49
371 K 79.95
267 F 80.5

select name from students where dept = 'MPA' and marks >= 70; 4)
OUTPUT
name
J
K

5) select rollno, name, marks from students where marks >= ( select
AVG(marks) from students);
OUTPUT
rollno name marks
201 J 78.49
267 F 80.5
297 G 69.67
321 B 71.45
322 C 68.45
323 A 78.45
371 K 79.95
PROBLEM – 2
Q: Student (sid, name, major, credits)
Faculty (fid, fname, rank, dept)
Class (courseno, schedule, fid, room)
Enroll (courseno, sid, grade)
A) Retrieve name, id & number of credits of math major students.
B) Retrieve id and names of students whose course is MPA 304.
C) Retrieve courseno, name and major of students enrolled in course taught by
faculty F110.
D) Retrieve names of students with largest number of credits.
E) Retrieve course in which less than 3 students are enrolled.
F) Change major of student with sid S102 to music.
G) Change room to R211 for courses taught by ABC.

Solution : To create table and insert values –


create table students (sid varchar(10) primary key, name varchar(20),
major varchar(10), credits integer);
create table faculty (fid varchar(10) primary key, fname varchar(20),
rank integer, dept varchar(10));
create table class (courseno varchar(10), schedule integer, fid
varchar(10) , room varchar(10));
create table enroll (courseno varchar(10), sid varchar(10) , grade
integer);
insert into students values
( 'S100', 'SA', 'Maths', 20 ),
( 'S102', 'SB', 'Maths', 2 ),
( 'S101', 'SC', 'Maths', 23 ),
( 'S103', 'SD', 'Maths', 23 ),
( 'S104', 'SE', 'Maths', 10 ),
( 'S106', 'SF', 'English', 7 ),
( 'S105', 'SG', 'C', 15 ),
( 'S107', 'SH', 'SQL', 23 ),
( 'S108', 'SI', 'Chemistry', 6 ),
( 'S109', 'SJ', 'Chemistry', 18 ),
( 'S110', 'SK', 'Music', 20 ),
( 'S111', 'SL', 'C', 20 ),
( 'S112', 'SM', 'C', 2 );

insert into faculty values


( 'F110', 'ABC', 2,'Maths'),
( 'F111', 'FF', 1,'Maths'),
( 'F112', 'FG', 2,'Music'),
( 'F113', 'FH', 3,'C'),
( 'F101', 'FI', 4,'C'),
( 'F102', 'FJ', 5,'SQL'),
( 'F103', 'FE', 6,'SQL'),
( 'F104', 'FD', 1,'Chemistry'),
( 'F105', 'FC', 1,'Chemistry'),
( 'F106', 'FB', 8,'Chemistry');

insert into class values


( 'MPA304', 40, 'F110', 'R201'),
( 'MPA304', 30, 'F106', 'R210'),
( 'MPA304', 20, 'F105', 'R212'),
( 'COE112', 10, 'F104', 'R209'),
( 'COE112', 10, 'F103', 'R208'),
( 'COE112', 30, 'F112', 'R207'),
( 'COE102', 20, 'F113', 'R206'),
( 'MPA301', 40, 'F111', 'R205');

insert into enroll values


( 'MPA304', 'S100', 22),
( 'MPA304', 'S101', 2),
( 'MPA304', 'S102', 12),
( 'MPA304', 'S103', 25),
( 'MPA304', 'S104', 9),
( 'COE112', 'S105', 5),
( 'COE112', 'S106', 32),
( 'COE112', 'S107', 11),
( 'COE112', 'S108', 22),
( 'MPA301', 'S109', 22),
( 'MPA301', 'S110', 16),
( 'MPA301', 'S112', 13);

1) select sid, name, credits from students where major = 'Maths';

OUTPUT
sid name credits
S100 SA 20
S101 SC 23
S103 SD 23
S104 SE 10

2) select sid, name from students where students.sid in ( select sid


from enroll where courseno = 'MPA304') ;

OUTPUT
sid name
S100 SA
S101 SC
S102 SB
S103 SD
S104 SE

3) select enroll.courseno, students.sid, students.name from (enroll


inner join class on class.courseno = enroll.courseno) inner join
students on students.sid = enroll.sid where class.fid = 'F110' ;
OUTPUT
courseno sid name
MPA304 S100 SA
MPA304 S101 SC
MPA304 S102 SB
MPA304 S103 SD
MPA304 S104 SE

4) select name from students where credits = ( select MAX(credits)


from students);
OUTPUT
name
SC
SD
SH

5) select courseno, count(courseno) as 'No. of students enrolled'


from enroll group by courseno having COUNT(courseno) <= 3

OUTPUT
courseno No. of students enrolled
MPA301 3

6) select * from students where sid = 'S102';


update students set major = 'Music' where sid = 'S102' ;
select * from students where sid = 'S102';

OUTPUT
sid name major credits
S102 SB Maths 2
___________________________
sid name major credits
S102 SB Music 2
7) select * from class where fid = ( select fid from faculty where
fname = 'ABC');
update class
set room = 'R211'
where
class.fid = ( select fid from faculty where fname = 'ABC');
select * from class where fid = ( select fid from faculty where
fname = 'ABC');

OUTPUT
courseno schedule fid room
MPA304 40 F110 R201
__________________________________
courseno schedule fid room
MPA304 40 F110 R211
PROBLEM – 3
Q: Student (sname, classno, theorymarks, drivingmarks)
Studentdrivingteacher (sname, drivingteachername)
Teachervehicle (drivingteachername, vehicle, theory, practical)
Vehicle (modelyear, vehicle)
A) Retrieve teachers who teach theory and give driving lessons on all vehicles.
B) Retrieve pairs of students satisfying :
iv) Have same marks
v) Have different theory teachers
vi) Have same driving marks but different driving teachers
C) Find students who are neither taught theory nor driving lesson by johnson.
D) Retrieve names of students with more marks than john in driving and theory.
E) Add age in student table.
F) List students having marks >= average theory class marks of class 8.

Solution : To create table and insert values –


create table Students (sname varchar(20), classno integer,
theorymarks integer, drivingmarks integer);
create table Studentdrivingteacher (sname varchar(20),
drivingteachername varchar(20));
create table Teachervehicle (drivingteachername varchar(20),
vehicle varchar(10), theory varchar(5), practical varchar(5))
create table Vehicle (modelyear integer, vehicle varchar(10))

insert into students values


( 'John', 8, 74, 80 ),
( 'SA', 8, 74, 80 ),
( 'SB', 9, 56, 35 ),
( 'SC', 7, 98, 88 ),
( 'SD', 10, 38, 89 ),
( 'SE', 11, 45, 92 ),
( 'SF', 8, 67, 44 ),
( 'SG', 2, 41, 85 );

insert into studentdrivingteacher values


( 'John', 'Johnson' ),
( 'SA', 'TA' ),
( 'SB', 'TB' ),
( 'SC', 'TB' ),
( 'SD', 'TA' ),
( 'SE', 'TA' ),
( 'SF', 'Johnson' ),
( 'SG', 'Johnson' );

insert into teachervehicle values


( 'Johnson', 'Car', 'Y', 'Y'),
( 'Johnson', 'Scooter', 'Y', 'Y'),
( 'Johnson', 'Bike', 'Y', 'Y'),
( 'TA', 'Car', 'Y', 'N'),
( 'TB', 'Scooter', 'N', 'Y');

insert into vehicle values


( 1966, 'Car'),
( 1961, 'Scooter'),
( 1956, 'Bike');

1) select distinct drivingteachername from Teachervehicle where theory


= 'Y' and practical = 'Y';
OUTPUT

drivingteachername
John

2)
i) select t.sname , d.sname from students as t, Students as d
where t.theorymarks = d.theorymarks and t.drivingmarks =
d.drivingmarks and t.sname <> d.sname

OUTPUT

sname sname
John SA
SA John

ii) select t.sname , d.sname from studentdrivingteacher as t,


Studentdrivingteacher as d where t.drivingteachername in (
select drivingteachername from Teachervehicle where theory
= 'Y' ) and d.drivingteachername in ( select
drivingteachername from Teachervehicle where theory = 'Y' )
and t.drivingteachername <> d.drivingteachername ;

OUTPUT

sname sname
John SA
John SD
John SE
SA John
SA SF
SA SG
SD John
SD SF
SD SG
SE John
SE SF
SE SG
SF SA
SF SD
SF SE
SG SA
SG SD
SG SE

iii) select t.sname , d.sname from studentdrivingteacher as t,


Studentdrivingteacher as d , students as a, students as b
where t.drivingteachername <> d.drivingteachername and
a.drivingmarks = b.drivingmarks and t.sname = a.sname and
d.sname = b.sname ;

OUTPUT

sname sname
John SA
SA John

3) select sname from Studentdrivingteacher where drivingteachername


<>'Johnson'

OUTPUT

sname
SA
SB
SC
SD
SE

4) select sname from Students where theorymarks > ( select theorymarks


from students where sname = 'John') and drivingmarks > ( select
drivingmarks from students where sname = 'John');

OUTPUT

sname
SC
5) SeLECT * FROM STUDENTS;
alter table students
add age integer
update Students set age = 20 where sname = 'John';
update Students set age = 21 where sname = 'SA' or sname = 'SE' ;
update Students set age = 18 where sname = 'SB' or sname = 'SF';
update Students set age = 16 where sname = 'SC' or sname = 'SG';
update Students set age = 22 where sname = 'SD';
select * from Students

OUTPUT

sname classno theorymarks drivingmarks


John 8 74 80
SA 8 74 80
SB 9 56 35
SC 7 98 88
SD 10 38 89
SE 11 45 92
SF 8 67 44
SG 2 41 85
___________________________________________

sname classno theorymarks drivingmarks age


John 8 74 80 20
SA 8 74 80 21
SB 9 56 35 18
SC 7 98 88 16
SD 10 38 89 22
SE 11 45 92 21
SF 8 67 44 18
SG 2 41 85 16

6) select sname from students where theorymarks >= (select


AVG(theorymarks) from Students where classno = 8);

OUTPUT

sname
John
SA
SC
PROBLEM – 4
Q: Dept (deptno not null number(2), dname varchar(14), loc varchar2(13))
Emp (empno not null number(4), ename varchar2(10), job varchar2(12), mgr
number(4), hiredate date, sal number(7,2), comm number(7,2), deptno number(2))

MGR is employee number of employee whom the employee reports to. Department
number is foreign key.
A) List all employees who have atleast one person reporting to them.
B) List employee details if and only if more than 5 employees are present in
department number 10.
C) List all employee names with their immediate higher authority.
Employees who don’t manage anyone. D)
E) Employees whose salary > lowest salary of an employee of dept number 20.
F) Employees earning more than highest paid manager.
G) Highest salary paid for each job
H) Most recently hired employee in each department.
I) Year when most people joined the company. Display the year as well as number
of employees.
J) Department having highest renumeration bill.
K) Display a ‘*’ against list of most recently hired employees.
L) Correlated subquerries to list out employees who earn more than avg salary of
their dept.
M) Find nth maximum salary
N) Length of service of each employee.

Solution : To create database and insert values -


create table Dept (deptno integer not null primary key , dname
varchar(14), loc varchar(13))
create table Emp (empno integer not null, ename varchar(10), job
varchar(12), mgr integer, hiredate date, sal float, comm float,
deptno integer )--foreign key references dept(deptno))

insert into Dept values


( 10, 'HR1', 'Delhi'),
( 20, 'HR3', 'Calcutta'),
( 30, 'R&D', 'Delhi'),
( 40, 'CS', 'Mumbai'),
( 50, 'MNG', 'Delhi');

insert into Emp values


( 01, 'A', 'Engineer', 22, '1999-12-23', 7000.56, 200.56, 10),
( 02, 'B', 'Engineer', 20, '2010-11-12', 8000.56, 200.56, 10),
( 03, 'C', 'Trainer', 20, '2010-2-22', 6000.56, 270.56, 50),
( 04, 'D', 'Researcher', 22, '1999-8-9', 9000.56, 250.56, 30),
( 05, 'E', 'Engineer', 01, '1996-5-2', 17000.56, 210.56, 30),
( 06, 'F', 'Engineer', 20, '1996-6-11', 17900.56, 210.56, 10),
( 07, 'G', 'Engineer', 02, '1998-10-11', 15670.56, 210.56, 10),
( 08, 'H', 'Engineer', 22, '2001-11-23', 12340.56, 210.56, 10),
( 21, 'I', 'Manager', NULL, '2002-7-1', 27000.56, 100.56, 10),
( 20, 'J', 'Manager', NULL, '2006-12-12', 9000.56, 2100.56, 20),
( 22, 'K', 'Manageer', NULL, '1999-1-3', 100.56, 2000.56, 50);

1) select * from Emp where empno in ( select mgr from Emp group by
mgr);

OUTPUT
empno ename job mgr hiredate sal comm deptno
1 A Engineer 22 1999-12-23 7000.56 200.56 10
2 B Engineer 20 2010-11-12 8000.56 200.56 10
20 J Manager NULL 2006-12-12 9000.56 2100.56 20
22 K Manageer NULL 1999-01-03 100.56 2000.56 50

2) select * from emp where deptno = (select deptno from Emp group by
deptno having count(deptno) >= 5)

OUTPUT
empno ename job mgr hiredate sal comm deptno
1 A Engineer 22 1999-12-23 7000.56 200.56 10
2 B Engineer 20 2010-11-12 8000.56 200.56 10
6 F Engineer 20 1996-06-11 17900.56 210.56 10
7 G Engineer 2 1998-10-11 15670.56 210.56 10
8 H Engineer 22 2001-11-23 12340.56 210.56 10
21 I Manager NULL 2002-07-01 27000.56 100.56 10

3) select t.ename as 'subordinate', d.ename ' Immediate higher


authority' from emp as t, Emp as d where t.mgr = d.empno

OUTPUT
subordinate Immediate higher authority
A K
B J
C J
D K
E A
F J
G B
H K

4) select * from emp where emp.empno not in (select mgr from Emp
where mgr is not null)
OUTPUT
empno ename job mgr hiredate sal comm deptno
3 C Trainer 20 2010-02-22 6000.56 270.56 50
4 D Researcher 22 1999-08-09 9000.56 250.56 30
5 E Engineer 1 1996-05-02 17000.56 210.56 30
6 F Engineer 20 1996-06-11 17900.56 210.56 10
7 G Engineer 2 1998-10-11 15670.56 210.56 10
8 H Engineer 22 2001-11-23 12340.56 210.56 10
21 I Manager NULL 2002-07-01 27000.56 100.56 10

5) select * from emp where emp.sal > (select MIN(sal) from Emp where
deptno = 20)

OUTPUT
empno ename job mgr hiredate sal comm deptno
1 A Engineer 22 1999-12-23 7000.56 200.56 10
2 B Engineer 20 2010-11-12 8000.56 200.56 10
3 C Trainer 20 2010-02-22 6000.56 270.56 50
4 D Researcher 22 1999-08-09 9000.56 250.56 30
5 E Engineer 1 1996-05-02 17000.56 210.56 30
6 F Engineer 20 1996-06-11 17900.56 210.56 10
7 G Engineer 2 1998-10-11 15670.56 210.56 10
8 H Engineer 22 2001-11-23 12340.56 210.56 10
21 I Manager NULL 2002-07-01 27000.56 100.56 10

6) select * from emp where emp.sal > (select Max(t.sal) from Emp as t
,emp as d where t.empno = d.mgr)

OUTPUT
empno ename job mgr hiredate sal comm deptno
5 E Engineer 1 1996-05-02 17000.56 210.56 30
6 F Engineer 20 1996-06-11 17900.56 210.56 10
7 G Engineer 2 1998-10-11 15670.56 210.56 10
8 H Engineer 22 2001-11-23 12340.56 210.56 10
21 I Manager NULL 2002-07-01 27000.56 100.56 10

7) select job , max(sal)as ‘Max Salary’ from emp group by job

OUTPUT
job Max Salary
Engineer 17900.56
Manager 27000.56
Researcher 9000.56
Trainer 6000.56
8) select ename from emp where hiredate > '2010-01-01'

OUTPUT
ename
B
C

9) select year(hiredate) as 'Year', count(empno) as 'No. of


employees' from emp group by year(hiredate)

OUTPUT
Year No. of employees
1996 2
1998 1
1999 3
2001 1
2002 1
2006 1
2010 2

10) select deptno , MAX( sal+comm) as ‘Max remuneration’from


Emp group by deptno;

OUTPUT
deptno Max remuneration
10 27101.12
20 11101.12
30 17211.12
50 6271.12

11) select '*' + ename as '*Employee' from Emp where (hiredate) >
'2010 - 1 - 1' ;

OUTPUT
*Employee
*B
*C

12) select t.ename , t.sal, t.deptno from emp as t where t.sal >
(select AVG(sal) from emp where t.deptno = deptno group by deptno
)

OUTPUT
ename sal deptno
F 17900.56 10
G 15670.56 10
I 27000.56 10
E 17000.56 30
C 6000.56 50

select ename , sal from Emp order by sal; 13)


OUTPUT
ename sal
K 100.56
C 6000.56
A 7000.56
B 8000.56
D 9000.56
J 9000.56
H 12340.56
G 15670.56
E 17000.56
F 17900.56
I 27000.56

14) select ename , datediff(day,hiredate, '2010-4-1') as 'x' from


Emp;

OUTPUT
ename x
A 3752
B -225
C 38
D 3888
E 5082
F 5042
G 4190
H 3051
I 2831
J 1206
K 4106

You might also like