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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T04:26:55+00:00 2026-06-19T04:26:55+00:00

I have a sql query, SELECT col1, col2, col3, ( SELECT COUNT(id) FROM B

  • 0

I have a sql query,

SELECT 
  col1, 
  col2, 
  col3, (
    SELECT COUNT(id) 
    FROM B 
    WHERE B.aid = A.id
  ) AS col4 
FROM A

What is the performance impact of having a select as a column? Will that statement be executed for every row that is returned?

I am really just interested in the performance of this query. I know there are other ways getting the count can be accomplished. But in this case I am only trying to understand how sql works with an inline select.

Apologies if this question is a dupe, I have looked through stackoverflow and I have not been able to find this anywhere.

  • 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-19T04:26:56+00:00Added an answer on June 19, 2026 at 4:26 am

    This depends upon your SQL implementation.

    SQL is declarative and it is up to the optimiser to construct a physical plan from the logical specification. SQL Server can decorrelate this sub query and turn into an OUTER JOIN

    CREATE TABLE A(id INT PRIMARY KEY, col1 INT, col2 INT, col3 INT)
    
    CREATE TABLE B(aid INT)
    
    CREATE CLUSTERED INDEX ix ON B(aid)
    
    /*Fool optimiser into thinking tables aren't empty*/
    update statistics A with rowcount = 1000000, pagecount = 100000
    
    update statistics B with rowcount = 1000000, pagecount = 100000
    
    SELECT col1,
           col2,
           col3,
           (SELECT count( B.aid)
            FROM   B
            WHERE  B.aid = A.id) AS col4
    FROM   A 
    
    DROP TABLE A, B 
    

    Gives plan

    Plan

    Which is basically the same as

    SELECT col1,
           col2,
           col3,
           Cnt
    FROM   (SELECT COUNT(aid) AS Cnt,
                   aid
            FROM   B
            GROUP  BY aid) T
           RIGHT OUTER JOIN A
             ON A.id = T.aid 
    

    The sub query is logically represented as a RIGHT OUTER JOIN with MERGE JOIN as the physical implementation. The merge join processes each input once rather than the row by row behaviour of a nested loops join.

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

Sidebar

Related Questions

I have the following query: SELECT col1, col2, col3 FROM tb1 WHERE col4=ANY( SELECT
I have a query like queryStr := 'SELECT col1, col2, col3, col4 FROM Table1
I have an SQL query like SELECT Col1, Col2 FROM Table1 UNION ALL SELECT
I have an sql query like this Select col1, (select abc from table2 where
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
i have this sql query select * from table where name like ? but
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have a simple SQL query: Select ID, COUNT(ID) as Selections, OptionName, SUM(Units) as
CREATE VIEW View1 AS SELECT Col1, Col2, dbo.fn1(col2) as Col3 FROM TestTable /* --Instead
I have a SQL Query: SELECT documents.*, t_rights.rights, documents_list.docs FROM documents INNER JOIN t_rights

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.