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

DBMSP7

The document provides examples of using different types of SQL joins like inner join, left join, right join, union and intersect to combine data from multiple tables and solve different business problems. It also includes some common errors and their solutions related to using joins.

Uploaded by

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

DBMSP7

The document provides examples of using different types of SQL joins like inner join, left join, right join, union and intersect to combine data from multiple tables and solve different business problems. It also includes some common errors and their solutions related to using joins.

Uploaded by

mitaleeextra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Use an INNER JOIN to list all students along with the courses they are enrolled in.

SQL> select s.studentid, s.fullname, c.coursename, c.courseid


2 from students s
3 inner join enrollments e on s.studentid=e.studentid
4 inner join courses c on e.courseid=c.courseid;

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;

Alternative for all details:


SQL> select c.*
2 , s.*
3 from courses c right join enrollments e
4 on c.courseid=e.courseid
5 left join students s
6 on s.studentid=e.studentid;
4. Combine data from Students and Professors using UNION to create a comprehensive list of all individuals.
ERROR: professorid and studentid had different data types, int and char respectively, therefore union of id’s was not
possible.
SQL> select studentid as id, fullname as name
2 from students
3 union
4 select professorid as id, name as name
5 from professors;
select studentid as id, fullname as name
*
ERROR at line 1:
ORA-01790: expression must have same datatype as corresponding expression

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>];

You might also like