Lec08 - SQL JOIN
Lec08 - SQL JOIN
Lecture 8
1
SQL JOIN
• JOINs are used to combine rows from two or more tables based
on a related column between them, usually a foreign key in one
table referencing a primary key in another.
• To retrieve related data from multiple tables in a single query.
• SQL JOIN syntax:
• SELECT columns_from_both_tables
FROM table1 interchangeable
JOIN table2
ON table1.column1 = table2.column2
joined with
interchangable
2
SQL JOIN
• Example: join the Customers and
Orders tables based on the
common values of their if tables have cols wz same name
customer_id columns
• SELECT Customers.customer_id,
Customers.first_name,
Orders.amount steps:
first complier go to foriegn table
FROM Customers if record exist in primary table take if not next record
JOIN Orders
ON Customers.customer_id =
Orders.customer_id;
3
SQL JOIN With AS Alias
• We can use AS aliases inside RIGHT JOIN to make our SQL
code short and clean.
• Example:
• SELECT C.customer_id, C.first_name, O.amount
FROM Customers As C JOIN Orders as O
ON C.customer_id = O.customer;
4
JOIN With WHERE Clause
• We can use the LEFT JOIN statement with an optional
WHERE clause.
• Example:
• SELECT Customers.customer_id, Customers.first_name,
Orders.amount
FROM Customers JOIN Orders
ON Customers.customer_id = Orders.customer
WHERE Orders.amount >= 500;
5
JOIN Multiple Tables bridge msalan
6
Types of JOINs in SQL
1. INNER JOIN
2. RIGHT OUTER JOIN
3. LEFT OUTER JOIN
4. FULL OUTER JOIN
5. CROSS JOIN
6. SELF JOIN
7
SQL INNER JOIN same as join
8
SQL INNER JOIN
• Returns only the rows where
there is a match in both tables.
• Example: join Customers and
Orders tables with their
matching fields customer_id.
• SELECT
Customers.customer_id,
Customers.first_name,
Orders.amount FROM
Customers INNER JOIN Orders
ON Customers.customer_id =
Orders.customer;
9
• Employee
SQL INNER JOIN
• SELECT Employees.Name,
Departments.DeptName
FROM Employees INNER JOIN
Departments ON
Employees.DeptID=Departments.DeptID; • Department
10
SQL LEFT JOIN
• The SQL LEFT JOIN combines two tables based on a
common column.
• It then selects records having matching values in these
columns and the remaining rows from the left table.
• It returns all attributes in left table even they don’t have a
match in the other table
• SQL LEFT JOIN Syntax: if fk is null the value will be null
if fk have value not in 2nd table also null
• SELECT columns_from_both_tables
FROM table1 LEFT JOIN table2
ON table1.column1 = table2.column2
11
• Employee
SQL LEFT JOIN
• Return all employee even if they are not
working in a department.
• SELECT Employees.Name,
Departments.DeptName
FROM Employees Left JOIN
Departments ON • Department
Employees.DeptID=Departments.DeptID;
12
SQL LEFT JOIN
• Example: left join the Customers
and Orders tables.
• SELECT Customers.customer_id,
Customers.first_name,
Orders.amount
FROM Customers
LEFT JOIN Orders
ON Customers.customer_id =
Orders.customer;
13
SQL RIGHT JOIN
• The SQL RIGHT JOIN statement joins two tables based on a
common column. It selects records that have matching values
in these columns and the remaining rows from the right table.
• SQL RIGHT JOIN Syntax:
• SELECT columns_from_both_tables
FROM table1 RIGHT JOIN table2
ON table1.column1 = table2.column2
14
• Employee
SQL RIGHT JOIN
departments
• Return all employee even if they are not
if there arent any emplyees in it
working in a department.
• SELECT Employees.Name,
Departments.DeptName
FROM Employees RIGHT JOIN
Departments ON • Department
Employees.DeptID=Departments.DeptID;
15
SQL RIGHT JOIN
• Example: join Customers and
Orders tables based on customer_id
of Customers and customer of
Orders
• SELECT Customers.customer_id,
Customers.first_name, Orders.amount
FROM Customers RIGHT JOIN Orders
ON Customers.customer_id =
Orders.customer;
16
SQL FULL OUTER JOIN
• The SQL FULL OUTER JOIN statement joins two tables
based on a common column.
• It selects records that have matching values in these
columns and the remaining rows from both of the tables.
• FULL OUTER JOIN syntax:
• SELECT columns
FROM table1 FULL OUTER JOIN table2
ON table1.column1 = table2.column2;
17
• Employee
SQL FULL OUTER JOIN
• Return all employee even if they are not
working in a department.
• SELECT Employees.Name,
Departments.DeptName FROM
Employees FULL OUTER JOIN
Departments ON Employees.DeptID =
Departments.DeptID; • Department
18
• Employee
SQL SELF JOIN
• Used with recursive relations
• Suppose an Employee table
contains a ManagerID column
referencing another EmployeeID.
• If we want to list the employees'
names and their managers names. • Result:
• SELECT E.Name AS Employee,
M.Name AS Manager FROM
Employees E LEFT JOIN Employees
M ON E.ManagerID = M.EmployeeID;
19
• Employee
SQL CROSS JOIN
• Get all combinations of employees and
departments.
• SELECT Employees.Name,
Departments.DeptName FROM Employees
CROSS JOIN Departments;
• Department
21
Questions
1. List customers, the products they bought, and the order date?
22
• SELECT Customers.Name AS Customer, Products.ProductName,
Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID
INNER JOIN Products ON Orders.ProductID = Products.ProductID;
23
Questions
2. Show all orders and include customer names (even if the order
has no customer).
24
• SELECT Orders.OrderID, Customers.Name AS Customer,
Orders.OrderDate FROM Orders
LEFT JOIN Customers
ON Orders.CustomerID = Customers.CustomerID;
25
Questions
3. Show all products and the order details (even if a product hasn't
been ordered).
26
• SELECT Products.ProductName, Orders.OrderID, Orders.OrderDate
FROM Orders RIGHT JOIN Products
ON Orders.ProductID = Products.ProductID;
27
Questions
4. List all customers even they didn’t made orders and list the
product they ordered.
28
• SELECT Customers.Name AS Customer, Products.ProductName,
Orders.OrderDate FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
LEFT JOIN Products ON Orders.ProductID = Products.ProductID;
29
JOIN vs. Nested Queries (Subqueries)
• Get names of customers who have
placed orders.?
• JOIN: SELECT Customers.Name FROM
Customers INNER JOIN Orders ON
Customers.CustomerID =
Orders.CustomerID;
• Nested query: SELECT Name FROM
Customers WHERE CustomerID IN (
SELECT CustomerID FROM Orders);
30