Joins With Employee and Departments
Insert data into the tables,
INNER JOIN
Definition: Combines rows from two tables when there is a matching value in the specified columns.
Use Case: When you want only the records that have corresponding entries in both tables.
Real-World Example: Show employees and their respective departments.
Explanation:
The INNER JOIN
only includes employees who belong to a department. Employees without a department will not appear in the result.
LEFT JOIN (or LEFT OUTER JOIN)
Definition: Returns all rows from the left table and the matching rows from the right table. If no match is found, NULL values are returned for columns of the right table.
Use Case: When you want to see all entries in one table (left) and their matches, if they exist, in the other table.
Real-World Example: List all employees, including those who don’t belong to any department.
RIGHT JOIN (or RIGHT OUTER JOIN)
Definition: Returns all rows from the right table and the matching rows from the left table. If no match is found, NULL values are returned for columns of the left table.
Use Case: When you want to ensure that all entries from the right table are included, even if there’s no match in the left table.
Real-World Example: List all departments, even those with no employees
All departments appear in the result. If a department has no employees, the employee column will show NULL
.
FULL OUTER JOIN
Definition: Combines the results of both LEFT JOIN
and RIGHT JOIN
. Returns all rows from both tables, with NULLs for non-matching rows.
Use Case: When you need to see everything, even if there are no matches in either table.
Real-World Example: List all employees and departments, including unmatched records from both.
Explanation: The result will include
Employees with departments.
Employees without departments (
dept_name = NULL
).Departments with no employees (
emp_name = NULL
).
Last updated