Online Book Reader

Home Category

MariaDB Crash Course - Ben Forta [38]

By Root 506 0

FROM products

WHERE prod_price >= 10

GROUP BY vend_id

HAVING COUNT(*) >= 2;

Output

+---------+-----------+

| vend_id | num_prods |

+---------+-----------+

| 1003 | 4 |

| 1005 | 2 |

+---------+-----------+

Analysis

This statement warrants an explanation. The first line is a basic SELECT using an aggregate function—much like the examples thus far. The WHERE clause filters all rows with a prod_price of at least 10. Data is then grouped by vend_id, and then a HAVING clause filters just those groups with a count of 2 or more. Without the WHERE clause two extra rows would have been retrieved (vendor 1002 that only sells products all priced under 10, and vendor 1001 that sells three products but only one of them is priced greater or equal to 10) as seen here:

Input

SELECT vend_id, COUNT(*) AS num_prods

FROM products

GROUP BY vend_id

HAVING COUNT(*) >= 2;

Output

+---------+-----------+

| vend_id | num_prods |

+---------+-----------+

| 1001 | 3 |

| 1002 | 2 |

| 1003 | 7 |

| 1005 | 2 |

+---------+-----------+

Grouping and Sorting


It is important to understand that GROUP BY and ORDER BY are different, even though they often accomplish the same thing. Table 13.1 summarizes the differences between them.

Table 13.1 ORDER BY Versus GROUP BY

The first difference listed in Table 13.1 is extremely important. More often than not, you will find that data grouped using GROUP BY will indeed be output in group order. But that is not always the case, and it is not actually required by the SQL specifications. Furthermore, you might actually want it sorted differently than it is grouped. Just because you group data one way (to obtain group-specific aggregate values) does not mean that you want the output sorted that same way. You should always provide an explicit ORDER BY clause as well, even if it is identical to the GROUP BY clause.

* * *

Tip: Don’t Forget ORDER BY

As a rule, anytime you use a GROUP BY clause, you should also specify an ORDER BY clause. That is the only way to ensure that data is sorted properly. Never rely on GROUP BY to sort your data.

* * *

To demonstrate the use of both GROUP BY and ORDER BY, let’s look at an example. The following SELECT statement is similar to the ones seen previously. It retrieves the order number and total order price of all orders with a total price of 50 or more:

Input

SELECT order_num, SUM(quantity*item_price) AS ordertotal

FROM orderitems

GROUP BY order_num

HAVING SUM(quantity*item_price) >= 50;

Output

+-----------+------------+

| order_num | ordertotal |

+-----------+------------+

| 20005 | 149.87 |

| 20006 | 55.00 |

| 20007 | 1000.00 |

| 20008 | 125.00 |

+-----------+------------+

To sort the output by order total, all you need to do is add an ORDER BY clause, as follows:

Input

SELECT order_num, SUM(quantity*item_price) AS ordertotal

FROM orderitems

GROUP BY order_num

HAVING SUM(quantity*item_price) >= 50

ORDER BY ordertotal;

Output

+-----------+------------+

| order_num | ordertotal |

+-----------+------------+

| 20006 | 55.00 |

| 20008 | 125.00 |

| 20005 | 149.87 |

| 20007 | 1000.00 |

+-----------+------------+

Analysis

In this example, the GROUP BY clause is used to group the data by order number (the order_num column) so that the SUM(*) function can return the total order price. The HAVING clause filters the data so that only orders with a total price of 50 or more are returned. Finally, the output is sorted using the ORDER BY clause.

SELECT Clause Ordering


This is probably a good time to review the order in which SELECT statement clauses are to be specified. Table 13.2 lists all the clauses you have learned thus far, in the order they must be used.

Table 13.2 SELECT Clauses and Their Sequence

Summary


In Chapter 12, “Summarizing Data,” you learned how to use the SQL aggregate functions to perform summary calculations on your data. In this chapter, you learned how to use the GROUP BY clause to perform these calculations on groups of data, returning results for each group. You saw how to use the HAVING

Return Main Page Previous Page Next Page

®Online Book Reader