In SQL, joins are used to combine rows from two or more tables based on a related column between them. Joins allow you to retrieve data from multiple tables simultaneously by establishing relationships between them. Here are the different types of joins commonly used in SQL:
1. Inner Join: Retrieves matching rows from both tables based on the specified join condition.
sql
SELECT *
FROM table1
INNER JOIN table2 ON table1.column = table2.column;
This query performs an inner join between `table1` and `table2`, where rows are matched based on the equality of `table1.column` and `table2.column`.
2. Left Join (or Left Outer Join): Retrieves all rows from the left table and matching rows from the right table.
sql
SELECT *
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
This query performs a left join between `table1` and `table2`, returning all rows from `table1` and the matching rows from `table2`. If there are no matches in `table2`, NULL values will be returned for the columns from `table2`.
3. Right Join (or Right Outer Join): Retrieves all rows from the right table and matching rows from the left table.
sql
SELECT *
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
This query performs a right join between `table1` and `table2`, returning all rows from `table2` and the matching rows from `table1`. If there are no matches in `table1`, NULL values will be returned for the columns from `table1`.
4. Full Join (or Full Outer Join): Retrieves all rows from both tables, including the unmatched rows.
sql
SELECT *
FROM table1
FULL JOIN table2 ON table1.column = table2.column;
This query performs a full join between `table1` and `table2`, returning all rows from both tables. If there are no matches, NULL values will be returned for the columns from the respective table.
5. Cross Join: Generates the Cartesian product of the two tables, resulting in a combination of all rows from both tables.
sql
SELECT *
FROM table1
CROSS JOIN table2;
This query performs a cross join between `table1` and `table2`, combining each row from `table1` with every row from `table2`. The result set will contain all possible combinations.
These are the commonly used types of joins in SQL. The specific type of join you choose depends on the relationship between the tables and the desired result set. By using joins, you can combine data from multiple tables to extract meaningful insights and perform complex queries.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc