I went through an article about CROSS APPLY
and OUTER APPLY
in SQL Server. The following tables were used to illustrate both.
Employee table :
EmployeeID FirstName LastName DepartmentID
1 Orlando Gee 1
2 Keith Harris 2
3 Donna Carreras 3
4 Janet Gates 3
Department table:
DepartmentID Name
1 Engineering
2 Administration
3 Sales
4 Marketing
5 Finance
I understood that OUTER APPLY
is similar to LEFT OUTER JOIN.
But when I applied OUTER APPLY
between tables as below,
select * from Department e
outer apply
Employee d
where d.DepartmentID = e.DepartmentID
I got below results (Same as INNER JOIN
results)
DepartmentID Name EmployeeID FirstName LastName DepartmentID
1 Engineering 1 Orlando Gee 1
2 Administration 2 Keith Harris 2
3 Sales 3 Donna Carreras 3
3 Sales 4 Janet Gates 3
When I applied OUTER APPLY
between tables as below( with right table
as a subquery).
select * from Department e
outer apply
(
select * from
Employee d
where d.DepartmentID = e.DepartmentID
)a
I got below results (Same as LEFT OUTER JOIN
results)
DepartmentID Name EmployeeID FirstName LastName DepartmentID
1 Engineering 1 Orlando Gee 1
2 Administration 2 Keith Harris 2
3 Sales 3 Donna Carreras 3
3 Sales 4 Janet Gates 3
4 Marketing NULL NULL NULL NULL
5 Finance NULL NULL NULL NULL
Can Someone explain why the two queries gave different outputs
?