Sie sind auf Seite 1von 5

Joins

Key Areas
What are Joins
Types of Joins
Examples of Joins
The Basics -- What Are Joins?
A concept in relational database theory
Technique for accessing 2+ tables in a single answer set
Each answer set row may contain data from each table
Joined on join column
Types Of Joins
We will discuss the following Joins:
Inner Join
Outer Joins
Full
Left
Right
Cross Join
Self Join
Inner Join: To retrieve matched records we can use comparison operators (i.e. =, < >).Whenever
we need to get the matched records from two tables, this type of Join is used.
Inner Join Also call as Natural Join or Equi Join.
Syntax:
select e.emp_no,e.emp_name,e.dept_no,d.dept_no,d.dept_name from prashanth.employee e
inner join dept d on e.dept_no = d.dept_no;
select e.emp_no,e.emp_name,e.dept_no, d.dept_no,d.dept_name from prashanth.employee e
inner join dept d on e.dept_no <> d.dept_no;
Outer Join : To retrieve matched and unmatched records from two tables we can use Outer Join.
Unmatched rows will be displayed as NULL from both tables.
Outer Join can be classified as: Left Outer Join , Right Outer Join and Full Outer Join.
Full Outer Join: All matched and unmatched records from both left table and right table will be
displayed and unmatched records will be displayed as NULL from both the tables.
Syntax:
select e.emp_no,e.emp_name,e.dept_no,d.dept_no,d.dept_name from prashanth.employee e full
join dept d on e.dept_no = d.dept_no;


Left Outer Join: All matched and unmatched records from left table will be displayed
and unmatched records will be displayed as NULL from left table. But from right table only
matched records will be displayed as it is and unmatched records will not be displayed.
Syntax:
select e.emp_no,e.emp_name,e.dept_no,d.dept_no,d.dept_name from prashanth.employee e left
join dept d on e.dept_no = d.dept_no;
Right Outer Join: All Records from right table will be displayed either matched or
unmatched and unmatched records will be displayed as NULL from right table. But from
left table only matched records will be displayed as it is and unmatched records will not be
displayed.
Syntax:
select e.emp_no,e.emp_name,e.dept_no,d.dept_no,d.dept_name from prashanth.employee e
right join dept d on e.dept_no = d.dept_no;
Cross Join: This Join returns all rows from the left table, each and every row from the left table is in
combination with all rows from the right table. Cross join also called as Cartesian Product Join.
Syntax:
select e.emp_no,e.emp_name,e.dept_no from prashanth.employee e cross join dept d;
Self Join: A table can be joined to itself is a self-join.
select e.emp_no,e.emp_name,e.dept_no,e.sal,e.city,c.dept_no from prashanth.employee e join
employee c on e.dept_no = c.dept_no where e.city = 'Hyd';
Alias Names
Temporary name for Table or View
Defined in From clause
Useful as abbreviation
Required in Self Join
Once defined, must be used in SQL where table name required
Multiple Table Joins
A join can have up to 64 participating tables
An n-table join requires n-1 join conditions
Omitting a join condition will result in a Cartesian Product Join

Das könnte Ihnen auch gefallen