Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
GROUP BY – Recap
ROLLUP
Summary

Instruction

Good! Now we know how well each contestant did in general (GROUP BY full_name), and also how well they did in each category (GROUP BY full_name, category). But note one thing: by adding the second column to the GROUP BY clause, we lost some information from the previous query. With the GROUP BY full_name, category clause, we know the average scores for each contestant in each category, but we are no longer able to check the overall average for each contestant.

That's where GROUP BY ROLLUP comes in handy. Take a look:

SELECT
  full_name,
  category, 
  AVG(score) AS avg_score
FROM contest_score
GROUP BY ROLLUP (full_name, category);

Note the change in the GROUP BY clause. We added the ROLLUP operator, followed by a pair of parentheses. Inside, we put full_name and category.

Let's see what changes when we use ROLLUP.

Exercise

Run the template query and note what happens.

Apart from averages by contestant and category, we can also see averages by contestant and an overall average across all contestants and categories.