Database Systems Lab 8 Presentation

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

LAB 8: SQL OUTER JOINS

AND SELF JOIN


• Objective: Dive deeper into the intricacies of SQL joins, focusing
on outer joins and understanding the concept of self joins.
SQL OUTER JOINS
• An OUTER JOIN keyword fetches the records when there is a
match in one of the tables. Therefore, it returns all the rows when
there is a match in one of the tables.
EXAMPLE: OUTER JOIN
• SELECT Customers.CustomerName, Orders.OrderID
• FROM Customers
• OUTER JOIN Orders ON Customers.CustomerID =
Orders.CustomerID;
SQL LEFT OUTER JOIN
• The LEFT OUTER JOIN keyword returns all the rows from the
left table, and the matching rows from the right table.
EXAMPLE: LEFT OUTER
JOIN
• SELECT Students.Name, Courses.CourseName
• FROM Students
• LEFT OUTER JOIN Courses ON Students.CourseID =
Courses.ID;
EXERCISE: LEFT OUTER
JOIN
• Retrieve all students and their enrolled courses, including students
who haven't enrolled in any course.
SQL RIGHT OUTER JOIN
• The RIGHT OUTER JOIN keyword returns all the rows from the
right table, and the matching rows from the left table.
EXAMPLE: RIGHT OUTER
JOIN
• SELECT Orders.OrderID, Customers.CustomerName
• FROM Orders
• RIGHT OUTER JOIN Customers ON Orders.CustomerID =
Customers.CustomerID;
EXERCISE: RIGHT OUTER
JOIN
• List all products and their orders, including products that haven't
been ordered yet.
SQL FULL OUTER JOIN
• The FULL OUTER JOIN keyword returns all the rows when there
is a match in one of the tables. It returns all the rows from both
tables.
EXAMPLE: FULL OUTER
JOIN
• SELECT Students.Name, Courses.CourseName
• FROM Students
• FULL OUTER JOIN Courses ON Students.CourseID =
Courses.ID;
EXERCISE: FULL OUTER
JOIN
• Retrieve a list of all students and all courses, including students
without courses and courses without students.
SQL SELF JOIN
• A self join is a join in which a table is joined with itself. It is used
to compare rows within the same table.
EXAMPLE: SELF JOIN
• SELECT A.EmployeeName, B.EmployeeName AS
'ManagerName'
• FROM Employees A, Employees B
• WHERE A.ManagerID = B.EmployeeID;
EXERCISE: SELF JOIN
• Given a table 'Employees' with an 'EmployeeID', 'EmployeeName',
and 'ManagerID', retrieve a list of employees and their respective
managers.
MASTERING SQL JOINS
• A deep understanding of SQL joins, including outer joins and self
joins, is essential for complex data retrieval and analysis scenarios.
FEEDBACK AND
QUESTIONS
• Students are encouraged to provide feedback and ask questions at
the end of the lab.

You might also like