Skip to content
Advertisement

SQL Conditional on SELECT Subquery value

I want to apply a condition check on my select subquery. How can I do it optimally?

Here’s the initial query:

And I want to add the WHERE condition subResult = :parameter. And the :parameter is optional, meaning if :parameter is null, include row in result.

But I can’t reference the result of the subquery subResult inside my WHERE condition.

I can copy/paste the subquery into the WHERE clause, but that seems sloppy and error prone:

Advertisement

Answer

If you are running Oracle 12 or higher, I would recommend a lateral join:

In earlier versions, you can use a subquery or CTE:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement