🏠 Home / Hub

🗄️ SQL Lesson 04 — Aggregate Functions

← Back to SQL Menu

1. Sample Data — orders table

ဒီ lesson မှာ ဒီ table ကို သုံးမယ်:

idcustomercityproductamountqty
1Ko MinYangonPhone5002
2Ma AyeMandalayLaptop12001
3Ko MinYangonCase205
4Ko KyawBagoTablet4001
5Ma AyeMandalayCharger303
6Ko KyawBagoPhone5001

2. Core Aggregate Functions

-- COUNT — how many rows
SELECT COUNT(*) FROM orders;             -- 6
SELECT COUNT(DISTINCT customer) FROM orders; -- 3 (Ko Min, Ma Aye, Ko Kyaw)

-- SUM — total
SELECT SUM(amount) FROM orders;           -- 2650
SELECT SUM(amount * qty) FROM orders;     -- expression sum

-- AVG — average
SELECT AVG(amount) FROM orders;           -- 441.67

-- MIN / MAX
SELECT MIN(amount) FROM orders;           -- 20
SELECT MAX(amount) FROM orders;           -- 1200

-- Combine them all
SELECT
    COUNT(*) AS total_orders,
    SUM(amount) AS total_sales,
    AVG(amount) AS avg_order,
    MIN(amount) AS smallest,
    MAX(amount) AS largest
FROM orders;

Result:

total_orderstotal_salesavg_ordersmallestlargest
62650441.67201200

3. GROUP BY — Group ခွဲပြီး Aggregate

-- Orders per customer
SELECT customer, COUNT(*) AS num_orders
FROM orders
GROUP BY customer;

-- Total spent per customer
SELECT customer, SUM(amount) AS total_spent
FROM orders
GROUP BY customer
ORDER BY total_spent DESC;

-- Sales per city
SELECT city, COUNT(*) AS orders, SUM(amount) AS revenue
FROM orders
GROUP BY city;

Orders per customer:

customernum_orders
Ko Min2
Ma Aye2
Ko Kyaw2

Total spent per customer:

customertotal_spent
Ma Aye1230
Ko Min520
Ko Kyaw900

4. HAVING — Group Filter (WHERE for GROUP BY)

-- WHERE = individual rows filter (before GROUP)
-- HAVING = group result filter (after GROUP)

-- Customers who spent more than 800
SELECT customer, SUM(amount) AS total_spent
FROM orders
GROUP BY customer
HAVING total_spent > 800;

-- Cities with more than 1 order
SELECT city, COUNT(*) AS order_count
FROM orders
GROUP BY city
HAVING order_count >= 2;

-- WHERE + GROUP BY + HAVING together
SELECT customer, SUM(amount) AS total
FROM orders
WHERE city != 'Bago'        -- filter rows first
GROUP BY customer
HAVING total > 100          -- filter groups after
ORDER BY total DESC;
WHERE vs HAVING:
WHERE — GROUP ဖြစ်ခြင်မပြည့်ဘဲ row filter (aggregate function မသုံးနိုင်)
HAVING — GROUP ပြီးနောက် filter (aggregate function သုံးနိုင်)

5. Aggregate Functions Reference

Functionဘာလုပ်တယ်Example
COUNT(*)Row အရေအတွက် (NULL ပါ ရေတွက်)COUNT(*) → 6
COUNT(col)NULL မဟုတ်တဲ့ value အရေအတွက်COUNT(email) → 4
COUNT(DISTINCT col)Unique values အရေအတွက်COUNT(DISTINCT city) → 3
SUM(col)ပေါင်းလဒ်SUM(amount) → 2650
AVG(col)ပျမ်းမျှAVG(amount) → 441.67
MIN(col)အနည်းဆုံးMIN(amount) → 20
MAX(col)အများဆုံးMAX(amount) → 1200
GROUP_CONCAT(col)Values ကို string တစ်ခုအဖြစ် ချိတ်"Phone,Case"

6. Real-World Example — Sales Report

-- Monthly sales report per city
SELECT
    city,
    COUNT(DISTINCT customer) AS customers,
    COUNT(*) AS total_orders,
    SUM(amount * qty) AS gross_revenue,
    AVG(amount) AS avg_order_value,
    MAX(amount) AS biggest_order
FROM orders
WHERE amount > 0
GROUP BY city
HAVING gross_revenue > 500
ORDER BY gross_revenue DESC;

🎉 SQL Queries Complete!

SELECT, INSERT, UPDATE, DELETE, JOIN, Aggregate Functions — ရပြီ!

SQL 05 → MySQL Setup → SQL Menu

← SQL 03 JOINs  |  Next: SQL Lesson 05 → MySQL Setup →

📌 Study Checklist