- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SQL query describing usage of SUM aggregate function and GROUP-BY with HAVING
Problem: Write the DB2 SQL query to give the sum of ORDER_TOTAL for the orders placed on 29th July and 30th July individually. The result should come in a single table.
Solution
We can find the sum of ORDER_TOTAL for the orders placed on 29th and 30th July individually using aggregate function SUM, GROUP BY and HAVING.
For example, if we have an ORDER table as below.
ORDER_ID | ORDER_TOTAL | ORDER_DATE |
Z22345 | 342 | 29-07-2020 |
Z62998 | 543 | 30-07-2020 |
Z56990 | 431 | 12-07-2020 |
Z56902 | 6743 | 29-07-2020 |
Z99781 | 443 | 10-07-2020 |
Z56112 | 889 | 30-07-2020 |
Below is the query which will give the desired result.
Example
SELECT ORDER_DATE, SUM(ORDER_TOTAL) FROM ORDERS GROUP BY ORDER_DATE HAVING ORDER_DATE IN (‘29-07-2020’, ‘30-07-2020’)
In this query, we have selected ORDER_DATE and ORDER_TOTAL with aggregate function SUM.
The GROUP BY will ensure that the sum of ORDER_TOTAL is taken date wise and HAVING clause will ensure that the result of only 29th and 30th July is displayed. The query will give the following result.
ORDER_DATE | ORDER_TOTAL |
29-07-2020 | 7085 |
30-07-2020 | 1432 |
- Related Articles
- What is the SQL query describing usage of MAX aggregate function and GROUP-BY with HAVING?
- Example of SQL query describing COUNT function and GROUP BY
- Explain SQL describing COUNT aggregate and CURRENT DATE function
- Example of SQL query describing the conditional processing
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- How will GROUP BY clause perform without an aggregate function?
- HAVING with GROUP BY in MySQL
- MongoDB query to implement aggregate function
- Explain aggregate functions with the help of SQL queries
- What is the benefit of using MySQL SUM() function with GROUP BY clause?
- Difference Between Group By and Order By in SQL
- MySQL query to group by column and display the sum of similar values in another column
- To Aggregate Totals in One Group with MongoDB
- How can I aggregate collection and group by field count in MongoDB?
- Display the record with non-duplicate Id using MySQL GROUP BY and HAVING

Advertisements