Ravindra BagaleCourses & study guides Track your progress

11. Aggregate Functions: SUM COUNT AVG MIN MAX

11.5 Practice: City Totals on raja_coffee Orders

Why. City totals are the bridge into GROUP BY next chapter.

What. You can aggregate the whole table now; per-city needs GROUP BY (ch12).

How. Practice global aggregates; peek at GROUP BY city once.

Steps

  1. Global SUM/COUNT/AVG.
  2. Then GROUP BY city preview.
  3. Compare Pune vs Mumbai totals.

Examples

Example 1 · dataset: raja_coffee

SELECT city,
       COUNT(*) AS orders,
       ROUND(SUM(amount_inr),2) AS sales_inr
FROM orders
GROUP BY city
ORDER BY sales_inr DESC;

Output

cityorderssales_inr
Pune109170.0
Mumbai32490.0
Kolhapur32410.0
Nashik32320.0
Goa31600.0
Solapur2730.0

Meaning. Preview of ch12: one row per city.

Ravindra Bagale's Tip

If you select city + SUM without GROUP BY, SQLite may error — that is your cue for ch12.