11. Aggregate Functions: SUM COUNT AVG MIN MAX
11.2 SUM and AVG on Money Columns (₹ $ £ €)
Why. Money totals and averages are daily analyst work across ₹ $ £ €.
What. SUM adds. AVG averages non-NULL values. Keep currency in the column name or filter.
How. Never sum INR and USD in one column without converting.
Steps
- SUM and AVG on raja_coffee INR.
- SUM by currency on interview_mini sales.
- Read units in the alias.
Examples
Example 1 · dataset: raja_coffee
SELECT ROUND(SUM(amount_inr),2) AS total_inr,
ROUND(AVG(amount_inr),2) AS avg_inr
FROM orders;Output
| total_inr | avg_inr |
|---|---|
| 18720.0 | 780.0 |
Meaning. Total and average order value in INR.
Example 2 · dataset: interview_mini
SELECT currency, ROUND(SUM(amount),2) AS total
FROM sales
GROUP BY currency
ORDER BY currency;Output
| currency | total |
|---|---|
| EUR | 475.0 |
| GBP | 295.0 |
| INR | 4000.0 |
| USD | 470.0 |
Meaning. Separate totals per currency — do not mix.
Ravindra Bagale's Tip
ROUND(…,2) helps money display; still store full precision when you can.
Ravindra Bagale's Tip – मराठी
ROUND(…,2) पैशाच्या display साठी; शक्य असल्यास पूर्ण precision ठेवा.
Ravindra Bagale's Tip – हिंदी
ROUND(…,2) पैसे दिखाने में मदद; जहाँ हो पूरा precision रखें।