SQL Joins - Definitions and Examples
In SQL, joins are used to combine rows from two or more tables based on a related column
between them. They help retrieve meaningful data from multiple tables as if they were a
single table. Understanding the different types of joins is crucial for database management
and query optimization.
INNER JOIN
Returns only the records that have matching values in both tables. It filters out rows
without corresponding matches in the other table.
LEFT JOIN (LEFT OUTER JOIN)
Returns all the records from the left table and the matching records from the right table. If
there is no match, NULL values are returned for columns from the right table.
RIGHT JOIN (RIGHT OUTER JOIN)
Returns all the records from the right table and the matching records from the left table. If
there is no match, NULL values are returned for columns from the left table.
FULL JOIN (FULL OUTER JOIN)
Returns all the records when there is a match in either the left or right table. If there is no
match, NULL values are returned for unmatched rows in both tables.
CROSS JOIN
Returns the Cartesian product of two tables, combining each row from the first table with
every row from the second table. It does not require a condition to join the tables.
SELF JOIN
Joins a table with itself. It is useful for hierarchical data or situations where rows in a table
need to be compared to other rows in the same table.