Aggregates of Aggregates

  • 02/11/2023
  • SQL
No Comments

¨  Always take note when computing aggregates of

aggregates!

“Find the largest total account balance of any branch.”

🞑 Two nested aggregates: max of sums

¨  A very common mistake:

SELECT branch_name, SUM(balance) AS tot_bal

FROM account GROUP BY branch_name

HAVING tot_bal = MAX(tot_bal)

🞑 A SELECT query can only perform one level of aggregation

🞑 Need a second SELECT to find the maximum total

🞑 Unfortunately, MySQL accepts this and returns bogus result

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts
No Comments

Recent Posts