Z.I.J
Z.I.J

Reputation: 1147

What is reason of LEFT and Right join in SQL

For Example: I have two tables

table agent enter image description here

table application enter image description here

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 enter image description here

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 enter image description here

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

Answers (3)

overflowed
overflowed

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

Amadan
Amadan

Reputation: 198506

Not much of a reason, really. In fact, MySQL docs say:

RIGHT JOIN works analogously to LEFT JOIN. To keep code portable across databases, it is recommended that you use LEFT JOIN instead of RIGHT JOIN.

It's just an option you have, one few people exploit.

Upvotes: 1

jarlh
jarlh

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

Related Questions