Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9291079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T20:33:56+00:00 2026-06-18T20:33:56+00:00

How do I perform a correlated subquery in Oracle that returns the first matching

  • 0

How do I perform a correlated subquery in Oracle that returns the first matching row based on an ORDER BY clause? I’m trying to translate a query from SQL Server that does this.

For the record, I need to stick to (mostly) SQL-92 syntax. Analytic functions should not be used at all, and I need to minimize the use of nonstandard SQL. TOP 1 ... ORDER BY is proprietary to SQL Server and I’m struggling translating it to rownum.

Note: It has been pointed out that this particular query doesn’t require TOP/LIMIT/rownum since it is semantically equivalent to use Min() as we only want one column. But I would still appreciate, and will reward, any help on how to perform the translation as stated–since I would like to learn Oracle better.

Here’s the SQL Server query (and the SqlFiddle for it):

SELECT
  D.StartDate,
  (
    SELECT TOP 1 E.EndDate
    FROM dbo.Dates E
    WHERE
      E.EndDate >= D.EndDate
      AND NOT EXISTS (
        SELECT *
        FROM dbo.Dates E2
        WHERE
          E.StartDate < E2.StartDate
          AND E.EndDate > E2.StartDate
      )
    ORDER BY
      E.EndDate,
      E.StartDate DESC
  ) EndDate
FROM
  dbo.Dates D
WHERE
  NOT EXISTS (
    SELECT *
    FROM dbo.Dates D2
    WHERE
      D.StartDate < D2.EndDate
      AND D.EndDate > D2.EndDate
  );

This is what I’ve tried. I’m stymied because I’m getting an error on the D.EndDate outer reference.

ORA-00904: “D”.”ENDDATE”: invalid identifier

But what’s the problem? A correlated subquery in the SELECT clause should have access to all outer table data. I don’t know where to go next. (And the SqlFiddle for this).

SELECT
  D.StartDate,
  (
    SELECT *
    FROM (
      SELECT E.EndDate
      FROM Dates E
      WHERE
        E.EndDate >= D.EndDate
        AND NOT EXISTS (
          SELECT *
          FROM Dates E2
          WHERE
            E.StartDate < E2.StartDate
            AND E.EndDate > E2.StartDate
        )
      ORDER BY
        E.EndDate,
        E.StartDate DESC
    )
    WHERE rownum = 1
  ) EndDate
FROM
  Dates D
WHERE
  NOT EXISTS (
    SELECT *
    FROM Dates D2
    WHERE
      D.StartDate < D2.EndDate
      AND D.EndDate > D2.EndDate
  );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T20:33:57+00:00Added an answer on June 18, 2026 at 8:33 pm

    I might be missing something, but can you not use MIN instead of TOP 1 ... ORDER BY, since you are ordering by EndDate, StartDate DESC, and only selecting EndDate, the start date is not relevant to the sort, it is only considered when you have 2 end dates that are the same, but since you are only selecting end date it doesn’t matter which of the two (or more end dates) are used:

    SELECT  D.StartDate,
            (   SELECT  MIN(E.EndDate)
                FROM    Dates E
                WHERE   E.EndDate >= D.EndDate
                AND     NOT EXISTS 
                        (   SELECT  1
                            FROM    Dates E2
                            WHERE   E.StartDate < E2.StartDate
                            AND     E.EndDate > E2.StartDate
                        )
            ) EndDate
    FROM    Dates D
    WHERE   NOT EXISTS 
            (   SELECT  1
                FROM    Dates D2
                WHERE   D.StartDate < D2.EndDate
                AND     D.EndDate > D2.EndDate
            );
    

    Example on SQL Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a @NamedQuery that will contain the following correlated subquery: @NamedQuery(name
I am trying to perform a query in grails that orders by multiple columns
I perform a simple query like this, but loose all the objects that do
I perform this kind of query in my Python code to conn = rdbms.connect(instance=_INSTANCE_NAME,
I am trying to perform an automatic login when the user clicks a link
Both perform the same operation that is MSDN Singleton: Single Call objects service one
I perform some calculations, based on the result, I would like to either use
I want to perform an insert into the database so that the database table
I am trying to perform a mapreduce job using the Python MRJob lib and
I am trying to perform a cURL request (either directly from shell or via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.