SELECT *
FROM ttms LEFT JOIN country
ON ttms.country = country.id
WHERE color = ‘gold’;
SELECT *
FROM ttms RIGHT JOIN country
ON ttms.country = country.id
WHERE color = ‘gold’;
Why are the results of the two queries the same this time?
Expert Answer
Right Join : Generally what right join does is it return all the records of table2 and only the matched records in table1 . If thier are no matched values in table2 then it will return null value.
Left Join : It is vice versa of Right Join. Generally what left join does is it returns all the records of table1and only the matched records in table2. If their are no matched values in table1 then it will return null value.
NOTE: 1) If table1_row_count is greater than table2_row_count it is good to use right_join.
2) If table1_row_count is less than table2_row_count it is good to use left_join.
EXAMPLE
Table1(TTMS)
Country | Colour |
1 | gold |
2 | silver |
3 | gold |
4 | black |
Table2(Country)
id | name |
1 | abc |
3 | xyz |
5 | pqr |
So here when you use left and right join queris you will get same answer as
1 gold
3 gold
Because we have insisted that all the results must have a certain value by using where clause. That is the reason you are getting same result for both the queries. In this case it woks like inner_join
If you do not give where clause the result may vary.
For example instead of using where clause on the joined table if we add the join conditions you will get a different result like how right and left join works.
select * from ttms LEFT join country on ttms.country=country.id AND color = ‘gold’;
(or)
select * from ttms LEFT join country on ttms.country=country.id WHERE(color=’gold’ OR color IS NULL);
Same for Right join as well
In this case you will get the values even the color is not gold.