SQL - Joins

In SQL, there are several types of joins that allow you to combine data from multiple tables based on a specified condition. Here are the most commonly used types of joins:

INNER JOIN: The INNER JOIN returns the matched rows from both tables based on a specified condition. It only includes the rows where the join condition is satisfied in both tables. The syntax for an INNER JOIN is as follows:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

LEFT JOIN (or LEFT OUTER JOIN): The LEFT JOIN returns all rows from the left table and the matched rows from the right table based on the join condition. If there is no match, NULL values are included for the columns of the right table. The syntax for a LEFT JOIN is as follows:

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

RIGHT JOIN (or RIGHT OUTER JOIN): The RIGHT JOIN returns all rows from the right table and the matched rows from the left table based on the join condition. If there is no match, NULL values are included for the columns of the left table. The syntax for a RIGHT JOIN is as follows:

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

FULL JOIN (or FULL OUTER JOIN): The FULL JOIN returns all rows from both tables, including unmatched rows from both tables. If there is no match, NULL values are included for the columns of the non-matching table. The syntax for a FULL JOIN is as follows:

SELECT column_name(s)
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;

CROSS JOIN: The CROSS JOIN returns the Cartesian product of the two tables, resulting in a combination of each row from the first table with each row from the second table. It does not require a join condition. The syntax for a CROSS JOIN is as follows:

SELECT column_name(s)
FROM table1
CROSS JOIN table2;

These are the main types of joins in SQL. Each join has its specific purpose and usage, allowing you to retrieve data from multiple tables based on different conditions. Adjust the table names and column names as per your specific database schema and requirements.