SELECT * FROM left_table OUTER JOIN right_table on left_table.name = righ
t_table.name;
SELECT * FROM left_table FULL OUTER JOIN right_table on left_table.name =
right_table.name;
These 2 statements gives below error. Are they not valid mysql statements ?
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'OUTER
JOIN right_table on left_table.name = right_table.name' at line 1
mysql> SELECT * FROM left_table RIGHT OUTER JOIN right_table on left_table.name
= right_table.name;
and
mysql> SELECT * FROM left_table RIGHT JOIN right_table on left_table.name
= right_table.name;
give same result. So whats the basic use of adding OUTER in statement?
I want to know what special purpose does it serve by adding OUTER in statement where as without adding it also its returning same output.
—- Update —-
Ok i give my table structure
mysql> select * from left_table;
+----+---------+
| id | name |
+----+---------+
| 1 | Pirate |
| 2 | money |
| 5 | Ninja |
| 6 | pradeep |
+----+---------+
and
mysql> select * from right_table;
+----+-------------+
| id | name |
+----+-------------+
| 1 | Rutabaga |
| 2 | Pirate |
| 3 | Darth Vader |
| 4 | Ninja |
+----+-------------+
I ran statements like
mysql> SELECT * FROM left_table LEFT JOIN right_table on left_table.name = right
_table.name where right_table.id is NULL;
+----+---------+------+------+
| id | name | id | name |
+----+---------+------+------+
| 2 | money | NULL | NULL |
| 6 | pradeep | NULL | NULL |
+----+---------+------+------+
and
mysql> SELECT * FROM left_table LEFT OUTER JOIN right_table on left_table.name =
right_table.name where right_table.id is NULL;
+----+---------+------+------+
| id | name | id | name |
+----+---------+------+------+
| 2 | money | NULL | NULL |
| 6 | pradeep | NULL | NULL |
+----+---------+------+------+
They still produce the same output. SO if any1 can show some statement where the results will differ. i would be able to understand it.
They may, in your specific testing case, produce the same results. However, they will not always.
An outer join, instead of an inner join, means still show results from the main table (which depends on whether you’re using left or right outer join) even if there is no corresponding row in the secondary table.
An inner join would not show the result at all if there was a row in the main table that matched the
WHEREconditions but did not have a row that it could join with in the secondary table.If your tables that you’re joining in testing have matching rows for every result (i.e. every row in
left_tablehas a row inright_tablewith the same value in thenamefield), then you won’t see any difference.What would make a difference is if you had some rows in one table, or in each table, that didn’t have a row in the other table.
For:
select * from left_table left outer join right_table on...where...and there are some rows in theright_tablethat meet the conditionals (in the where) but don’t have a row in theleft_table, they would not show. But if there were rows in theleft_tablethat meet the conditionals without a joining row in theright_table, they would still show.Same goes reverse for
RIGHT OUTER JOIN.EDIT:
LEFT JOINandLEFT OUTER JOINare aliases of each other. They are the same thing. I would just useOUTERto clarify for your own sake, if you’d like. I prefer to always useOUTERorINNERjust so I can look quickly and see what I did.