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

  • Home
  • SEARCH
  • 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 626143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:23:11+00:00 2026-05-13T19:23:11+00:00

I am getting and calculating some basic order information in my SQL query. I

  • 0

I am getting and calculating some basic order information in my SQL query. I have it working as it should but have been reading about the GROUP BY SQL Clause. I am wondering if the following SQL statement would benefit from GROUP BY and if it would be more efficient to use it? Thanks!

SELECT orders.billerID, 
orders.invoiceDate, 
orders.txnID, 
orders.bName, 
orders.bStreet1, 
orders.bStreet2, 
orders.bCity, 
orders.bState, 
orders.bZip, 
orders.bCountry, 
orders.sName, 
orders.sStreet1, 
orders.sStreet2, 
orders.sCity, 
orders.sState, 
orders.sZip, 
orders.sCountry, 
orders.paymentType, 
orders.invoiceNotes, 
orders.pFee, 
orders.shipping, 
orders.tax, 
orders.reasonCode, 
orders.txnType, 
orders.customerID, 
customers.firstName AS firstName, 
customers.lastName AS lastName, 
customers.businessName AS businessName, 
orderStatus.statusName AS orderStatus, 
(IFNULL(SUM((orderItems.itemPrice * orderItems.itemQuantity)), 0.00) + orders.shipping + orders.tax) AS orderTotal, 
((IFNULL(SUM((orderItems.itemPrice * orderItems.itemQuantity)), 0.00) + orders.shipping + orders.tax) - (SELECT IFNULL(SUM(payments.amount), 0.00) FROM payments WHERE payments.orderID = orders.id)) AS orderBalance 
FROM orders 
LEFT JOIN customers ON orders.customerID = customers.id 
LEFT JOIN orderStatus ON orders.orderStatus = orderStatus.id
LEFT JOIN orderItems ON orderItems.orderID = orders.id 
LEFT JOIN payments ON payments.orderID = orders.id
  • 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-13T19:23:11+00:00Added an answer on May 13, 2026 at 7:23 pm

    GROUP BY would probably allow the SQL engine to better optimize your query but would make it harder to read due to the large number of grouping parameters.

    Another option as recommended by the SQL Team is to consider using Sub queries. This can often make the GROUP BY statements much simpler and makes the overall query much easier to read.

    Using a Sub query:

    SELECT orders.billerID, 
        orders.invoiceDate, 
        orders.txnID, 
        orders.bName, 
        orders.bStreet1, 
        orders.bStreet2, 
        orders.bCity, 
        orders.bState, 
        orders.bZip, 
        orders.bCountry, 
        orders.sName, 
        orders.sStreet1, 
        orders.sStreet2, 
        orders.sCity, 
        orders.sState, 
        orders.sZip, 
        orders.sCountry, 
        orders.paymentType, 
        orders.invoiceNotes, 
        orders.pFee, 
        orders.shipping, 
        orders.tax, 
        orders.reasonCode, 
        orders.txnType, 
        orders.customerID, 
        customers.firstName AS firstName, 
        customers.lastName AS lastName, 
        customers.businessName AS businessName, 
        orderStatus.statusName AS orderStatus, 
        orderItem.fees + orders.shipping + orders.tax AS orderTotal, 
        orderItem.fees + orders.shipping + orders.tax - payments.amount AS orderBalance 
    FROM orders 
    LEFT JOIN customers ON orders.customerID = customers.id 
    LEFT JOIN orderStatus ON orders.orderStatus = orderStatus.id
    LEFT JOIN 
        ( 
          SELECT orderID, SUM(itemPrice * itemQuantity) as fees
          FROM orderItems
          GROUP BY orderID
        ) orderItems ON orderItems.orderID = orders.id 
    LEFT JOIN 
        ( 
          SELECT orderID, SUM(amount) as amount
          FROM payments
          GROUP BY orderID
        ) payments ON payments.orderID = orders.id
    

    Using a GROUP BY:

    SELECT orders.billerID, 
        orders.invoiceDate, 
        orders.txnID, 
        orders.bName, 
        orders.bStreet1, 
        orders.bStreet2, 
        orders.bCity, 
        orders.bState, 
        orders.bZip, 
        orders.bCountry, 
        orders.sName, 
        orders.sStreet1, 
        orders.sStreet2, 
        orders.sCity, 
        orders.sState, 
        orders.sZip, 
        orders.sCountry, 
        orders.paymentType, 
        orders.invoiceNotes, 
        orders.pFee, 
        orders.shipping, 
        orders.tax, 
        orders.reasonCode, 
        orders.txnType, 
        orders.customerID, 
        customers.firstName AS firstName, 
        customers.lastName AS lastName, 
        customers.businessName AS businessName, 
        orderStatus.statusName AS orderStatus, 
        SUM(orderItems.itemPrice * orderItems.itemQuantity) + orders.shipping + orders.tax AS orderTotal, 
        SUM(orderItems.itemPrice * orderItems.itemQuantity) + orders.shipping + orders.tax - SUM(payments.amount) AS orderBalance 
    FROM orders 
    LEFT JOIN customers ON orders.customerID = customers.id 
    LEFT JOIN orderStatus ON orders.orderStatus = orderStatus.id
    LEFT JOIN orderItems ON orderItems.orderID = orders.id 
    LEFT JOIN payments ON payments.orderID = orders.id
    GROUP BY 
        orders.billerID, 
        orders.invoiceDate, 
        orders.txnID, 
        orders.bName, 
        orders.bStreet1, 
        orders.bStreet2, 
        orders.bCity, 
        orders.bState, 
        orders.bZip, 
        orders.bCountry, 
        orders.sName, 
        orders.sStreet1, 
        orders.sStreet2, 
        orders.sCity, 
        orders.sState, 
        orders.sZip, 
        orders.sCountry, 
        orders.paymentType, 
        orders.invoiceNotes, 
        orders.pFee, 
        orders.shipping, 
        orders.tax, 
        orders.reasonCode, 
        orders.txnType, 
        orders.customerID, 
        customers.firstName, 
        customers.lastName, 
        customers.businessName, 
        orderStatus.statusName  
    

    GROUP BY Explained:

    You can thing of GROUP BY as collecting records together that have similar data. For my example I am going to use a simple produce table with Category, Name and Price columns. If I group the data by Category I can aggregate ( i.e. SUM, COUNT, MIN, MAX, etc.) based on any of the other columns. Since I am grouping by the Category column the resulting records will have a unique value for Category. Any of the other columns might be return different value and therefore cannot be included in the select statement.

    Name, Category, Price
    Green Peppers, Peppers, 1.50
    Orange Peppers, Peppers, 2.50
    Yellow Peppers, Peppers, 2.50
    Lemons, Citrus, 1.00
    Oranges, Citrus, 1.00
    Limes, Citrus, 1.00

    SELECT 
        Category, /* This is unique because it is in the GROUP BY clause */
        AVG(Price) AS AveragePrice,
        MAX(Price) AS MaxPrice,
        MIN(Price) AS MinPrice
        /* , Name */  /* This is invalid because it is not in the GROUP BY clause */
                      /* The values are not unique so SQL does not know what to return */
    FROM Produce
    GROUP BY Category
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you pass an array as a pointer, you lose… May 14, 2026 at 6:00 pm
  • Editorial Team
    Editorial Team added an answer It's hard to tell which column goes with which table,… May 14, 2026 at 6:00 pm
  • Editorial Team
    Editorial Team added an answer MethodToMock is not virtual and therefore can't be mocked. What… May 14, 2026 at 6:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.