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 8178595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:48:23+00:00 2026-06-06T23:48:23+00:00

Just got a small question. When trying to get a single max-Value of a

  • 0

Just got a small question. When trying to get a single max-Value of a table. Which one is better?

SELECT MAX(id) FROM myTable WHERE (whatever)

or

SELECT TOP 1 id FROM myTable WHERE (whatever) ORDER BY id DESC

I’m using Microsoft SQL Server 2012

  • 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-06T23:48:24+00:00Added an answer on June 6, 2026 at 11:48 pm

    There will be no difference as you can test yourself by inspecting the execution plans. If id is the clustered index, you should see an ordered clustered index scan; if it is not indexed, you’ll still see either a table scan or a clustered index scan, but it won’t be ordered in either case.

    The TOP 1 approach can be useful if you want to pull along other values from the row, which is easier than pulling the max in a subquery and then joining. If you want other values from the row, you need to dictate how to deal with ties in both cases.

    Having said that, there are some scenarios where the plan can be different, so it is important to test depending on whether the column is indexed and whether or not it is monotonically increasing. I created a simple table and inserted 50000 rows:

    CREATE TABLE dbo.x
    (
      a INT, b INT, c INT, d INT, 
      e DATETIME, f DATETIME, g DATETIME, h DATETIME
    );
    CREATE UNIQUE CLUSTERED INDEX a ON dbo.x(a);
    CREATE INDEX b ON dbo.x(b)
    CREATE INDEX e ON dbo.x(e);
    CREATE INDEX f ON dbo.x(f);
    
    INSERT dbo.x(a, b, c, d, e, f, g, h)
    SELECT 
      n.rn, -- ints monotonically increasing
      n.a,  -- ints in random order
      n.rn, 
      n.a, 
      DATEADD(DAY, n.rn/100, '20100101'), -- dates monotonically increasing
      DATEADD(DAY, -n.a % 1000, '20120101'),     -- dates in random order
      DATEADD(DAY, n.rn/100, '20100101'),
      DATEADD(DAY, -n.a % 1000, '20120101')
    FROM
    (
      SELECT TOP (50000) 
         (ABS(s1.[object_id]) % 10000) + 1, 
         rn = ROW_NUMBER() OVER (ORDER BY s2.[object_id])
      FROM sys.all_objects AS s1 
      CROSS JOIN sys.all_objects AS s2
    ) AS n(a,rn);
    GO
    

    On my system this created values in a/c from 1 to 50000, b/d between 3 and 9994, e/g from 2010-01-01 through 2011-05-16, and f/h from 2009-04-28 through 2012-01-01.

    First, let’s compare the indexed monotonically increasing integer columns, a and c. a has a clustered index, c does not:

    SELECT MAX(a) FROM dbo.x;
    SELECT TOP (1) a FROM dbo.x ORDER BY a DESC;
    
    SELECT MAX(c) FROM dbo.x;
    SELECT TOP (1) c FROM dbo.x ORDER BY c DESC;
    

    Results:

    enter image description here

    The big problem with the 4th query is that, unlike MAX, it requires a sort. Here is 3 compared to 4:

    enter image description here

    enter image description here

    This will be a common problem across all of these query variations: a MAX against an unindexed column will be able to piggy-back on the clustered index scan and perform a stream aggregate, while TOP 1 needs to perform a sort which is going to be more expensive.

    I did test and saw the exact same results across testing b+d, e+g, and f+h.

    So it seems to me that, in addition to producing more standards-compliance code, there is a potential performance benefit to using MAX in favor of TOP 1 depending on the underlying table and indexes (which can change after you’ve put your code in production). So I would say that, without further information, MAX is preferable.

    (And as I said before, TOP 1 might really be the behavior you’re after, if you’re pulling additional columns. You’ll want to test MAX + JOIN methods as well if that’s what you’re after.)

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

Sidebar

Related Questions

I just got out of a small project and I've tried to follow a
I've got a small program that displays third-party generated HTML pages. It's really just
I just started using Sequel in a really small Sinatra app. Since I've got
i just got a call from Hosting company, they claimed that my application creates
I just got an email from twitter saying my keys have now been enabled
I have a general IO question. I was trying to replace a single line
I got a small question with a big background. Let me ask a short
I got a small question about Maven. I got a project my pom.xml It
This is just a small question, more aimed at understanding the usage of arrays
Just got the new iPad, and when I touch the microphone and say a

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.