So far, we have gone over the SELECT, FROM and WHERE clauses. These three clauses are used to control what we are seeing in a view. What if we want to control how what we are searching for appears in the view, that is where GROUP BY comes in. The GROUP BY clause is used to arrange how records will appear in a view and does this by organizing the data that has an identical value for a particular column in a group, hence the name. If we go back to the customer-transaction table as an example, if a customer made several transactions across the history of the table these transactions may be scattered throughout the view; GROUP BY allows us to package all of these transactions together by customer.
The GROUP BY clause is placed after the WHERE clause, think “Look at these columns, from this table, where the total amount is more than 100, and group it by the customer”. Let’s introduce a new example with a different table.
You’ll notice that there are two columns in the GROUP BY statement. What this is saying is to group by the ProductID and then the SalesOrderID. This query lets us see a view that shows which order each particular product was assigned to and is grouped by the product.
Click here for Part 4: ORDER BY statement