08 Joins
08 Joins
I WANT TO BE
AN ASTRONAUT
Subha Sandeep Samal
🛠 Oracle SQL Joins – A Comprehensive
Guide with Theory and Examples
Introduction to Joins in Oracle SQL
In SQL, a JOIN is used to combine rows from two or more tables based on a related
column between them. It's like forming friendships between tables!
The INNER JOIN returns only the matching records from both tables, discarding non-
matching rows.
Example
FROM employees e
Fun Fact:
INNER JOIN is the most commonly used join in SQL! It's like a club entry with a
strict guest list! 🎟
The LEFT JOIN returns all records from the left table and matching records from the
right table. If no match is found, NULL values are returned.
Example
FROM employees e
ON e.department_id = d.department_id;
Explanation: This query returns all employees and their department names. If an
employee is not assigned to a department, NULL is displayed.
Fun Fact:
If you're looking for employees without a department, you can filter with WHERE
d.department_name IS NULL;
The RIGHT JOIN returns all records from the right table and matching records from
the left table. If no match is found, NULL values are returned.
Example
FROM employees e
ON e.department_id = d.department_id;
The FULL JOIN returns all records from both tables. If there is no match, NULL
values are assigned.
Example
FROM employees e
ON e.department_id = d.department_id;
Explanation: This query retrieves all employees and departments, including those
without a match.
Fun Fact:
Some databases (like MySQL) don’t support FULL OUTER JOIN directly! You can
simulate it using LEFT JOIN + UNION + RIGHT JOIN!
The CROSS JOIN returns the Cartesian product of the two tables, meaning every row
in the first table is paired with every row in the second table.
Example
FROM employees e
Fun Fact:
Be careful with CROSS JOIN! If you have 1️0 employees and 1️0 departments, you'll
get 1️00 rows!
Example
ON e1️.manager_id = e2️.employee_id;
Explanation: This finds employees and their managers by joining the employees
table with itself.
Fun Fact:
SELF JOIN is often used in hierarchical data structures like organization charts or
family trees!
Example
FROM employees e
Fun Fact:
If tables have unintended common columns, NATURAL JOIN might give unexpected
results. Proceed with caution!
An EQUI JOIN is a specific case of INNER JOIN where equality (=) is used.
Example
A NON-EQUI JOIN uses conditions other than equality, such as <, >, !=.
Example
FROM employees e
JOIN salary_grades s
FROM employees e
Conclusion
Joins are essential for SQL queries! Whether it's INNER JOIN for precision, LEFT
JOIN for inclusiveness, or CROSS JOIN for creativity, SQL joins are the backbone
of relational databases!