query processing and data analysis
TRANSCRIPT
*Property of STI K0019
Retrieving Data from Several Tables
What is a Join? Types of Join
*Property of STI K0019
What is a Join?
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.
*Property of STI K0019
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:SELECT TABLE1.COLUMN1, TABLE2.COLUMN2...
FROM TABLE1, TABLE2 [, TABLE3 ]
WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN_NAME ]
*Property of STI K0019
Types of Joins
Equijoins or Inner Joins (cont.) Example:
SELECT DISTINCT name, size,
order_items.quantity
FROM product, order_items
WHERE product.id = order_items.prod_id
AND product.quantity = order_items.quantity
*Property of STI K0019
Types of Joins
Natural Joins The natural join removes duplicate columns in the joining
columns.
Syntax:SELECT TABLE1.*, TABLE2.COLUMN_NAME
[ TABLE3.COLUMN_NAME ]
FROM TABLE1, TABLE2 [ TABLE3 ]
WHERE TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME = TABLE3.COLUMN_NAME ]
*Property of STI K0019
Types of Joins
Natural Joins (cont.) Example:SELECT product.*,
order_items.quantity
FROM product, order_items
WHERE product.id = order_items.prod_id
*Property of STI K0019
Types of Joins
Non-equijoins It combines two or more tables based on a specified column
value not equaling a specified column value in another table.
Syntax:SELECT TABLE1.*, TABLE2.COLUMN_NAME
[ TABLE3.COLUMN_NAME ]
FROM TABLE1, TABLE2 [, TABLE3 ]
WHERE TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME
[ AND TABLE1.COLUMN_NAME != TABLE2.COLUMN_NAME ]
*Property of STI K0019
Types of Joins
Non-equijoins (cont.) Example:SELECT EMPLOYEE_TBL.EMP_ID, SALARY_TBL.DATE_HIRE
FROM EMPLOYEE_TBL,
SALARY_TBL
WHERE EMPLOYEE_TBL.EMP_ID != SALARY_TBL.EMP_ID;
*Property of STI K0019
Types of Joins
Outer Joins It produces all rows that exist in one table, even if there
are corresponding rows do not exist in the joined table.
The left outer join preserves every row in the left-hand table.
The right outer join preserves every row in the right-hand table.
A full outer join, preserves all rows from both tables.
*Property of STI K0019
Types of Joins
Outer Joins (cont.) Syntax:SELECT TABLE1.COLUMN_NAME, TABLE2.COLUMN_NAME
FROM TABLE1
{RIGHT | LEFT | FULL} [OUTER] JOIN
TABLE2 ON TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME
Example:SELECT EMPLOYEE_TBL.EMP_ID, SALARY_TBL.DATE_HIRE
FROM EMPLOYEE_TBL LEFT OUTER JOIN
SALARY_TBL ON EMPLOYEE_TBL.EMP_ID = SALARY_TBL.EMP_ID;
*Property of STI K0019
Types of Joins
Self Joins The self join combines tables to itself.
Syntax:SELECT A.COLUMN_NAME, B.COLUMN_NAME, [ C.COLUMN_NAME ]
FROM TABLE1 A, TABLE2 B [, TABLE3 C ]
WHERE A.COLUMN_NAME = B.COLUMN_NAME
[ AND A.COLUMN_NAME = C.COLUMN_NAME ]
*Property of STI K0019
Types of Joins
Self Joins (cont.)
Example:SELECT A.LAST_NAME, B.LAST_NAME, A.FIRST_NAME
FROM EMPLOYEE_TBL A,
EMPLOYEE_TBL B
WHERE A.LAST_NAME = B.LAST_NAME;
*Property of STI K0019
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