What is the difference between left join and right join?
Overview
A JOIN clause integrates rows (tuples) from two or more tables based on a similar column/attribute between them. There are various varieties of join. Now, we will discuss only two of them, which are LEFT OUTER JOIN and RIGHT OUTER JOIN.
LEFT JOIN
This is sometimes referred to as a Left Outer Join. As a result, Outer is an optional keyword to use with Left Join. The Left Join clause connects two or more tables and retrieves all rows from the left table, as well as those who get matched with the right table, or it returns null if no matching record is found.
RIGHT JOIN
A Right Outer Join is another name for this. As a result, Outer is a term that may be used with Right Join. The Right Join clause joins two or more tables and obtains all rows from the right table and also those who get matched from the left table, returning null if no matching record is found.
Venn diagram
A Venn diagram will help us to understand the concepts of LEFT OUTER JOIN and RIGHT OUTER JOIN more clearly.
Example
In this example, we see the difference between LEFT OUTER JOIN and RIGHT OUTER JOIN.
-- Create Country tableCREATE TABLE Country(Country_id int primary key,Country_name varchar(15),Country_currency varchar(5));-- Insert valuesINSERT INTO Country VALUES(1, "India", "INR");INSERT INTO Country VALUES(2, "Iran", "IRR");INSERT INTO Country VALUES(3, "Italy", "EUR");INSERT INTO Country VALUES(5, "Turkey", "TRY");-- Print DataSELECT "Country","=>>",country_id AS ID, country_name AS Name,"",country_currency AS Currency FROM Country; SELECT "";-- Create City tableCREATE TABLE City(City_id int primary key,City_name varchar(15),Countryid int);-- Insert valuesINSERT INTO City VALUES(1, "Mumbai", 1);INSERT INTO City VALUES(2, "Ankara", 5);INSERT INTO City VALUES(3, "GOA", "1");INSERT INTO City VALUES(4, "Tehran", 2);INSERT INTO City VALUES(5, "Kabul", NULL);-- Print DataSELECT "City","=>>",City_id AS ID, City_name AS Name,"",Countryid FROM City; SELECT "";-- LEFT JoinSELECT "LEFT OUTER JOIN","=>>",Country_name AS Country,"",City_name AS City,"",Country_currency AS CurrencyFROM Country LEFT JOIN City ON Country.Country_id=City.Countryid;SELECT "";-- RIGHT JoinSELECT "RIGHT OUTER JOIN =>>",Country_name AS Country,"",City_name AS City,"",Country_currency AS CurrencyFROM Country RIGHT JOIN City ON Country.Country_id=City.Countryid;SELECT "";
Explanation
- In lines 2-32, we create the
CountryandCitytables using theCREATEcommand, insert values using theINSERTcommand, and display the data of theCountrytable using theSELECTCommand. - In lines 35-38, we integrate tables using
LEFT JOIN. It retrieves all rows from the left table (Country) as well as those that get matched with the right table (City), or it returns null if no matching record is found.Italyhas noCityin the record, so it showsNULLin theCityattribute. - In lines 41-45, we join tables using
RIGHT JOIN. It returns null if no matching record is discovered, and it obtains all rows from the right table (City) as well as those that match with the left table (Country).Countryandcurrencyare also not specified forKabul; therefore, it showsNULLin both columns.
Free Resources