5.4

Improving the GROUP BY Query

Improving the GROUP BY Query

  • The report would be nicer if we showed the category name instead of the category_id. This will require joining the product table to the category table.
  • We can ROUND the AVG list price by category to TWO decimals points.
  • We can CONCAT the dollar sign to the left of the list_price.

Code Sample:

USE bike;
SELECT category_name, 
    CONCAT('$', ROUND(AVG(list_price),2)) AS 'Average List Price'
FROM product p
    JOIN category c
    ON p.category_id = c.category_id
GROUP BY category_name
ORDER BY category_name;

Output:

ag_03.png

USE bike:

SELECT category_name,

     CONCAT('$', ROUND(AVG(list_price),2)) AS 'Average List Price'

FROM product p

     JOIN category c

    ON p.category_id = c.category_id

GROUP BY category_name

ORDER BY category_name;

CC BY-NC-ND International 4.0

CC BY-NC-ND International 4.0: This work is released under a CC BY-NC-ND International 4.0 license, which means that you are free to do with it as you please as long as you (1) properly attribute it, (2) do not use it for commercial gain, and (3) do not create derivative works.

End-of-Chapter Survey

: How would you rate the overall quality of this chapter?
  1. Very Low Quality
  2. Low Quality
  3. Moderate Quality
  4. High Quality
  5. Very High Quality
Comments will be automatically submitted when you navigate away from the page.
Like this? Endorse it!