MySQL Tutorial: the usage of GroupBy is now back to the function. Remember to use the SUM command to calculate all Sales (turnover! What should we do if we want to calculate the turnover (sales) of each store (store_name? In this case, we need to do two things: first, we need to select both store_name and Sales columns. Second, we need to confirm that all sales are calculated separately based on each store_name. The syntax is:
SELECT "column 1", SUM ("column 2") FROM "table name" group by "column 1"
SELECT store_name, SUM (Sales) FROM Store_Information group by store_name
In our demonstration,
Store_Information table
Store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
Let's get
SELECT store_name, SUM (Sales) FROM Store_Information group by store_name
Result:
Store_name SUM (Sales)
Los Angeles $1800
San Diego $250
Boston $700
When we select more than one column and at least one column contains the function, we need to use the group by command. In this case, we need to determine that we have all other columns of group. In other words, we need to put all the columns including functions in the group by clause.