I have a little mess up in my head going on i dont really know why.
My question is how come a right join AND a left join were made to handle database queries, why both of them, isn t one enough?
for instance let s take the left join.
LEFT JOIN table_name2
and we get all the rows from table_name1 and rows from the other table which do have a match
Now if we SWITCH the tables
LEFT JOIN table_name1
and we get all the rows from table_name2 which we could have done with RIGHT JOIN as well but we would have to switch tables.
Once again WHY isn t a for example Left join enough why does a right join exist? VICE VERSA.
yes you are correct that in the scenario you described, you can just simply swap table1 and 2 in your statement and you can get away with just using the right or the left. however, in the bigger picture, you have to consider that you will first set up your database design, relationships and then data will be populated. You may be flexible in what type of data gets entered, allowing null values and you may need to create reports that include these null values so the left and right joins would be necessary where you dont have records that match. You may want to include all of the rows in table1 even if there isnt a match in table2 and vice versa. You arent going to be modifying these queries after the program is developed.
If its just running manual queries in the DB interface, then yes you can get comfortable with a left or right join and you control how to include the table names in your query.