0% found this document useful (0 votes)
16 views

Lab Program 2

i. This query finds the names of juniors enrolled in classes taught by Professor Harshith by joining the student, class, and enrolled tables to filter for juniors in classes with Harshith's faculty ID. ii. This query finds class names that meet in room R128 or have 5 or more enrolled students by selecting class names for the room or those with an enrollment count greater than 5. iii. This query finds student names enrolled in two classes that meet at the same time by self-joining the class table and then joining to the enrolled and student tables to filter for students in classes with the same meeting time

Uploaded by

Raghu C
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Lab Program 2

i. This query finds the names of juniors enrolled in classes taught by Professor Harshith by joining the student, class, and enrolled tables to filter for juniors in classes with Harshith's faculty ID. ii. This query finds class names that meet in room R128 or have 5 or more enrolled students by selecting class names for the room or those with an enrollment count greater than 5. iii. This query finds student names enrolled in two classes that meet at the same time by self-joining the class table and then joining to the enrolled and student tables to filter for students in classes with the same meeting time

Uploaded by

Raghu C
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Program - 1

Consider the following relations:

Student (snum: integer, sname: string, major: string,


level: string, age: integer)
Faculty (fid: integer, fname: string, deptid: integer)
Class (cname: string, meets at: string, room: string, fid:
integer)
Enrolled (snum: integer, cname: string)
Write the following queries in SQL. No duplicates should be printed
in any of the answers.
i. Find the names of all Juniors (level = JR) who are enrolled
in a class taught by Prof. Harshith
ii. Find the names of all classes that either meet in room R128
or have five or more Students enrolled.
iii. Find the names of all students who are enrolled in two
classes that meet at the same time.
iv. Find the names of faculty members who teach in every
room in which some class is taught.
v. Find the names of faculty members for whom the combined
enrolment of the courses that they teach is less than five.
vi. Find the deptids of the faculty who are teaching 2 or more classes.
vii. Find the names of the students who are enrolled in the class ‘C01’
and whose age is greater than 20.
viii. : Find the names of the faculty who are teaching senior students.
SQL> create table student
(snum number(4) primary key,
sname varchar2(10), major varchar2(5),
slevel varchar2(10), age number(3));

SQL> create table faculty


(fid number(4) primary key,
fname varchar2(10), deptid number(5));
SQL> create table class
(cname varchar2(4) primary key,
meets_at varchar2(10), room varchar2(10),
fid number(4) references faculty);

SQL> create table enrolled


(snum number(4) references student,
cname varchar2(4) references class,
primary key(snum, cname) );
SQL> insert into student values
(&snum, ’&sname’, ‘&major’, ‘&slevel’, &age);
Snum Sname Major Level Age
1 Adarsh CSE Jr 18

2 Akhil ISE Sr 22

3 Bhavya CSE Sr 21

4 Shwetha CSE Jr 19

5 Nagaraj ISE Sr 22

6 Hema CSE Jr 19
SQL> insert into faculty values
(&fid, ‘&fname’, &deptid);
Fid Fname Deptid

501 Harshith 201

502 Shobha 202

503 Chaitra 203

504 Pratap 204

505 Sushma 205


SQL> insert into class values
( ‘&cname’, ‘&meets_at’, ‘&room’, &fid);
Cname Meets_at Room Fid
C01 10am R128 501
C02 11am R124 501
C03 11am R125 502
C04 12pm R128 502
C05 2pm R128 503
C06 2pm R124 502
C07 3pm R125 501
C08 12pm R124 504
SQL> insert into enrolled values (&snum, ‘&cname’);
Snum Cname
1 C01
2 C03
3 C06
4 C07
2 C02
3 C01
4 C02
2 C01
5 C01
6 C01
i. Find the names of all Juniors (level = JR) who are
enrolled in a class taught by Prof. Harshith.
Step 1: Find Harshith’s fid
(from faculty table)
Step 2: Find cname for the above fid
(from class table)
Step 3: Find snum for the above cname
(from the enrolled table)
Step 4: Print sname for the above snum
where slevel is ‘Jr’ (from student table)
SQL> select sname from student where
slevel =‘Jr’ and snum in
(select snum from enrolled where cname in
(select cname from class where fid in
(select fid from faculty where
fname = ‘Harshith’ ) ) );
(or)
SQL> select distinct sname from
student s, class c, enrolled e, faculty f
where f.fname = ‘Harshith and
f.fid = c.fid and c.cname = e.cname and
e.snum = s.snum and s.slevel = ‘Jr’;
ii. Find the names of all classes that either meet in
room R128 or have five or more Students enrolled.

1. Find the names of all classes (i.e cname) that


meet in room R128.
(from class table).

2. Find the names of all classes(i.e cname) that


have five or more students enrolled.
(from enrolled table)
SQL> select cname from class where
room = ‘R128’ or cname in
(select cname from enrolled
group by cname
having count(*)>=5 );
iii. Find the names of all students who are enrolled in
two classes that meet at the same time.

1. Find the cnames that meets at the same time.


(from class table)
2. Find the snum for the above cnames.
(from enrolled table)
3. Find the sname for the above snum.
(from student table)
SQL> select sname from student where snum in
(select snum from enrolled where cname in
(select distinct c1.cname
from class c1, class c2 where
c1.cname <> c2.cname and
c1.meets_at = c2.meets_at ) );
C1:class C2:class
Cname Meets_at Cname Meets_at
C01 10am C01 10am
C02 11am C02 11am
C03 11am C03 11am
C04 12pm C04 12pm
C05 2pm C05 2pm
C06 2pm C06 2pm
C07 3pm C07 3pm
C08 12pm C08 12pm
iv. Find the names of faculty members who teach in
every room in which some class is taught.

1. Find the available room numbers.


(from class table)
2. Find all the room numbers which are taught by
particular faculty id (for example 501).
(from class table)
SQL> select f.fname from faculty f
where not exists
(select distinct room from class
minus
select c1.room from class c1
where c1.fid = f.fid);
501
(124, 125, 128) - (124, 125, 128)
returns no records (so NOT EXISTS returns TRUE)

502
(124, 125, 128) - (124, 125, 128)
returns no records (so NOT EXISTS returns TRUE)

503
(124, 125, 128) - (128) returns records

504
(124, 125, 128) - (124) returns records

505
(124, 125, 128) - (empty) returns records
v. Find the names of faculty members for whom the
combined enrolment of the courses that they teach is
less than five.
(i.e we need to find the faculty names who teach less
than 5 cnames and these cnames must be enrolled .

1. Find the fids who are teaching less than 5


cnames. (from class table)

2. Find the faculty names for the above fids and


ensure that cnames which they are teaching
are enrolled.
(from faculty, class and enrolled tables)
SQL> select distinct f.fname
from faculty f, class c, enrolled e
where f.fid = c.fid and e.cname = c.cname
and c.fid in
(select fid from class group by fid
having count(*) < 5 );
Ex: Find the deptids of the faculty who are
teaching 2 or more classes.
Select deptid from faculty where fid in
(select fid from class
group by fid having count(*)>=2);
Ex: Find the names of the students who are
enrolled in the class ‘C01’ and whose age is
greater than 20.
Select sname from student
where age>20 and
snum in (select snum from enrolled
where cname=‘C01’);
Ex: Find the names of the faculty who are
teaching senior students.

You might also like