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

  • SEARCH
  • Home
  • 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 579131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:20:03+00:00 2026-05-13T14:20:03+00:00

My previous question: Date of max id: sql/oracle optimization In my previous question, I

  • 0

My previous question:

Date of max id: sql/oracle optimization

In my previous question, I was finding different ways of finding the date of the record with the highest id number. Below are several of the offered solutions, and their ‘cost’ as calculated by explain plan.

select date from table where id in (
select max(id) from table)

has a cost of 8

select date from table where rownum < 2 order by id desc;

has a cost of 5

select date from (select date from table order by id desc) where rownum < 2;

also has a cost of 5

with ranked_table as (select rownum as rn, date from table order by id desc)
  select date from ranked_table where rn = 1;

has a cost of 906665

SELECT t1.date
FROM table t1
LEFT OUTER JOIN table t2
  ON t1.id < t2.id
WHERE t2.id IS NULL;

has a cost of 1438619

Obviously the index on id is doing its job. But I was wondering, in what cases would the last two perform at least as well, if not better? I want to understand the benefits of doing it that way.

This was done in Oracle. All varieties can be discussed, but kindly say what your answer applies to.

  • 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-05-13T14:20:03+00:00Added an answer on May 13, 2026 at 2:20 pm

    Use solution #1 if you want the most portable SQL that will work on a wide variety of other brands of RDBMS (i.e. not all brands support rownum):

    select date from table where id in (select max(id) from table);
    

    Use solution #3 if you want the most efficient solution for Oracle:

    select date from (select date from table order by id desc) where rownum < 2;
    

    Note that solution #2 doesn’t always give the right answer, because it returns the “first” two rows before it has sorted them by id. If this happens to return the rows with the highest id values, it’s only by coincidence.

    select date from table where rownum < 2 order by id desc;
    

    Regarding the more complex queries #4 and #5 that give such a high cost, I agree I wouldn’t recommend using them for such a simple task as fetching the row with the highest id. But understanding how to use subquery factoring and self-joins can be useful for solving other more complex types of queries, where the simple solutions simply don’t do the job.

    Example: given a hierarchy of threaded forum comments, show the “hottest” comments with the most direct replies.

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

Sidebar

Related Questions

From a previous question , I learn that Linq to SQL is not able
This question is directly related to my previous question ASP.NET AJAX Is it possible
this question is an extension to a previous question i asked (and was answered).
This question is a follow up to my previous question about getting the HTML
Continuing from my previous question , is there a comprehensive document that lists all
Going back to my previous question on OCSP, does anybody know of reliable OCSP
In a previous question, I asked about various ORM libraries. It turns out Kohana
From a previous question I have seen that the CLR has workstation and server
We determined in a previous question that many features of HTML SELECTs are not
In a previous question about overriding Time.now , I was working toward a solution

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.