The convenient way of thinking about the query clauses, is that they are processed sequentially from the top down. First, the select clause tells the query what columns are to be in the resulting record set. Then the from clause tells the query to take the records from the products tabular section, across all sales documents. Then the joints, grab information from other tables, the sales document table. And the product catalogue. Then, it's the where clauses turn. It looks at the conditions we gave. And filters out every record that doesn't meet them. Now, let's look at the next two clauses down. [MUSIC] So this is what select from and where clauses output for us. A list of products sold across all my sales documents. Obviously, the same product appear more than once in the list. Is there a way to modify this query, so it sums up the quantities and prices for each product like this. [SOUND] Well, this is exactly what the group by clause does. First of all, I have to tell the query what filter fields to group the results by. What I said here is basically this, hey query combined all records having the same product field value into a single record. But if I try to run the query, this is what I get. The query says, that it cannot group the results by products and output the document number at the same time. The problem here is that there are records with the same product belonging to the different documents. So the query just doesn't know what document number to take. The only field that knows how to output now, is the product field. And as for the rest of them, we need either to remove the field from the output list, or tell the query how to aggregate all its different values into one. Let me show you what we can use aggregation wise. As you can see, we have a whole bunch of functions to aggregate numeric values. Sum them up, calculate the average value, take them minimum or maximum and count them up. None of them for the exception of the count probably makes much of a sense for string values. Although we still can use min and max, because the platform can compare strings as you remember. So, let me just delete the document and its joined like this. And now, we have the same problem with the product type field. So, I'm going to delete this field from the output. But I still need this join, because of this condition over here, so I'm keeping it. And now, I'm going to some optic quantity, and line total like this. And here we go. This is the result I need it. All right, now to they having clause. You can think of having as of where, that works after the grouping is done, and only for the group fields. For example, I can leave in the output only the records for the price over $40. And here we go. Or, I can select only products containing coca substring anywhere in the description. Like this [SOUND]. Check out by the way, this like logical expression. This percent wildcards mean any number of any symbols. So this condition found both cokes we had. So these were our group by and having clauses. One less thing that doesn't deserve its own episode, but it's worth mentioning anyway, the order by clause. This is a simple one. Just list all the fields you want your output to be sorted by. And here you go. And that's pretty much it. [MUSIC]