Group 7 - Assignment 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

DBMS ASSIGNMENT-2

SQL QUERIES ON ACADEMIC INSTI DATABASE

QUERY-1 :-
Find the name and rollNo of students who have their advisor as HOD of the department same as
the student and the advisor must have joined before 2000.

select s.rollNo , s.name

from student as s , department as d , professor as p


where s.deptNo=d.deptId and s.advisor=d.hod and p.empId=s.advisor and p.startYear<2000

Firstly , we cross student , department and professor , then we apply the condition that the
student deptNo and departmentId match , check for the advisor id matching with the hod id
having start year > 2000.

QUERY-2 :-
Find the deptId and deptName of the departments which do not have any professor.

select d.deptId , d.name


from department as d
where d.deptId NOT IN (
select p.deptNo
from professor as p)

First get the deptNo of all the professors and select the complimentary set of the obtained
deptIds.

QUERY-3 :-
Find the name and rollNo of students who got atleast one U grade in the enrolled courses.

select s.rollNo , s.name


from student as s , enrollment as e , course as c
where s.deptNo=c.deptNo and s.rollNo=e.rollNo and c.courseId=e.courseId and grade='U'

First cross the student , enrollment and course and then apply the conditions that the
studentdept and course deptNo match , student rollNo and enrollment rollNo match and the
grade is 'U'.

QUERY-4 :-
Find the name and rollNo of students who have not taken atleast one course in their respective
department.

select s.rollNo , s.name


from student as s

where s.rollNo NOT IN (

select s1.rollNo
from student as s1 , enrollment as e , course as c
where s1.rollNo=e.rollNo and s1.deptNo=c.deptNo and e.courseId=c.courseId)

First find the students who have taken atleast one course in their respective department and
then finding the complement of that set of students gives the set of students who didnt take
atleast one course from their department.

QUERY-5 :-
Find the number of students who got S-grade in atleast one course in each department.

select d.deptId , d.name , count(distinct e.rollNo) as Sgradestudents


from department as d , enrollment as e , course as c
where d.deptId=c.deptNo and c.courseId=e.courseId and e.grade='S'
group by d.deptId , d.name

First find the departments with enrolled students getting S grade in its respective courses and
then grouping the departments together and finding the number of distinct rollNos in the
grouped data thus giving the number of students in each department who got atleast one S
grade in that department courses.

QUERY-6
Find the courses whose enrollment number has exceeded the classroom capacity(100) and need
larger halls in the current semester.

select e.courseId , c.cname , count(e.rollNo) as Enrollment

from enrollment as e , course as c


where c.courseId=e.courseId and e.sem='odd' and e.year='2002'
group by courseId
having count(rollNo)>100

First find the count of the number of students enrolled in each course of the current semester
and then conditioning that the count is greater than 100.

QUERY-7 :-
Find the maximum enrollment in a particular course out of all semester of all the years.

create view courseenrollment as (

select d.name , c.cname , count(e.rollNo) as Enrollment

from enrollment as e , course as c , department as d

where d.deptId=c.deptNo and e.courseId=c.courseId


group by e.courseId);

select name , cname , max(Enrollment) as maxEnrollment


from courseenrollment
group by name;

drop view courseenrollment


First create a view containing the count of the students enrolled in each of the course
throughout all the semesters and years and then group the courses , selecting the course with
maximum enrollment across all the sems and years that course is taught. Finally , drop the
view.

QUERY-8 :-
Find the courseId , department name and the coursename of the courses in which the number of
males and females enrolled are equal.

create view females as (


select distinct c.courseId , d.name , c.cname , count(distinct e.rollNo) as Females

from department as d , course as c , enrollment as e , student as s

where s.rollNo=e.rollNo and e.courseId=c.courseId and d.deptId=c.deptNo and


s.sex='female'
group by c.cname);

create view males as (

select distinct c.courseId , d.name , c.cname , count(distinct e.rollNo) as Males


from department as d , course as c , enrollment as e , student as s
where s.rollNo=e.rollNo and e.courseId=c.courseId and d.deptId=c.deptNo and
s.sex='male'

group by c.cname);

select distinct f.courseId , f.name , f.cname

from females as f , males as m

where f.Females=m.Males;

drop view males;


drop view females
First find the count of the number of males and number of females in each of the courses by
grouping them by courses and then condition that the males is equal to the number of
females.

You might also like