Purpose: The JOIN
clause combines rows from two or more tables based on a related column, facilitating complex queries across multiple tables.Types of Joins:
- INNER JOIN: Returns only rows with matching values in both tables.
- LEFT JOIN: Returns all rows from the left table and matched rows from the right, with NULLs for non-matches.
- RIGHT JOIN: Returns all rows from the right table and matched rows from the left, with NULLs for non-matches.
- FULL OUTER JOIN: Returns all rows when there is a match in either left or right table.
Example
SELECT e.first_name, d.department_name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
Explanation: This retrieves employee names along with their department names by joining the employees
and departments
tables on department_id
.
Leave a Reply