SQL - Joins
SQL joins are used to combine rows from two or more tables based on a related column between them. Here’s a detailed tutorial on the various types of joins available in SQL.
Types of Joins
- INNER JOIN: Returns records that have matching values in both tables.
- LEFT (OUTER) JOIN: Returns all records from the left table and the matched records from the right table. The result is NULL from the right side if there is no match.
- RIGHT (OUTER) JOIN: Returns all records from the right table and the matched records from the left table. The result is NULL from the left side when there is no match.
- FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table. Records that do not match in either table are returned with NULL values.
- CROSS JOIN: Returns the Cartesian product of both tables, meaning every row in the first table is combined with every row in the second table.
Using Aliases
Aliases can make your queries shorter and more readable.
Example:
SELECT e.first_name, e.last_name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;