Are left outer joins and left joins the same? [duplicate] - mysql

This question already has answers here:
Difference between RIGHT & LEFT JOIN vs RIGHT & LEFT OUTER JOIN in SQL [duplicate]
(4 answers)
Closed 9 years ago.
I have seen joins called LEFT OUTER JOIN or RIGHT OUTER JOIN. In some places I have seen LEFT JOIN or RIGHT JOIN. I am confused by this.
I posted a question 2 days ago, but I am unable to understand the links the solutions provide.
Are these types of joins both the same, or is there some difference between the two?

There are no difference between both.
Refer visual represenation of joins

The first link they quoted gives you:
INNER JOIN: returns rows when there is a match in both tables.
LEFT JOIN / LEFT OUTER JOIN: returns all rows from the left table, even if there are no matches in the right table.
RIGHT JOIN / RIGHT OUTER JOIN: returns all rows from the right table, even if there are no matches in the left table.
FULL JOIN / FULL OUTER JOIN / OUTER JOIN: returns rows when there is a match in one of the tables.
SELF JOIN: is used to join a table to itself, as if the table were two tables, temporarily renaming at least one table in the SQL statement.
CARTESIAN JOIN: returns the cartesian product of the sets of records from the two or more joined tables.
The self join is actually not a special join. It just reflects the fact that you can join a table with itself. Doing so you must alias it in order to address the fact that it appears more than once in the same statement.
The cartesian join can be considered as an inner join without a restricting condition. Or you may view an inner join as a cartesian join with an added restriction (the join condition).

If you look at the manual page for JOIN you will see the following lines:
join_table:
table_reference [INNER | CROSS] JOIN table_factor [join_condition]
| table_reference STRAIGHT_JOIN table_factor
| table_reference STRAIGHT_JOIN table_factor ON conditional_expr
| table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor
The bold line clearly shows that the keyword OUTER is optional.

In MySQL syntax, LEFT OUTER JOIN and LEFT JOIN are identical: (from http://dev.mysql.com/doc/refman/5.0/en/join.html)
| table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor
Note that the OUTER keyword is optional for LEFT/RIGHT JOIN.
LEFT and RIGHT joins are both outer joins. I believe there are some flavors of SQL that may require the OUTER keyword, but MySQL does not. That is to say that at times LEFT JOIN may not be valid.

I am working in SQL Server and as per my usage and experience there is absolutely no difference between LEFT JOIN and LEFT OUTER JOIN. The same is true for RIGHT JOIN and RIGHT OUTER JOIN. When you use LEFT JOIN keyword in SQL Server, it means LEFT OUTER JOIN only.
So as per my opinion its the generic rule which is same for all database engines.
see here and here

Related

Why LEFT JOIN is terribly slower than INNER JOIN?

I have two tables, toynav_product_import - 18533 rows, catalog_product_entity - 42000 rows.
The below query, LEFT JOIN takes more than 2 minutes, while INNER JOIN runs in 0.009 seconds. The first table has the necessary index for the barcode field.
SELECT tpi.barcode FROM toynav_product_import tpi
INNER JOIN catalog_product_entity cpe ON tpi.barcode = cpe.sku
Please advise
toynav_product_import
catalog_product_entity
An outer join ( LEFT JOIN or RIGHT JOIN ) has to do all the work of an INNER JOIN plus the extra work of null-extending the results
And even if a LEFT JOIN were faster in specific situations, it is not functionally equivalent to an INNER JOIN, so you cannot simply go replacing all instances of one with the other!
Sorry cant post this as a comment
A LEFT JOIN is slower than the Inner Join. By definition, an outer join (LEFT JOIN or RIGHT JOIN) has to do all the work of an INNER JOIN plus the extra work of null-extending the results, Thats the reason. And as it also returns more number of Rows as compare to inner join, Thats why execution takes more time.
But by indexing the Foreign Keys properly, you can definitely increase the performance of the Joins.
It also depends on the Data, Its not always the case that Left join is slower, There are the cases when Left join is faster, But mostly Inner join is faster according to above described reasons.
Please refer to this link, the guy explained the difference very clearly.

Mysql: how can `natural join` using `join_condition`?

In Mysql, I found the natural join syntax is:
`table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor`,
but other join types like inner or outer join can use join_conditon, like the syntax:
`table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition`,
so how can I use join_condition in natural join? Is there some alternatives?
A natural join has an implicit join condition, equality on all columns with the same name.
If you want some other join condition, don't use a natural join. If a natural join's implicit join condition is what you want, use that. In general, it's probably easier for everyone to understand if you don't use a natural join and make your join condition explicit.

Why left join show the error?

Is there is any error in the query i have given below.
SELECT * FROM employee Left JOIN bonus;
I have executed this using the sqlfiddle online. It shows me a error.
and also am facing the issue for full outer join.
SELECT * FROM bonus FULL OUTER JOIN employee ON employee.id=bonus.id;
You must specify a join column when using a LEFT or RIGHT join. How are your Employee and Bonus tables related? If both tables contain an "EmployeeId" column for example, then you would do something like this:
SELECT * FROM employee LEFT JOIN bonus ON employee.EmployeeId = bonus.EmployeeId
If you don't use ON, all joins automatically become CROSS JOIN. You can try it with CROSS JOIN. Here is the mysql documentation: http://dev.mysql.com/doc/refman/5.5/en/join.html
Per documentation :http://dev.mysql.com/doc/refman/5.5/en/join.html
join_table:
table_reference [INNER | CROSS] JOIN table_factor [join_condition]
| table_reference STRAIGHT_JOIN table_factor
| table_reference STRAIGHT_JOIN table_factor ON conditional_expr
| table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor
In MySQL the on is optional for join, but not for left outer join or right outer join.

Is the On Clause Neccessary for Left/Right Outer Joins

I have the following very simple left outer join in MYSQL:
select * from employee left join department
However, this gives me the meaningless error message
Error Code: 1064. 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 '' at line 1
But if I simply add an ON clause as shown below, the query works:
select * from employee left join department on employee.departmentId = department.id;
So, is the ON clause mandatory? If I do a "Full Join", it definitely works without an ON clause, but left and right outer joins don't work unless I add an ON clause to them.
By the way there is nothing special about the 2 tables, and there is no foreign key relation between them either.
Edition:
This is the full join that works in MySql:
select * from employee full join department;
In the ANSI standard, on is required for all join types except cross join. This applies to Oracle and most other databases.
MySQL is a bit different. This is the syntax diagram in the MySQL documentation:
join_table:
table_reference [INNER | CROSS] JOIN table_factor [join_condition]
| table_reference STRAIGHT_JOIN table_factor
| table_reference STRAIGHT_JOIN table_factor ON conditional_expr
| table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
| table_reference NATURAL [{LEFT|RIGHT} [OUTER]] JOIN table_factor
That is, MySQL allows the on to be optional for join, but not left outer join or right outer join. MySQL does not support full outer join. And, to make it more confusing cross join is like join and can accept an on clause.
And, you should ignore the MySQL extensions. Always use an on clause for left, right, and inner joins. Never use an on clause for cross join. I prefer using these to the natural join (which you don't ask about), because I prefer to be explicit in the columns being used for the join.
EDIT:
According to SQL Fiddle, version 5.6 gets an error on full outer join when left outer join works. So this generates an error:
select *
from (select 1 as a) t1 full outer join
(select 2 as b) t2
on t1.a = t2.b;
This also generates an error:
select *
from (select 1 as a) t1 full join
(select 2 as b) t2;
And MySQL documentation (through 5.7) is all very clear that full join is not supported. I'm not sure why your query might have worked.
If you want that it work without ON clause, you need the same column name in the 2 tables:
department.departmentId with employee.departmentId

LEFT OUTER JOIN is expressed as just LEFT JOIN in MySql syntax?

Is LEFT OUTER JOIN expressed as just LEFT JOIN in MySQL syntax? Or are LEFT OUTER JOIN and LEFT JOIN different things in MySQL?
Basically they are the same.
LEFT and RIGHT are outer type join. The keyword OUTER is optional, and what this means in this case is that whether you specify it or not makes no difference.
Additional Information,
At the top level there are mainly 3 types of joins:
INNER
OUTER (LEFT, RIGHT)
CROSS