Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Different Approach to Subqueries With Oracle 12c

DZone's Guide to

A Different Approach to Subqueries With Oracle 12c

When writing queries in database versions, we couldn't use correlated subqueries in the FROM section of our query. With Oracle 12c, this is addressed.

· Database Zone ·
Free Resource

Databases are better when they can run themselves. CockroachDB is a SQL database that automates scaling and recovery. Check it out here.

In this article, I’ll speak about improvements that were made by Oracle 12c and SQL, such as the phrases LATERALCROSS 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 subqueries in the FROM section of our query. With Oracle 12c, the basis for our uses and needs of this sort has been addressed.

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 the 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, the subselect that we wrote in from the section of our query is dependent on the employees table that we used externally.

Due to this dependence, Oracle could not recognize the external-dependent parameter in the subquery 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 use cases.

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 the correlated subquery on the FROM side. What about the execution plan of our query, though?

lateral-joinNow, let’s examine CROSS APPLY and OUTER APPLYCROSS APPLY  and OUTER APPLY also run in a similar way to the LATERAL basis and serve a similar need. The differences are that if we 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 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 using OUTER JOIN.

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

Databases should be easy to deploy, easy to use, and easy to scale. If you agree, you should check out CockroachDB, a scalable SQL database built for businesses of every size. Check it out here. 

Topics:
oracle ,database ,subquery ,oracle 12c

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}