Oracle SQL: LATERAL, CROSS APPLY, OUTER APPLY

Hi everyone,

In this article I’ll speak about improvements that were made by Oracle 12c and SQL, such as the phrases LATERAL, CROSS APPLY and OUTER APPLY. I hope this will be a helpful article in terms of awareness.

When writing queries in database versions before Oracle 12c, we weren’t able to use correlated subquery in the FROM section of our query. With Oracle 12c, the basis for our uses and needs of this sort has been accessed.

Now, let’s figure out how this structure works through some examples.

First of all, let’s look at the result that we get when we run the query below in 11g database.

 

SELECT *
FROM hr.employees e,
  (SELECT * FROM hr.departments WHERE department_id = e.department_id) x;

ORA-00904: "E"."DEPARTMENT_ID": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 14 Column: 55

So, subselect that we wrote in from section of our query is dependent on employees table that we used externally.
Due to this dependence, Oracle could not recognize the external-dependent parameter in subquerry, and received an error. And this is the very point on which Oracle made the necessary improvements, thus paving the way for the user for these kinds of uses.
Now, let’s try to substantiate this need on 12c database.

SELECT *
FROM hr.employees e,
  LATERAL
  (SELECT * FROM hr.departments WHERE department_id = e.department_id) x
WHERE x.department_name ='Public Relations';

We can see that the result returns successfully when we run our query. With this command, now we could use correlated subquerry on FROM side. What about execution plan of our query, though?

lateral-join

Now, let’s examine CROSS APPLY and OUTER APPLY. CROSS APPLY and OUTER APPLY also run in a similar way to LATERAL basis, and serves a similar need. The differences between are: if wer run CROSS APPLY, it automatically applies CROSS JOIN on tables/inline views that we wrote in FROM. If we use OUTER APPLY, it applies LEFT OUTER JOIN on tables/inline views that we wrote in FROM.
Now, let’s see how to use both these two commands.

SELECT *
FROM hr.employees e
  CROSS APPLY
  (SELECT * FROM hr.departments WHERE department_id = e.department_id) x;

The query above integrated the two groups by using cross join.

SELECT *
FROM hr.employees e
  OUTER APPLY
  (SELECT * FROM hr.departments WHERE department_id = e.department_id) x;

outer-apply

The query above integrated the two groups by using outer join.

So, it seems that these commands which have newly arrived with ORACLE 12c SQL will facilitate our job a great deal in cases where we’ll need them.

Advertisements

About ... from Emrah METE

Bilgisayar Mühendisi
This entry was posted in Oracle, Root and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s