5.3

Simple GROUP BY Query

Code Example:

USE bike;
SELECT category_id, AVG(list_price)
FROM product
GROUP BY category_id

Results:

ag_02.png

USE bike:

  • Set the bike database to be the default

SELECT category_id, AVG(list_price):

  • Select the category_id from the base table
  • Calculate the Average of the list price for all rows in the table


FROM product:

  • Product is the base table from which data will be returned

GROUP BY category_id:

  • Instead of returning a single value that is the average of all list_price items in the product table, return an average list_price for each category
  • Without the GROUP BY clause, we see from our first example only a single row is returned with an average list_price of 1520.591402.
  • With the GROUP BY clause, we return an average for each category_id.