Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the difference between having and where in SQL

Shulou Source: shulou.com Published: 2022-05-31 19:26:04 09月18日 Update

Today, I will talk to you about the difference between having and where in SQL. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

In a select statement, you can use the groupby clause to divide rows into smaller groups, then use the clustering function to return summary information for each group, and you can use the having clause to restrict the result set returned. The groupby clause groups query results and returns summary information for rows Oracle groups query results according to the value of the expression specified in the groupby clause. In a query statement with a groupby clause, the column specified in the select list is either the column specified in the groupby clause or contains the clustering function copy code as follows: selectmax (sal), jobempgroupbyjob

(note that max (sal), the job of job does not have to appear, but it makes sense.) the select and groupby,having clauses of the query statement are the only places where the where function occurs, and the cluster function cannot be used in the cluster clause. The copy code is as follows: selectdeptno,sum (sal) fromempwheresal > 1200groupbydeptnohavingsum (sal) > 8500orderbydeptno

When the having clause is used in the gropuby clause, only the groups that meet the having condition are returned in the query results. There can be where clause and having clause in a sql statement. Having is similar to the where clause, which is used to set the qualifying condition. The function of the where clause is to remove the rows that do not meet the where condition before grouping the query results, that is, to filter the data before grouping, the condition cannot contain a clustering function, and the where condition is used to display specific rows. The role of the having clause is to filter groups that meet the criteria, that is, to filter data after grouping, which often contains clustering functions, to display specific groups using having conditions, or to group using multiple grouping criteria. The copy code for querying the number of employees for each position in each department is as follows: selectdeptno,job,count (*) fromempgroupbydeptno,job

If you are still confused about when to use WHERE and when to use HAVING, follow these instructions: the WHERE statement comes before the GROUPBY statement; SQL evaluates the WHERE statement before grouping. The HAVING statement comes after the GROUPBY statement; SQL evaluates the HAVING statement after grouping.

After reading the above, do you have any further understanding of the difference between having and where in SQL? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Clause statement grouping condition query code function result middle finger content function information data or place multiple is in more standard knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi Linux Shulou Tech Info Microsoft