Docsity

SQL Join Extracting Data From Multiple Tables, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download SQL Join Extracting Data From Multiple Tables and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

SQL Join

Extracting Data From Multiple Tables

Why we need joins?

  • Write a query to display the employee names along with their department names.
  • Output should be like:
  • Department name Employee name

Multiple tables in From Clause

  • Syntax
  • Select Column-names
  • From Table-1, Table- 2
  • Where ( condition +Sub query)
  • Group by
  • Having ( condition +Sub Query)
  • Order by

Cross Join

  • A cross join (also called a Cartesian join) is a join of tables without specifying the join condition.
  • In this scenario, the query would return all possible combination of the tables in the SQL query.
  • CROSS JOIN returns the Cartesian product of rows from tables in the join.
  • In other words, it will produce rows which combine each row from the first table with each row from the second table.

Cartesian Product Example

• A = {1,2}; B = {3,4}

• A × B = {1,2} × {3,4} = {(1,3), (1,4), (2,3), (2,4)}

Example query of cross join

Cartesian Product of two tables

Multiple Tables in From Clause with where Condition

  • Select *
  • From Employee, Department
  • Where Employee.DepartmentID = Department.DepartmentID

Why we need joins?

  • Write a query to display the employee names along with their department names.
  • Output should be like:
  • Department name Employee name Select d.DepartmentName, E. LastName From Employee AS e, Department AS d Where d.DepartmentID = e.Department ID DepartmentName EmployeeName Sales Rafferty Engineering Jones Engineering Steinberg Clerical Robinson Clerical Smith Output

SQL Joins

  • In SQL joins are used to get data from two or more tables based on relationship between some of the columns in tables.
  • Several different types of JOINs can be performed in SQL. The key ones are as follows:
  • Cross join
  • Inner join
  • Outer join(left, right, full)
  • Self Join

Join Syntax

  • Select
  • From Table 1
  • Join Table 2
  • On ( condition based on matching column)
  • Where (condition + sub Query)
  • Group by
  • Having ( Condition + Sub Query)
  • Order by
  • SELECT *
  • FROM employee
  • CROSS JOIN department; Select * From employee, department

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.

Why we need joins?

  • Write a query to display the employee names along with their department names.
  • Output should be like:
  • Department name Employee name Select D.DepartmentName, E. LastName From Employee AS E Inner Join Department AS D On D.DepartmentID = E.Department ID DepartmentName EmployeeName Sales Rafferty Engineering Jones Engineering Steinberg Clerical Robinson Clerical Smith Output

Multi table join

  • Use to join more than two tables. Processing starts from left to right.
  • Select *
  • FROM Table1 AS T
  • INNER JOIN Table2 AS T
  • ON T1.col1 = T2.col
  • INNER JOIN Table3 AS T
  • ON T2.col1 = T3.col Select * From Table1 As T1, Table 2 As T2, Table3 As T Where T1.col1 = T2.col1 AND T2.Col1 = T3.Col