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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:32:06+00:00 2026-06-13T02:32:06+00:00

I want to select max Id from nested select. Here is the my query:

  • 0

I want to select max Id from nested select.

Here is the my query:

select max(Id) from (SELECT TOP 100 Id FROM [MyTable]) t

This select should return 100, but it actually returns max id of MyTable.

P.S. MyTable contains 100 000 records, so query returns 100 000.

Here is an Example:

returns 100 (It’s ok)

SELECT TOP 100 Id FROM [MyTable]

returns 100 000, but I want to select max id of “SELECT TOP 100 Id FROM [MyTable]”

select max(Id) from (SELECT TOP 100 Id FROM [MyTable]) t

returns 100 000, but I want to select max id of “SELECT TOP 100 Id FROM [MyTable] where Id > 100”

select max(Id) from (SELECT TOP 100 Id FROM [MyTable] where Id > 100) t
  • 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-13T02:32:07+00:00Added an answer on June 13, 2026 at 2:32 am

    Queries with TOP but without ORDER BY, like yours:

    SELECT TOP 100 
        Id 
    FROM tableX ;
    

    do not return consistent results. The above means “show me 100 IDs, I don’t care which, from the millions there are in the table”. The DBMS is free to return any 100 it chooses, any time you run the query. It can even choose to send you back the same 100 for the first 2 years, then suddenly change its mind and return some other 100 of them.

    In fact, that’s what you see, when it is used a subquery, the optimizer transforms the query “give me the max of any 100 IDs” to “give me the max of all the IDs of the table”


    Change that query to (you can use any ORDER BY of your choice):

    SELECT TOP 100 
        Id 
    FROM tableX 
      ORDER BY Id ;
    

    on its own and where it is used as a subquery and you will have consistent behaviour.

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

Sidebar

Related Questions

I have this query here: $query='insert into pageview (visitor,id_realestate,time) select q17872745t, 150, now() from
select max(qtd) from (select count(int_re_usu) as qtd from tb_questionario_voar_resposta) Why this query dont work?
i have this lists, and i want select li element from jquery when the
I am running a query from the VBA editor of Access: select max(somerow) from
I am running this query SELECT sh.*, u.initials AS initals FROM database1.table1 AS sh
Say I have this great query in my stored procedure. Select * from Temp
I have small PHP script which has $query = SELECT MAX(id) FROM `dbs`; //query
The query in question is SELECT MAX(Date) as max, MIN(Date) as min FROM table
I want a query that updates the max number of the id... here is
The I kind of want to do is select max(f1, f2, f3). I know

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.