7.
Perform JOIN Operations (All Types of Joins)
Theory:
JOINs in SQL are used to combine rows from two or more tables based on a related column
between them.
1. INNER JOIN
Syntax:
SELECT table1.column1, table2.column2
FROM table1
INNER JOIN table2 ON table1.common_column = table2.common_column;
Returns: Only the matching rows from both tables.
2. LEFT JOIN (LEFT OUTER JOIN)
Syntax:
SELECT table1.column1, table2.column2
FROM table1
LEFT JOIN table2 ON table1.common_column = table2.common_column;
Returns: All rows from the left table, and matched rows from the right table. NULL for
unmatched rows.
3. RIGHT JOIN (RIGHT OUTER JOIN)
Syntax:
SELECT table1.column1, table2.column2
FROM table1
RIGHT JOIN table2 ON table1.common_column = table2.common_column;
Returns: All rows from the right table, and matched rows from the left table. NULL for
unmatched rows.
4. FULL OUTER JOIN
Note: Not supported directly in MySQL
Syntax (Using UNION):
SELECT table1.column1, table2.column2
FROM table1
LEFT JOIN table2 ON table1.common_column = table2.common_column
UNION
SELECT table1.column1, table2.column2
FROM table1
RIGHT JOIN table2 ON table1.common_column = table2.common_column;
Returns: All records when there is a match in either table.