ဒီ lesson မှာ ဒီ table ကို သုံးမယ်:
| id | customer | city | product | amount | qty |
|---|---|---|---|---|---|
| 1 | Ko Min | Yangon | Phone | 500 | 2 |
| 2 | Ma Aye | Mandalay | Laptop | 1200 | 1 |
| 3 | Ko Min | Yangon | Case | 20 | 5 |
| 4 | Ko Kyaw | Bago | Tablet | 400 | 1 |
| 5 | Ma Aye | Mandalay | Charger | 30 | 3 |
| 6 | Ko Kyaw | Bago | Phone | 500 | 1 |
-- 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_orders | total_sales | avg_order | smallest | largest |
|---|---|---|---|---|
| 6 | 2650 | 441.67 | 20 | 1200 |
-- 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:
| customer | num_orders |
|---|---|
| Ko Min | 2 |
| Ma Aye | 2 |
| Ko Kyaw | 2 |
Total spent per customer:
| customer | total_spent |
|---|---|
| Ma Aye | 1230 |
| Ko Min | 520 |
| Ko Kyaw | 900 |
-- 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;
| 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" |
-- 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 03 JOINs | Next: SQL Lesson 05 → MySQL Setup →