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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:40:26+00:00 2026-06-14T21:40:26+00:00

If I execute the following query in SQL Server 2008 R2, will the count(*)

  • 0

If I execute the following query in SQL Server 2008 R2, will the count(*) aggregate be determined only once for OUTER SELECT query or it will repeat for every record in OUTER SELECT?

I was guessing that SQL Server would be intelligent to see that the same calculation is being repeated and so it would do this calculation only once for optimization purpose. The value of TotalCount in query below is going to be the same for all rows in outer query.

SELECT 
    p.ProductId, p.ProductName,
    (select count(*) from Products p1) as TotalCount 
FROM Products p
  • 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-14T21:40:28+00:00Added an answer on June 14, 2026 at 9:40 pm

    No, you’re expecting too much from SQL Server. Also: the query processor really cannot be sure that this value won’t be changing over time – so it cannot really “optimize” this for you.

    For every single row, this subquery will be executed once.

    So if your SELECT statement will return 10 million rows, this count will be determined 10 million times.

    If you don’t want that, you can always run the select count(*).. once before the query and store the value into a SQL variable, and select that variable in your query:

    DECLARE @TableCount INT
    
    SELECT @TableCount = COUNT(*) FROM Products
    
    SELECT 
        p.ProductId, p.ProductName, @TableCount
    FROM 
        Products p
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am executing following sql query on SQL Server 2008 using jTDS API: SELECT
I've got 2 servers running SQL Server 2008, and I have the following query:
i have the following SQL Query which runs on SQL Server CE 4 SELECT
I'm running the simple query on SQL Server 2008 R2 for the following table:
I am trying to execute the following SQL query using MS SQL Server Management
I have the following SQL query to execute in Sql Server MSSM, and it
Assuming A.sql contains the following code, then second Select query won’t be executed due
I am trying to execute following query: Insert into pligg_links_temp select * from pligg_links
I am trying to execute the following Query select distinct pincode as Pincode,CAST(Date_val as
I'm trying to debug\execute a SP from t-sql (sql server 2008) and when I

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.