JOINS Fundamentals
1. INNER JOIN:
This join returns only the rows that have matching values in both tables.
SELECT a.column1, b.column2
FROM table_a a
INNER JOIN table_b b
ON a.id = b.id;
This query retrieves rows where the id in table_a matches the id in table_b.
2. LEFT (OUTER) JOIN:
This returns all rows from the left table and the matched rows from the right table. If there is
no match, NULLs are returned for columns from the right table.
SELECT a.column1, b.column2
FROM table_a a
LEFT JOIN table_b b
ON a.id = b.id;
This returns all records from table_a and the matched records from table_b. Rows from
table_a without a match in table_b will show NULL for columns from table_b.
3. FULL JOIN (or FULL OUTER JOIN):
This returns rows when there is a match in one of the tables. It combines the results of both
LEFT and RIGHT joins.
SELECT a.column1, b.column2
FROM table_a a
FULL OUTER JOIN table_b b
ON a.id = b.id;
This will return all records when there is a match in either table. If there is no match, NULL values
are returned for non-matching columns from the other table.