In MySQL, “joins” are used to combine rows from two or more tables based on a related column between them. The primary types of joins are: These joins are powerful tools for querying data from multiple tables based on logical relationships
- INNER JOIN:- Returns rows when there is a match in both tables.
SELECT columns
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
- LEFT JOIN (or LEFT OUTER JOIN):- Returns all rows from the left table and the matched rows from the right table. Unmatched rows will have `NULL` values.
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
- RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table and the matched rows from the left table. Unmatched rows will have `NULL` values.
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
- FULL JOIN (or FULL OUTER JOIN): Combines the result of both left and right joins. It returns all rows from both tables, with `NULL` for non-matching rows on either side. Note that MySQL does not directly support `FULL OUTER JOIN`, but it can be emulated using `UNION`.
SELECT columns
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name
UNION
SELECT columns
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
- CROSS JOIN: Returns the Cartesian product of the rows from the tables, meaning each row from the first table is combined with all rows from the second table
SELECT columns
FROM table1
CROSS JOIN table2;