Advance SQL For Processing Multiple Tables
Advance SQL For Processing Multiple Tables
1. INNER JOIN
An INNER JOIN returns only the rows where there is a match in both tables based on the
specified condition.
2. FULL JOIN
A FULL JOIN returns all records when there is a match in either the left or right table. If
there’s no match, it returns NULL for columns from the table with no match.
4. LEFT JOIN
A LEFT JOIN returns all records from the left table (in this case, Students), and the
matched records from the right table (Courses). If there’s no match, NULL is returned for
columns from the right table.
6. RIGHT JOIN
A RIGHT JOIN returns all records from the right table, and the matched records from the
left table. If there’s no match, NULL is returned for columns from the left table.
Laboratory Exercises:
Create a database with two tables.
Name table 1 as Courses with columns: course_id (interger, primary key), course_name
In Courses table add 6 courses BSIT, BEED, BSED, BSBA-HRM, BSBA-MM, BSOA
Name table 2 as Students wth columns: student_id (integer, primary key), student_name,
course_id (integer)
In the Students table add 20 students: 4 students without course_id, 4 students each
BSIT, BEED, BSOA, BSED.
Perform:
1. Inner Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
INNER JOIN Courses
ON Students.course_id = Courses.course_id;
2. Full Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
FULL JOIN Courses
ON Students.course_id = Courses.course_id;
4. Left Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
LEFT JOIN Courses
ON Students.course_id = Courses.course_id;
6. Right Join
SELECT Students.student_id, Students.student_name, Courses.course_id, Courses.course_name
FROM Students
RIGHT JOIN Courses
ON Students.course_id = Courses.course_id;
Results:
1. You’ll see a list of students who are enrolled in a course, along with the course details.
2. You’ll see a list of all records where there is a match in either Students or Courses, and
also includes unmatched rows with NULLs in the columns that don’t have a match
3. You’ll see students without a course or courses without any students.
4. You’ll see a list of all students, with their course details if available. For students without
a course, the course_id and course_name fields will be NULL.
5. You’ll see a list of students who are not enrolled in any course.
6. You’ll see a list of all courses, with student details if available. Courses without any
students will have NULL values in the student_id and student_name fields.
7. You’ll see a list of courses that do not have any students enrolled.