GROUP BY with AGGREGATE functions

suggest change

Table ORDERS

+---------+------------+----------+-------+--------+
| orderid | customerid | customer | total | items  |
+---------+------------+----------+-------+--------+
|       1 |          1 | Bob      |  1300 |     10 |
|       2 |          3 | Fred     |   500 |      2 |
|       3 |          5 | Tess     |  2500 |      8 |
|       4 |          1 | Bob      |   300 |      6 |
|       5 |          2 | Carly    |   800 |      3 |
|       6 |          2 | Carly    |  1000 |     12 |
|       7 |          3 | Fred     |   100 |      1 |
|       8 |          5 | Tess     | 11500 |     50 |
|       9 |          4 | Jenny    |   200 |      2 |
|      10 |          1 | Bob      |   500 |     15 |
+---------+------------+----------+-------+--------+

Return the number of rows that satisfy a specific criteria in WHERE clause.

E.g.: Number of orders for each customer.

SELECT customer, COUNT(*) as orders
FROM orders
GROUP BY customer
ORDER BY customer

Result:

+----------+--------+
| customer | orders |
+----------+--------+
| Bob      |      3 |
| Carly    |      2 |
| Fred     |      2 |
| Jenny    |      1 |
| Tess     |      2 |
+----------+--------+

Return the sum of the selected column.

E.g.: Sum of the total and items for each customer.

SELECT customer, SUM(total) as sum_total, SUM(items) as sum_items
FROM orders
GROUP BY customer
ORDER BY customer

Result:

+----------+-----------+-----------+
| customer | sum_total | sum_items |
+----------+-----------+-----------+
| Bob      |      2100 |        31 |
| Carly    |      1800 |        15 |
| Fred     |       600 |         3 |
| Jenny    |       200 |         2 |
| Tess     |     14000 |        58 |
+----------+-----------+-----------+

Return the average value of a column of numeric value.

E.g.: Average order value for each customers.

SELECT customer, AVG(total) as avg_total
FROM orders
GROUP BY customer
ORDER BY customer

Result:

+----------+-----------+
| customer | avg_total |
+----------+-----------+
| Bob      |       700 |
| Carly    |       900 |
| Fred     |       300 |
| Jenny    |       200 |
| Tess     |      7000 |
+----------+-----------+

Return the highest value of a certain column or expression.

E.g.: Highest order total for each customers.

SELECT customer, MAX(total) as max_total
FROM orders
GROUP BY customer
ORDER BY customer

Result:

+----------+-----------+
| customer | max_total |
+----------+-----------+
| Bob      |      1300 |
| Carly    |      1000 |
| Fred     |       500 |
| Jenny    |       200 |
| Tess     |     11500 |
+----------+-----------+

Return the lowest value of a certain column or expression.

E.g.: Lowest order total for each customers.

SELECT customer, MIN(total) as min_total
FROM orders
GROUP BY customer
ORDER BY customer

Result:

+----------+-----------+
| customer | min_total |
+----------+-----------+
| Bob      |       300 |
| Carly    |       800 |
| Fred     |       100 |
| Jenny    |       200 |
| Tess     |      2500 |
+----------+-----------+

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents