The different types of SQL joins, including inner join, left outer join, right outer join, and full outer join. It also provides a relational schema for practice problems and a list of queries to write using joins. examples of each join type and explains how to identify the appropriate join type for each query. Resources for further learning are also provided.
Download SQL Join Types and Practice Problems and more Slides Database Management Systems (DBMS) in PDF only on Docsity!
SQL Join
Outer Join
Recap Cross Join &Inner Join
Inner join
Inner join creates a new result table by taking
Cartesian product
filter (join predicate)
An inner join in SQL returns rows where there is at least one match on both tables.
Example query of inner join
SELECT *
FROM employee
INNER JOIN department
ON employee.DepartmentID = department.DepartmentID;
Left Outer join
In an left outer join, all rows from the first table mentioned in the SQL query is selected, regardless whether there is a matching row on the second table mentioned in the SQL query.
Example Query Left Outer Join
SELECT * FROM employee LEFT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID;
Right Outer Join Example Query
SELECT * FROM employee RIGHT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID;
Full Outer Join
This join combines left outer join and right outer join. It returns row from either table when the conditions are met and returns null value when there is no match.
Class Activity
Find out the sales by stores. Output should have the results for all stores.
Your output should like:
Join Example Query
We want to find out sales by store, and we want to see the results for all stores regardless whether there is a sale in the Store_Information table.
SELECT A1.store_name STORE, SUM(A2.Sales) SALESFROM Geography ALEFT OUTER JOIN Store_Information AON A1.store_name = A2.store_nameGROUP BY A1.store_name
Practice Problems: Identify the
appropriate type of join for each query
Write the following Queries using joins:
List the names of the employees who earn more than any employee in marketing department.
Find the names of suppliers that deliver computers.
List the names of all departments with all suppliers, display all suppliers with all department names regardless whether they supplied any item or not.
List the supplier names who do not deliver any item.
List the names of employees who earn more than average salary of employees in the accounting department. List the Item details that is never delivered.