
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Example of SQL query describing COUNT function and GROUP BY
Problem: Write a query on TRANSACTIONS DB2 table to list down the number of orders (ORDER_ID) assigned to a particular transaction (TRANSACTION_ID).
Solution
We can use the below query to find out the number of orders assigned to a particular transaction id on TRANSACTIONS DB2 table.
Example
SELECT TRANSACTION_ID, COUNT(ORDER_ID) FROM TRANSACTIONS GROUP BY TRANSACTION_ID
We will use GROUP BY function on the ORDER_ID to fetch the result order wise. The COUNT function will count the number of orders. For example, we have below DB2 ORDERS table.
TRANSACTION_ID | ORDER_ID |
IRN22345 | A23118 |
IRN22345 | A45901 |
IRN22345 | A67990 |
IRN56902 | A23119 |
IRN99781 | A67921 |
IRN56902 | A23167 |
The result of our DB2 query will return the below result.
TRANSACTION_ID | COUNT(ORDER_ID) |
IRN22345 | 3 |
IRN56902 | 2 |
IRN99781 | 1 |
- Related Articles
- SQL query describing usage of SUM aggregate function and GROUP-BY with HAVING
- What is the SQL query describing usage of MAX aggregate function and GROUP-BY with HAVING?
- Example of SQL query describing the conditional processing
- Explain SQL describing COUNT aggregate and CURRENT DATE function
- MySQL query to group results by date and display the count of duplicate values?
- Difference Between Group By and Order By in SQL
- Using GROUP BY and COUNT in a single MySQL query to group duplicate records and display corresponding max value
- MySQL- GROUP and COUNT by date?
- MySQL query to group by names and display the count in a new column
- MongoDB aggregation framework with group query example?
- How can we know the repetition of a value in column with the help of group function COUNT(*) and GROUP BY clause?
- MongoDB SELECT COUNT GROUP BY?
- Difference between SQL(Structured Query Language) and T-SQL(Transact-SQL).
- MongoDB query to group by _id
- MongoDB Group query to get the count of repeated marks in documents?

Advertisements