Docsity

SQL Join Types and Practice Problems, Slides of Database Management Systems (DBMS)

Partial preview of the text

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) SALES FROM Geography A LEFT OUTER JOIN Store_Information A ON A1.store_name = A2.store_name GROUP BY A1.store_name

Practice Problems: Identify the

appropriate type of join for each query

  • Write the following Queries using joins:
  1. List the names of the employees who earn more than any employee in marketing department.
  2. Find the names of suppliers that deliver computers.
  3. List the names of all departments with all suppliers, display all suppliers with all department names regardless whether they supplied any item or not.
  4. List the supplier names who do not deliver any item.
  5. 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.

Resources

  • www.w3schools.com
  • www.1keydata.com