DBMSP7
DBMSP7
2. Perform a LEFT JOIN to find courses that have never been taken by any student.
SQL> select c.courseid, c.coursename from courses c
2 left join enrollments e on c.courseid=e.courseid
3 where enrollmentid is null;
Same as:
SQL> select distinct courseid from courses where courseid not in (select distinct courseid from enrollments);
3. Utilize a RIGHT JOIN to list all courses and any associated student details.
SQL> select c.courseid, c.coursename, s.studentid, s.fullname
2 from courses c right join enrollments e
3 on c.courseid=e.courseid
4 inner join students s
5 on s.studentid=e.studentid;
SOLUTION: while displaying the student columns, “ use null as dep, null as professorid ” to make the values in those
attributes as null for all students, similarly, do for professors using “ null as studentid “
SQL> select studentid as studid, fullname as name, null as dep, null as professorid
2 from students
3 union
4 select null as studentid, name as name, department as dep, professorid as profid
5 from professors;
5. Use INTERSECT to find common courses between two specific students.
ERRR: intersect keyword not working
SOLUTION: solve using inner join
SQL> select e1.courseid,e1.studentid,e2.studentid
2 from enrollments e1
3 inner join enrollments e2
4 on e1.courseid=e2.courseid
5 where e1.studentid='22BCE101' and e2.studentid='22BCE102';
6. List all courses that are offered but not enrolled in by a particular student..
SQL> select c.courseid, c.coursename from courses c
2 left join enrollments e on c.courseid=e.courseid and studentid='22BCE191'
3 where enrollmentid is nulL;
LOGICAL ERROR: the condition for studentid=’22BCE191’ has to be put on the join.
SQL> select c.courseid, c.coursename from courses c
2 left join enrollments e on c.courseid=e.courseid
3 where enrollmentid is null and studentid='22BCE191';
SOLUTION: the condition for studentid=’22BCE191’ has to be put on the enrollment table and not the join.
7. Find Highest marks in each course.
SQL> select max(marks) from (select courseid,marks from enrollments group by courseid,marks);
8. Generate Student progress report based on marks obtained in different components of each course
SQL> select s.studentid, s.fullname, c.courseid, c.componentname, e.marks, e.grade
2 from students s
3 inner join enrollments e on s.studentid=e.studentid
4 inner join coursecomponents c on e.courseid=c.courseid;
MAIN LEARNINGS:
Learnt how to user inner join, outer join, left join, right join, union, intersect for two or more tables.
Syntax: select <table_alias.columns_to_be_selected> from <table1> <join_to_perform> <table2> on
<joining_criteria_for_table1_and_table2> [where <additional_condition>];