Types Of Sql Join in SQL
Sure! Here are the main types of SQL JOINs used to combine rows from two or more tables based on related columns:
1. INNER JOIN
Returns only the rows where there is a match in both tables.
Rows without matching values in either table are excluded.
SELECT columnsFROM table1INNER JOIN table2ON table1.common_column = table2.common_column;2. LEFT JOIN (or LEFT OUTER JOIN)
Returns all rows from the left table and matched rows from the right table.
If there is no match, the result contains
NULLfor right table columns.
SELECT columnsFROM table1LEFT JOIN table2ON table1.common_column = table2.common_column;3. RIGHT JOIN (or RIGHT OUTER JOIN)
Returns all rows from the right table and matched rows from the left table.
If no match,
NULLvalues for left table columns.
SELECT columnsFROM table1RIGHT JOIN table2ON table1.common_column = table2.common_column;4. FULL JOIN (or FULL OUTER JOIN)
Returns rows when there is a match in either left or right table.
Non-matching rows from both sides are included with
NULLs for missing parts.
SELECT columnsFROM table1FULL OUTER JOIN table2ON table1.common_column = table2.common_column;5. CROSS JOIN
Returns the Cartesian product of both tables.
Combines every row of the first table with every row of the second table.
No ON clause needed.
SELECT columnsFROM table1CROSS JOIN table2;6. SELF JOIN
A join where a table is joined with itself.
Useful for hierarchical or comparative queries.
SELECT a.columns, b.columnsFROM table1 aJOIN table1 bON a.common_column = b.related_column;Summary Table
| Join Type | Description | Result Includes |
|---|---|---|
| INNER JOIN | Rows with matching keys in both tables | Only matched rows |
| LEFT JOIN | All rows from left table + matched right rows | Left rows + NULL for non-matching right rows |
| RIGHT JOIN | All rows from right table + matched left rows | Right rows + NULL for non-matching left rows |
| FULL JOIN | Rows matched in either table | All rows from both tables with NULLs for unmatched parts |
| CROSS JOIN | Cartesian product of both tables | Every combination of rows |
| SELF JOIN | Table joined with itself | Rows combined within same table |
If you want, I can provide example queries for any join type!