Reputation: 1147
For Example: I have two tables
table agent
table application
1)
select a.agentID, agent_nm from agent a
left join application ap on (ap.agentID=a.agentID);
I am using Left join in above query and got the result is
2)
select a.agentID, agent_nm from application ap
right join agent a on (ap.agentID=a.agentID);
And i am using Right join in above query and got the same result as i got from using the Left join. like
So my question why are left and right join in mysql. We can retrieve the data with left join only swap the table. So why we are using the right join in our queries. Same way we can achieve target only using the Righ join without using the left join. So what is the reason of Left and Right Join in SQL. Can anyone explain the logic?
Upvotes: 2
Views: 367
Reputation: 1838
When you have a more complex statement and want to add something with a right join, you may do not want to swap the statements just because you only have left join's. Can also make queries more hard to read.
Upvotes: 1
Reputation: 198506
Not much of a reason, really. In fact, MySQL docs say:
RIGHT JOIN
works analogously toLEFT JOIN
. To keep code portable across databases, it is recommended that you useLEFT JOIN
instead ofRIGHT JOIN
.
It's just an option you have, one few people exploit.
Upvotes: 1
Reputation: 44795
It's symmetric, LEFT and RIGHT outer joins are exchangeable. Most people use LEFT OUTER join because it's easier to think "main table" left join "additional data"...
Upvotes: 1