SQL GROUP BY Query


The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

Syntax

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Example

sales Table

order_idorder_dateorder_priceorder_quantitycust_name
112/22/20051602Ashwani
208/10/20051902Soun
307/13/20055005Sachin
407/15/20054202Ashwani
512/22/200510004Deepak
610/2/20058204Ashwani
711/03/200520002Sachin
SELECT cust_name, SUM(order_price) FROM sales
GROUP BY cust_name
Output
cust_nameorder_price
Sachin2500
Soun190
Ashwani1400
Deepak1000

Share this article on