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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:41:29+00:00 2026-06-06T20:41:29+00:00

Recently in the code of my collegue I saw an sql query, where she

  • 0

Recently in the code of my collegue I saw an sql query, where she used GROUP BY with lots of columns. Most of these columns needn’t be grouped in the query. She has done this to prevent this error:

Column ‘some_col’ is invalid in the select list because it is
not contained in either an aggregate function or the GROUP BY clause.

I was wondering how heavy GROUP BY is, and is it ok to use such statements? If it is heavy than I’d better optimize her the query cause now I work on that piece of code.

  • 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-06T20:41:30+00:00Added an answer on June 6, 2026 at 8:41 pm

    It is hard to tell for sure without seeing the specific query, but I used to achieve surprising performance gains (at leas in SQL2K) by minimizing number of columns included in GROUP BY, and resolving those columns back with join on the inner query. To be more specific: let’s assume you have classing OrderDetails (OrderID, ProductID, Quantity, Price) and Products (ProductID, ProductName) tables. Changing this query:

    select P.ProductID, ProductName, sum(Quantity * Price)
    from Products as P
      inner join OrderDetails as OD on P.ProductID = OD.ProductID
    group by P.ProductID, ProductName
    

    to this:

    select X.ProductID, PP.ProductName, X.OrderValue
    from
      (
        select P.ProductID, sum(Quantity * Price) as OrderValue
        from Products as P
           inner join OrderDetails as OD on P.ProductID = OD.ProductID
        group by P.ProductID
      ) as X
        inner join Products as PP on X.ProductID = P.ProductID
    

    would give me performance gain despite two joins to the same table, because grouping on integer index was faster then grouping on text-valued, unsorted product name.

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

Sidebar

Related Questions

I recently saw some code from a college teacher where he had something like
I recently saw code that looks like this: (function (someGlobal) { someGlobal.DoSomething(); })(someGlobal); where
I came across some code recently that used a custom error handler to turn
I saw recently following code: #define MY_ASSERT_CONCAT_(a, b) a##b #define MY_ASSERT_CONCAT(a, b) MY_ASSERT_CONCAT_(a, b)
I'm writing a lot of python code recently, and i used the tab-to-space mode
I'm learning NHibernate and was looking at some code recently where NHibernate was used
Saw an interesting piece of code recently, and my mind made a snap judgement
I had to delve into some VB6 code recently and I saw this pattern
Iv recently written code that will help me export an SQL database into CSV
I recently wrote code that didnt work as i would expect, it was: message

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.