GROUP BY Clause in SQL
By S.Qasim
Published: 14 May 2013
Clauses of Select Statment in SQL
GROUP BY Clause in SQL
GROUP BY clause in SQl is used for arrange the same type of data in to groups. GROUP BY clause is used in SELECT statement with aggregate functions .
For example if we have a following table with some payment Record of customer:
| customer_name | city | Payments |
| Tauqeer | Karachi | 50000 |
| Zia | Lahore | 20000 |
| Noman | Karachi | 30000 |
| Tauqeer | Karachi | 10000 |
| Noman | Karachi | 15000 |
| Zia | Lahore | 30000 |
Now from this table if we want to calculate and display the total payment of each customer then we use SELECT statement with SUM function and GROUP BY Clause. Where SUM Function calculate the sum of payment column by making GROUP of column customer_name.
SELECT customer_name, SUM(payments) AS Net_Payments FROM payments
GROUP BY customer-name ;
The above statement produced the following result :
| customer_name | Net_Payments |
| Tauqeer | 60000 |
| Zia | 50000 |
| Noman | 45000 |