Vous êtes sur la page 1sur 4

What is a Join?

RetrievingDatafrom
SeveralTables

A join is a collection of data from multiple tables.

It combines two or more tables to retrieve data from


multiple tables.

The primary keys and foreign keys are used to join related
tables to one another.

What is a Join?
Types of Join

*PropertyofSTI

K0019

1 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Types of Joins

*PropertyofSTI

K0019

2__________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Types of Joins

Equijoins or Inner Joins

The equijoin or inner join combines two tables with a


common column which is usually the primary key.

Syntax:

Equijoins or Inner Joins (cont.)

Example:

SELECT DISTINCT name, size,


order_items.quantity

SELECT TABLE1.COLUMN1, TABLE2.COLUMN2...


FROM product, order_items
FROM TABLE1, TABLE2 [, TABLE3 ]
WHERE product.id = order_items.prod_id
WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
AND product.quantity = order_items.quantity
[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN_NAME ]

*PropertyofSTI

K0019

3 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*PropertyofSTI

K0019

4__________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Types of Joins

Types of Joins

Natural Joins

Natural Joins (cont.)

The natural join removes duplicate columns in the joining


columns.

Example:

SELECT product.*,
order_items.quantity

Syntax:

FROM product, order_items

SELECT TABLE1.*, TABLE2.COLUMN_NAME


WHERE product.id = order_items.prod_id
[ TABLE3.COLUMN_NAME ]
FROM TABLE1, TABLE2 [ TABLE3 ]
WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN_NAME ]
*PropertyofSTI

K0019

5 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Types of Joins

K0019

6__________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Types of Joins

Non-equijoins

*PropertyofSTI

Non-equijoins (cont.)

It combines two or more tables based on a specified column


value not equaling a specified column value in another table.

Example:

SELECT EMPLOYEE_TBL.EMP_ID,
SALARY_TBL.DATE_HIRE

Syntax:

FROM EMPLOYEE_TBL,

SELECT TABLE1.*, TABLE2.COLUMN_NAME

SALARY_TBL

[ TABLE3.COLUMN_NAME ]

WHERE EMPLOYEE_TBL.EMP_ID != SALARY_TBL.EMP_ID;

FROM TABLE1, TABLE2 [, TABLE3 ]


WHERE TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME ]

*PropertyofSTI

K0019

7 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*PropertyofSTI

K0019

8__________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Types of Joins

Types of Joins

Outer Joins

Outer Joins (cont.)

It produces all rows that exist in one table, even if there are
corresponding rows do not exist in the joined table.

Syntax:

SELECT TABLE1.COLUMN_NAME, TABLE2.COLUMN_NAME


FROM TABLE1

{RIGHT | LEFT | FULL} [OUTER] JOIN

The left outer join preserves every row in the left-hand table.

TABLE2 ON TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME

The right outer join preserves every row in the right-hand


table.

Example:

SELECT EMPLOYEE_TBL.EMP_ID, SALARY_TBL.DATE_HIRE


FROM EMPLOYEE_TBL LEFT OUTER JOIN

A full outer join, preserves all rows from both tables.

*PropertyofSTI

K0019

SALARY_TBL ON EMPLOYEE_TBL.EMP_ID = SALARY_TBL.EMP_ID;

9 _________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Types of Joins

*PropertyofSTI

K0019

10 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
Types of Joins

Self Joins

The self join combines tables to itself.

Syntax:

Self Joins (cont.)

Example:

SELECT A.LAST_NAME, B.LAST_NAME, A.FIRST_NAME

SELECT A.COLUMN_NAME, B.COLUMN_NAME,


[ C.COLUMN_NAME ]

FROM EMPLOYEE_TBL A,
EMPLOYEE_TBL B

FROM TABLE1 A, TABLE2 B [, TABLE3 C ]

WHERE A.LAST_NAME = B.LAST_NAME;

WHERE A.COLUMN_NAME = B.COLUMN_NAME


[ AND A.COLUMN_NAME = C.COLUMN_NAME ]

*PropertyofSTI

K0019

11 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

*PropertyofSTI

K0019

12 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Types of Joins

Cross Joins

It returns all possible combinations of rows from the two


tables.

Syntax:

SELECT *
FROM TABLE1 CROSS JOIN TABLE2

Example:

SELECT *
FROM ORDER CROSS JOIN PURCHASE
*PropertyofSTI

K0019

13 ________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________
___________________________

Vous aimerez peut-être aussi