SQL GROUP BY Syntax
Defination
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
Example
Group by on all table.
SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder
GROUP BY CustomerName
Group by filtered table.
SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder
where CustomerName like 'JO%'
GROUP BY CustomerName
Same result but using Having.
SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder
GROUP BY CustomerName
having CustomerName like 'JO%'
Using Having to query on aggregate.
SELECT CustomerName,SUM(CustomerOrder) FROM CustomerOrder
GROUP BY CustomerName
having sum(CustomerOrder) > 4
Group By more than 2 column.
SELECT CustomerName,OrderDate,SUM(CustomerOrder) FROM CustomerOrder
GROUP BY CustomerName,OrderDate