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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:14:44+00:00 2026-05-26T00:14:44+00:00

I have these queries: SELECT a.Id, a.Name, (SELECT COUNT(Id) FROM b WHERE b.IdTableA =

  • 0

I have these queries:

SELECT a.Id, a.Name, (SELECT COUNT(Id) FROM b WHERE b.IdTableA = a.Id) AS Num
FROM a
ORDER BY a.Name

table b has a FK on table a (IdTableA)
In this case, is it efficient? Is there any other way to do this?

The other question is:

SELECT client.Id, client.Name
     ,(SELECT SUM(projects) FROM projects WHERE IdClient = client.Id) AS projects
FROM client

What about this one?

Sometimes we need to use more than one calculated column (SELECT SUM), even 10 or 15.

We very are worried about performance since the table projects could have more than 500K records.

I’ve read that storing those SUMS in a table and update that table when the data changes could be better for performance. But this goes against normalization…

Please help me with both queries…

Thanks

  • 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-26T00:14:45+00:00Added an answer on May 26, 2026 at 12:14 am
    SELECT a.Id, a.Name, (SELECT COUNT(Id) FROM b WHERE b.IdTableA = a.Id) AS Num
    FROM a
    ORDER BY a.Name
    

    can possible be rewritten as

    SELECT a.Id, a.Name, COUNT(b.Id) AS Num
    FROM a JOIN b ON b.IdTableA = a.Id 
    GROUP BY a.Id, a.Name
    ORDER BY a.Name
    

    which carries less risk of being wrongly executed by MySQL.

    Storing sums of data for easy retrieval is acceptable when you have a lot more reads than writes (or when writes are allowed to be slow, but reads have to be fast). Usually you use a data-warehouse for this though: the warehouse stores the aggregate data, and your OLTP database stores the individual rows.

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

Sidebar

Related Questions

I have these tables: customer -------- customer_id int name varchar(255) order ----- order_id int
I have two different queries: SELECT PB_BANK_CODE, PB_BANK_NAME FROM GLAS_PDC_BANKS WHERE PB_COMP_CODE='1' AND PB_BANK_CODE='025'
I have a table called baskets with these columns: basket (name of the basket)
I have a Users table with these values (stripped down for the example): Name
I have a 5×7 HTML table. On many queries, there are fewer than 35
I have 3 table: [Order, Order_to_goods, Goods] I want to get of all participants
I usually use sql parameters with queries, but in this case I need to
I have a MEMBERS table with the following relevant columns: Name JoinDate Level --1=Gold,2=Silver,3=Bronze**
I have a saved query in MS Access 2007 named test. SELECT Count(system_info.id) AS
Let's say I have the table NAME | ID | REF foo1 | 1

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.