Working with Joins
SQL Joins
Join Types
Inner join, Left Join, Right Join Amit Dua
Other Joins
Updating, Deleting,
Use Cases and Best Practices
Introduction to SQL Joins
SQL joins are a powerful feature that allow you to
combine data from multiple tables based on a
common column.
This makes it easy to retrieve related information
and build more complex queries.
Faculty
Source: https://www.freepik.com/free-photo/man-giving-business-presentation-using-high-technology-digital pen_15665031.htm#query=business%20growth&position=10&from_view=search
Types of SQL Joins
Inner Join
Returns only the rows that have matching values in both tables.
Left Join
Returns all rows from the left table, plus the matching rows from the right table.
Faculty
Right Join
Returns all rows from the right table, plus the matching rows from the left table.
Source: https://www.freepik.com/free-photo/man-giving-business-presentation-using-high-technology-digital pen_15665031.htm#query=business%20growth&position=10&from_view=search
Exercise 1: Inner Join
Product(ProductID, ProductName, CategoryID, Price)
Category(CategoryID, CategoryName, Description)
SELECT ProductID, ProductName, CategoryName
FROM Products
INNER JOIN Categories ON Products.CategoryID =
Categories.CategoryID;
Faculty
Source: Font: Arial, Font Size: 8 pts
Exercise 2: Left Join
Customers(CustomerID, CustomerName, ContactName, Address,
City, PostalCode, Country)
Orders(OrderID, CustomerID, EmployeeID, OrderDate, ShipperID)
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
Faculty
Source: Font: Arial, Font Size: 8 pts
Exercise 3: Right Join
Orders(OrderID, CustomerID, EmployeeID, OrderDate,
ShipperID)
Employees(EmployeeID, LastName, FirstName, BirthDate,
Photo)
SELECT Orders.OrderID, Employees.LastName,
Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID =
Employees.EmployeeID
ORDER BY Orders.OrderID;
Faculty
Source: Font: Arial, Font Size: 8 pts
Other Joins
SQL FULL OUTER JOIN
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or
right (table2) table records.
SQL Self Join
A self join is a regular join, but the table is joined with itself.
Faculty
Source: Font: Arial, Font Size: 8 pts
Other Joins
SQL FULL OUTER JOIN
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or
right (table2) table records.
SQL Self Join
A self join is a regular join, but the table is joined with itself.
Faculty
Source: Font: Arial, Font Size: 8 pts
Use Cases and Best Practices
Consolidate Data
Joins allow you to combine information from multiple sources into a single
query result.
Improve Reporting
By linking related data, joins enable more robust and insightful reports.
Optimize Performance
Careful use of join types can significantly improve the speed of your Faculty
queries.
Enforce Data Integrity
Joins help ensure data consistency by only returning matching records.
Source: Font: Arial, Font Size: 8 pts
Thank You!