top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Understanding SQL Joins

SQL (Structured Query Language) is a powerful language used for managing and manipulating relational databases. In the databases, data often resides in multiple tables. To extract meaningful information, we need to combine data from these tables. SQL joins are powerful tools that allow us to link data across tables based on related columns. Let's delve into the various types of SQL joins and understand how it helps the data.

 

Let’s imagine a database “Glycemic” consists of two tables: Foods and GlycemicIndices.

 

Foods Table store information about different foods.

ID

FoodName

1

Bread

 2

Wheat

3

Oatmeal

4

Apple

5

Orange


GlycemicIndices stores information on glycemic index values for each foodID.

FoodID

GI

1

75

2

51

3

58

4

38

5

40


Types of SQL Joins:


Inner Join: An inner join returns rows from both tables where the join condition is met. It returns only the matching rows from both tables.

      SELECT Foods.FoodName, GlycemicIndices.GI FROM Foods INNER JOIN GlycemicIndices ON Foods.ID = GlycemicIndices.FoodID;

FoodName

GI

Bread

75

Wheat

51

Oatmeal

58

Apple

38

Orange

40


Left Join (or LEFT OUTER JOIN): A left join returns all rows from the left table and the matching rows from the right table. If there are no matching rows in the right table, the result will contain NULL values for the columns from the right table.

 

SELECT Foods.FoodName, GlycemicIndices.GI FROM Foods LEFT JOIN GlycemicIndices ON Foods.ID = GlycemicIndices.FoodID;


Right Join (or RIGHT OUTER JOIN): A right join is the opposite of a left join. It returns all rows from the right table and the matching rows from the left table. If there are no matching rows in the left table, the result will contain NULL values for the columns from the left table.


SELECT Foods.FoodName, GlycemicIndices.GI FROM Foods RIGHT JOIN GlycemicIndices ON Foods.ID = GlycemicIndices.FoodID;


Full Join(or Full Outer Join): A full outer join returns all rows from both tables, combining the results of a left join and a right join. If there are no matching rows in either table, the result will contain NULL values for the non-matching columns.

 

SELECT Foods.FoodName, GlycemicIndices.GI FROM Foods FULL OUTER JOIN GlycemicIndices ON Foods.ID = GlycemicIndices.FoodID;



Cross Join: A cross join returns the Cartesian product of rows from both tables, combining each row from the first table with every row from the second table:

 

SELECT Foods.FoodName, GlycemicIndices.GI FROM Foods CROSS JOIN GlycemicIndices;

 


SQL joins are essential for querying relational databases effectively. By mastering JOIN, we can unlock the full potential of the database, ensuring we can extract and manipulate data as needed. Understanding joins will help us bridge the data divide efficiently. Let's grab your SQL toolkit and start joining the dots in data!


Happy Querying!!

 


 


8 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page