Lecture On SQL Joining 2
Lecture On SQL Joining 2
EMPLOYEES DEPARTMENTS
…
What is the Join?
•Use a join to query data from more than one table
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1=table2.column2
NATURAL JOIN
• (retrieve data from two tables by the same column)
Cartesian product:
20 x 8 = 160 rows
…
Creating Cross Joins
…
Using Cross Joins
Try:
Write SQL statement to do the following relation:
DEPARTMENT LOCATION
Solution:
Select *
From department CROSS JOIN location
Generally:
CROSS PRODUCT is not meaningful operation
Retrieving Record with Equijoin
Employees ∞ Department
EMPLOYEES DEPARTMENTS
Select *
From employees ,departments
Where employees.department_id=departments.department_id
Creating Natural Joins
…
Qualifying Ambiguous
Column Names
…
Self-Joins Using the ON Clause
… …
…
Joining More than two table
Employees Departments Locations
Joining More than two table
Solution:
select first_name,department_name,city
from employees JOIN departments using(department_id)
JOIN locations using(location_id)
OR:
select first_name,department_name,city
from employees
JOIN departments ON(employees.department_id=departments.department_id)
JOIN locations ON(departments.location_id=locations.location_id)
OR:
select first_name,department_name,city
from employees E,departments D,locations L
where E.department_id=D.department_id
and D.location_id=L.location_id
Applying Additional Conditions
to a Join
EMPLOYEES JOB_GRADES
…
Summary