In SQL, a JOIN operation is used to combine rows from two or more tables based on a related column between them. It allows you to retrieve data from multiple tables simultaneously by specifying the relationship between them. There are several types of JOIN operations in SQL, including:
- INNER JOIN: Returns only the matching rows between two tables based on the specified join condition.
SELECT column_list FROM table1 INNER JOIN table2 ON table1.column = table2.column;
- LEFT JOIN (or LEFT OUTER JOIN): Returns all rows from the left table and the matching rows from the right table based on the join condition. If there are no matching rows in the right table, NULL values are returned.
SELECT column_list FROM table1 LEFT JOIN table2 ON table1.column = table2.column;
- RIGHT JOIN (or RIGHT OUTER JOIN): Returns all rows from the right table and the matching rows from the left table based on the join condition. If there are no matching rows in the left table, NULL values are returned.
SELECT column_list FROM table1 RIGHT JOIN table2 ON table1.column = table2.column;
- FULL JOIN (or FULL OUTER JOIN): Returns all rows from both tables, including the unmatched rows. If there are no matching rows, NULL values are returned for the columns of the table without a match.
SELECT column_list FROM table1 FULL JOIN table2 ON table1.column = table2.column;
- CROSS JOIN: Returns the Cartesian product of the two tables, which means it combines each row from the first table with every row from the second table. It does not require a join condition.
SELECT column_list FROM table1 CROSS JOIN table2;
These are the basic types of JOIN operations in SQL. Each type serves different purposes and can be used to retrieve data from multiple tables based on the desired relationship.