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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:17:50+00:00 2026-05-12T07:17:50+00:00

I want to be able to take any arbitrary SELECT TOP(X) query that would

  • 0

I want to be able to take any arbitrary SELECT TOP(X) query that would normally return a large number of rows (without the X limit) and transform that query into a query that counts how many rows would have been returned without the TOP(X) (i.e. SELECT COUNT(*)). Remember I am asking about an arbitrary query with any number of joins, where clauses, group by’s etc.

Is there a way to do this?


edited to show syntax with Shannon’s solution:

i.e.

`SELECT TOP(X) [colnames] FROM [tables with joins] 
 WHERE [constraints] GROUP BY [cols] ORDER BY [cols]`

becomes

`SELECT COUNT(*) FROM 
 (SELECT [colnames] FROM [tables with joins] 
 WHERE [constraints] GROUP BY [cols]) 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-05-12T07:17:50+00:00Added an answer on May 12, 2026 at 7:17 am

    Inline view:

    select count(*)
    from (...slightly transformed query...) t
    

    … slightly transfomed query… is:

    1. If the select clause contains any columns without names, such as select ... avg(x) ... then do one of 1) Alias the column, such as avg(x) as AvgX, 2) Remove the column, but make sure at least one column is left, or my favorite 3) Just make the select clause select 1 as C
    2. Remove TOP from select clause.
    3. Remove order by clause.

    EDIT 1 Fixed by adding aliases for the inline view and dealing with unnamed columns in select clause.

    EDIT 2 But what about the performance? Doesn’t this require the DB to run the big query that I wanted to avoid in the first place with TOP(X)?

    Not necessarily. It may be the case for some queries that this count will do more work than the TOP(x) would. And it may be the case that for a particular query, you could make the equivelent count faster by making addional changes to remove work that is not needed for the final count. But those simplifications can not be included in a general method to take any arbitrary SELECT TOP(X) query that would normally return a large number of rows (without the X limit) and transform that query into a query that counts how many rows would have been returned without the TOP(X).

    And in some cases, the query optimizer may optimize away stuff so that the DB is not to run the big query.

    For example Test table & data, using SQL Server 2005:

    create table t (PK int identity(1, 1) primary key,
      u int not null unique,
      string VARCHAR(2000))
    
    insert into t (u, string)
    select top 100000 row_number() over (order by s1.id) , replace(space(2000), ' ', 'x')
    from sysobjects s1, 
        sysobjects s2, 
        sysobjects s3, 
        sysobjects s4, 
        sysobjects s5, 
        sysobjects s6, 
        sysobjects s7
    

    The non-clustered index on column u will be much smaller than the clustered index on column PK.

    Then set up SMSS to show the actual execution plan for:

    select PK, U, String from t
    select count(*) from t
    

    The first select does a clusted index scan, because it needs to return data out of the leafs. The second query does an index scan on the smaller non-clusteed index created for the unique constraint on U.

    Applying the transform of the first query we get:

    select count(*)
    from (select PK, U, String from t) t
    

    Running that and looking at the plan, the index on U is used again, exact same plan as select count(*) from t. The leaves are not visited to find the values for String on every row.

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

Sidebar

Related Questions

I want to be able to take still images with a web cam, via
In a Java application I want to be able to take a timestamp at
I have a .Net 2.0/3.5 WebApplication. I want to be able to take money
I really want to be able to have a way to take an app
I want to be able to capture the exception that is thrown when a
I want to be able to make an HTTP call updating some select boxes
I want to be able to take, as input, a character pointer to a
I want to be able to take a string of text from the user
I want to be able to do: For Each thing In things End For
I want to be able to play sound files in my program. Where should

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.