You are on page 1of 14

SQL

JOINS IN SQL
DAY 5 Sathish Chandran
JOINS

TO RETRIEVE DATA FROM


MORE THAN ONE TABLE
2 Steps

List the tables in FROM


List the join condition in WHERE
EQUI JOIN

Join two tables for


equality values ( in join condition )
For eg;
WHERE e.job_id = j.job_id
NON EQUI JOIN

Join two tables for


Non-equality values (in join condition)
For eg;
WHERE e.job_id <> j.job_id
INNER JOIN
Join two tables to get matched data
between tables using join condition

HR.EMPLOYEES E JOIN HR.JOBS J


ON E.JOB_ID = J.JOB_ID
RIGHT OUTER JOIN
To get matched data +
unmatched data from right side table
HR.EMPLOYEES E RIGHT JOIN HR.JOBS J
ON E.JOB_ID = J.JOB_ID
LEFT OUTER JOIN
To get matched data +
unmatched data from left side table
HR.EMPLOYEES E LEFT JOIN HR.JOBS J
ON E.JOB_ID = J.JOB_ID
FULL OUTER JOIN
To get matched data +
unmatched data from
BOTH side table
HR.EMPLOYEES E FULL JOIN HR.JOBS J
ON E.JOB_ID = J.JOB_ID
SELF JOIN
Table join by itself
SELECT E.FIRST_NAME EMPLOYEE_NAME,
E1.FIRST_NAME MANAGER_NAME
FROM HR.EMPLOYEES E, HR.EMPLOYEES E1
WHERE E.MANAGER_ID = E1.EMPLOYEE_ID
NATURAL JOIN

No join condition required


JOIN by common column names

HR.EMPLOYEES E NATURAL JOIN HR.JOBS J


CROSS JOIN
No join condition required
Multiplies records
Catesian product

HR.EMPLOYEES E CROSS JOIN HR.JOBS J


Points to Remember
• For Cross and Natural Joins; join conditions are not required
• To join tables; join condition column names need not to be same,
but data type should be same. Logically the same.
• Without join condition; leads to cross join.
Column ambiguous error will come; if we don’t have alias name
for join columns ( same column names in joining tables )
So always use alias names.
• Columns from joining tables can be used in SELECT & WHERE clauses
All the tables used in Joins; must be connected with Join conditions

You might also like