AdBlock Detected!
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.
SQL GROUP BY |
|
SQL > SQL Commands >
Group By
The GROUP BY clause is used to tell SQL what level of granularity the aggregate function should be calculated in. The level of granularity is represented by the columns in the SELECT statement that are not aggregate functions.
Key Takeaway: GROUP BY collapses multiple rows with the same value into one summary row and applies aggregate functions like SUM and COUNT to each group. Every non-aggregate column in SELECT must be in GROUP BY.
SyntaxThe syntax for GROUP BY is, More than one column can be specified in the GROUP BY clause, and more than one function can be included. GROUP BY is a command that can trip up many beginners, as it is often possible to have a SQL statement with the correct GROUP BY syntax, yet get the wrong results. A good rule of thumb when using GROUP BY is to include all the non-aggregate function columns in the SELECT statement in the GROUP BY clause. ExamplesWe use the following table for our examples. Table Store_Information
Example 1: GROUP BY a single columnWe want to find total sales for each store. To do so, we would key in, Result:
Example 2: GROUP BY multiple columnsIn Example 1, there is only one column associated with GROUP BY. It is possible to have two or more columns associated with GROUP BY. We want to find total sales for each product at each store. To do so, we would key in, Result:
Example 3: GROUP BY multiple columns and multiple functionsWe want to find total sales and the average sales for each product at each store. To do so, we would key in, Result:
Example 4: Group by month / date / weekA common use of the GROUP BY function is on a time period, which can be month, week, day, or even hour. This type of query is often combined with the ORDER BY keyword to provide a query result that shows a time series. For example, to find total daily sales from Store_Information, we use the following SQL: Result:
ExercisesFor these exercises, assume we have a table called Region_Sales with the following data: Table Region_Sales
1. Which of the following order is correct for a SQL statement?
2. Write a SQL statement that calculates the total dollar sales amount for each region. What is the result? 3. Write a SQL statement that calculates the average annual dollar sales amount for just the East region and the West region. What is the result?
Frequently Asked QuestionsWhat error do I get if I forget GROUP BY?If you use an aggregate function alongside non-aggregated columns without GROUP BY, most databases throw an error like "column must appear in the GROUP BY clause or be used in an aggregate function." Can I use WHERE with GROUP BY?Yes. WHERE filters rows before they are grouped. The order is: WHERE (filter rows) → GROUP BY (group remaining rows) → HAVING (filter groups). Both WHERE and HAVING can appear in the same query. Can I GROUP BY an expression or calculated column?Yes. For example, How is GROUP BY different from DISTINCT?DISTINCT removes duplicate rows from the result. GROUP BY is used for aggregation — it collapses rows into groups and allows you to apply functions like SUM or COUNT to each group. |
Our website is made possible by displaying ads to our visitors. Please supporting us by whitelisting our website.