First, your outer join is being "nullified" because your additional selection criteria is more selective than your query. However, even without the additional criteria, you are trying to query all departments whether or not they have any employees. If your example is based on modifications to the Scott schema, there are no employees without departments unless you have added some. In this case the result of an outer join is the same as an inner join.
The plus in parenthesis (+) means add a blank if there aren't any. The way you've coded this (above) means that any employee who is not in a department will be displayed with NULL department information. Because you have attached to it the additional criteria that that you only want to see rows where the department location is 'TVM' you will not see any employees that have no department... because their department is, by definition, NULL. You have effectively nullified your outer join by additional selection criteria even if you have employees who are in no department.Code:from emp, dept where emp.deptno = dept.deptno(+)
The equivalent join clause for your code above is
The keyword "left" indicates that you want to see all rows from the table on the left of the word join... this is the opposite of the placement of (+).Code:from emp left outer join dept on emp.deptno = dept.deptno
As I've said, in the Scott schema there are no employees without departments. But there are departments with no employees. If your tables have departments with no employees and you want to see all departments with or without employees you would query
orCode:from emp, dept where emp.deptno(+) = dept.deptno
But be careful of selection criteria that is more restrictive than your outer join.Code:from emp right outer join dept on emp.deptno = dept.deptno




Reply With Quote