SQL GROUP BY
Now we return to the aggregate functions. Remember we used the SUM keyword to calculate the total sales for all stores? What if we want to calculate the total sales for each store? Well, we need to do two things: First, we need to make sure we
SELECT "column_name1", SUM("column_name2")
In our example, table Store_Information,
Table Store_Information
we would key in,
SELECT store_name, SUM(Sales)
Result:
The GROUP BY keyword is used when we are selecting multiple columns from a table (or tables) and at least one arithematic operator appears in the SELECT statement. When that happens, we need to GROUP BY all the other selected columns, i.e., all columns except the one(s) operated on by the arithematic operator.
|