HAVING SQL: description, syntax, examples

SQL is a standard language for working with relational databases. It has in its arsenal many powerful tools for manipulating data stored in the form of tables.

having sql description

Undoubtedly, the ability to group data when they are sampled according to a certain attribute is one of such tools. The SQL HAVING statement, along with the WHERE statement, allows you to determine the selection conditions for data already grouped in some way.

HAVING SQL parameter: description

First of all, it is worth noting that this parameter is optional and is used exclusively in conjunction with the GROUP BY parameter. As you remember, GROUP BY is used when aggregate functions are used in SELECT, and the results of their calculations need to be obtained for certain groups. If WHERE allows you to specify sampling conditions before the data is grouped, then HAVING contains conditions that apply to the data directly in the groups themselves. For a better understanding, let's look at an example with the circuit shown in the figure below.

sql having statement

This is a great example giving a HAVING SQL description. Given a table with a list of product names, companies that produce them, and some other fields. In the request in the upper right corner we are trying to get information about how many product names each company produces, and we want to display only those companies that issue more than 2 names. The GROUP BY parameter formed three groups corresponding to the names of companies, for each of which the number of products (lines) was calculated. But the HAVING parameter, by its condition, cuts out one group from the resulting sample, since it did not satisfy the condition. As a result, we get two groups corresponding to companies with the number of products 5 and 3.

You may wonder why use HAVING if SQL has WHERE. If we used WHERE, then he would look at the total number of rows in the table, and not by group, and the condition would not make sense in this case. However, quite often they get along fine in one request.

sql having examples syntax

In the example above, we can see how the data is first selected by the employee names specified in the WHERE parameter, and then the result grouped in GROUP BY passes an additional check on the amount of salary for each employee.

SQL HAVING Parameter: Examples, Syntax

Consider some of the features of the HAVING SQL syntax. The description of this parameter is quite simple. Firstly, as already noted, it is used exclusively in conjunction with the GROUP BY parameter and is indicated immediately after it and before ORDER BY, if any, in the request. This is understandable, since HAVING defines the conditions for already grouped data. Secondly, in the condition of this parameter, only aggregate functions and fields specified in the GROUP BY parameter can be used. All conditions in this parameter are specified in exactly the same way as in the case of WHERE.

Conclusion

As you can see, there is nothing complicated in this operator. Semantically, it is used in the same way as WHERE. It is important to understand what WHERE is used with respect to all the data selected, and HAVING - only with respect to the groups defined in the GROUP BY parameter. We have presented a comprehensive description for HAVING SQL, which is sufficient for confident work with it.

Source: https://habr.com/ru/post/K20503/


All Articles