You are on page 1of 15

1. Find students who are enrolled in more courses than the average number of courses per student.

1) Count of courses each student has enrolled in.


SQL> select count(*) from enrollments group by studentid;

2) Average number of courses per student.


SQL> select avg(CourseCount) from (select count(*) as CourseCount from enrollments group by studentid);

3) Students who enrolled in more courses than the average number of courses per student.
SQL> select s.StudentID, s.FullName
2 from Students s
3 where (select count(*) from Enrollments e where e.StudentID = s.StudentID)
4 >
5 (select avg(CourseCount) from (select count(*) as CourseCount from Enrollments group by StudentID));
2. Retrieve the names of students who have scored higher than ANY student in a specific course.
1) Find details of courseID 106.
SQL> select * from enrollments where CourseID = 106;

2) Find all students who scored more than that.


SQL> select distinct s.FullName
2 from Students s, Enrollments e, Marks m
3 where s.StudentID = e.StudentID
4 and e.EnrollmentID = m.EnrollmentID
5 and m.Score > (SELECT m.Score
6 from Marks m, Enrollments e
7 where e.CourseID = 106 and m.EnrollmentID = e.EnrollmentID);

3. Identify courses which are not enrolled by any student (using IN and EXISTS).
1) All the courseid’s which some or the other student has enrolled in
SQL> select courseid from courses c
2 where exists
3 (select * from enrollments e
4 where e.courseid=c.courseid);

2) Find those courses that are not in the above list but are there in courses table.
SQL> select courseid from courses
2 where courseid not in
3 (select courseid from courses c
4 where exists
5 (select * from enrollments e
6 where e.courseid=c.courseid));

4. Select students who have scored above ALL the average marks of each course they are enrolled in.
1) Find average marks for every course.
SQL> select AVG(e2.Marks)
2 from Enrollments e2, Enrollments e
3 where e2.CourseID = e.CourseID
4 group by e2.CourseID;

2) Find average marks per course, along with courseid.


SQL> select AVG(e2.Marks),e2.courseid
2 from Enrollments e2, Enrollments e
3 where e2.CourseID = e.CourseID
4 group by e2.CourseID;

3) Join enrollments with students and find those whose marks are more than ALL average marks for each course.
SQL> select s.fullname,e.courseid,e.marks from students s
2 join Enrollments e on s.studentid=e.studentid
3 where e.marks > all(
4 select avg(e2.marks) from Enrollments e2 where
5 e2.courseid=e.courseid group by e2.courseid);

5. List course where more than 20% of enrolled students failed.


1) Courses in which students have failed, along with the number of students that failed in it:
SQL> select courseid,count(*) as totalfailed from enrollments where grade='F' group by courseid;

2) Courses in which students have failed, along with the total count of students enrolled in it:
SQL> select courseid,count(*) as totalstud from enrollments group by courseid
2 having courseid in
3 (select courseid from (select courseid,count(*) from enrollments
4 where grade='F' group by courseid));

3) Courses in which students have failed, along with the total count of students enrolled in it AND 20% of that total strength:
SQL> select courseid,totalstud,0.2*totalstud as perc20 from
2 (select courseid,count(*) as totalstud from enrollments group by courseid
3 having courseid in
4 (select courseid from (select courseid,count(*) from enrollments
5 where grade='F' group by courseid)));
4) Display those courses where table 1’s totalfailed > perc20 of totalstud in table 3:
SQL> select courseid,count(*) as totalfailed from enrollments where grade='F' group by courseid
2 having count(*)>(
3 select perc20 from
4 ( select courseid,totalstud,0.2*totalstud as perc20 from
5 (select courseid,count(*) as totalstud from enrollments group by courseid
6 having courseid in
7 (select courseid from (select courseid,count(*) from enrollments
8 where grade='F' group by courseid)))));

6. List courses that have ‘Tutorial’ course components.


SQL> select c.courseid,c.coursename from courses c
2 join coursecomponents cc on cc.courseid=c.courseid and
3 cc.componentname='Assignment';

7. Display the names of topper students in each course.


SQL> select e.courseid, s.fullname, max(e.marks) as HighestMarks
2 from enrollments e
3 join students s on e.studentid = s.studentid
4 group by e.courseid, s.fullname;
8. Find the student that scored highest in all the courses that he registered for.
1) Based on the query above, which is the table of all the courses and the students that scored highest in each, count the
total courses that those students scored highest in:
SQL> select fullname, count(*) as highcourses from (
2 select e.CourseID, s.FullName, MAX(e.Marks) as HighestMarks
3 from Enrollments e
4 join Students s on e.StudentID = s.StudentID
5 group by e.CourseID, s.FullName)
6 group by fullname;
2) Find the total number of courses each student has been enrolled in:
SQL> select s.fullname, count(*) as regCourses
2 from enrollments e
3 join students s on e.StudentID = s.StudentID
4 group by s.FullName;
3) Join the above 2 tables based on student name to compare the number of courses that they enrolled in and the number
of courses they scored the highest in:
SQL> select t1.fullname,t1.highcourses,t2.regcourses from
2 (select fullname, count(*) as highcourses from (
3 select e.CourseID, s.FullName, MAX(e.Marks) as HighestMarks
4 from Enrollments e
5 join Students s ON e.StudentID = s.StudentID
6 group by e.CourseID, s.FullName)
7 group by fullname) t1,
8 (select s.fullname, count(*) as regCourses
9 from enrollments e
10 join students s on e.StudentID = s.StudentID
11 group by s.FullName) t2
12 where t1.fullname=t2.fullname;
4) From the above table, find those students that have the same amount of courses they registered for and the amount of
courses they scored highest in:
9. Calculate total marks for a student in a course considering the weightage of each component and update in
Enrollment table.
1) Find each student and their new marks
SQL> select e.enrollmentid, marks*weightage/100 as updatedmarks
2 from enrollments e
3 join coursecomponents c
4 on e.courseid=c.courseid;

2) Update enrollments table with these new values (the values get rounded off and updated):
SQL> update enrollments e
2 set e.marks = (
3 select e.marks * c.weightage / 100
4 from coursecomponents c
5 where e.courseid = c.courseid
6 );

14 rows updated.
3) Similarly update values of marks table:
SQL> update marks m set m.score=(select e.marks from enrollments e where e.enrollmentid=m.enrollmentid);
10. Update Grade of each student for each course in Enrollment table based on Marks.

SQL> update enrollments


2 set grade =
3 case
4 when marks >= 9 then 'A+'
5 when marks >= 8 then 'A'
6 when marks >= 7 then 'B+'
7 when marks >= 6 then 'B'
8 when marks >= 5 then 'C+'
9 when marks >= 4 then 'C'
10 else 'F'
11 end;
COMMON LEARNINGS

1. Incorrect Grouping while using Aggregate functions:


Error: We can get incorrect data summaries upon using the wrong group by clauses.
Eg. avg(marks) group by student and avg(marks) group by courseid will give entirely different data summaries.
Solution: Always pair aggregate functions with the appropriate group by clauses to get accurate data summaries.
2. Subquery Mismanagement:
Error: Referencing outer query columns incorrectly in a sub query or referencing inner query columns incorrectly in outer
queries can give errors or wrong data.
Eg. different cardinalities of the columns being compared inside and outside parts of the query.
Solution: The scope of each subquery must be kept in mind clearly and correct references to inner or outer queries must
be made.
3. JOIN Operations:
Error: Joining on the wrong columns can give missing or duplicated data.
Eg. 2 different outputs can be obtained on joining marks table with enrollments table based on courseid or enrollmentid.
Solution: The tables should be joined on the correct criteria.

You might also like