Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between where and having in sql statements

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "what is the difference between where and having in sql sentences". In daily operation, I believe that many people have doubts about the difference between where and having in sql sentences. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is the difference between where and having in sql sentences?" Next, please follow the editor to study!

HAVING first groups and then filters records, and WHERE filters records before aggregation. that is, it acts before the GROUP BY clause and the HAVING clause, while the HAVING clause filters the group records after aggregation.

The object of action is different. The WHERE clause acts on tables and views, and the HAVING clause acts on groups. WHERE selects input rows before grouping and aggregation calculation (therefore, it controls which rows enter the aggregation calculation), while HAVING selects grouped rows after grouping and aggregation. Therefore, the WHERE clause cannot contain an aggregate function; there is no point in trying to use an aggregation function to determine which rows are input to the aggregation operation. In contrast, the HAVING clause always contains aggregate functions. (strictly speaking, you can write HAVING clauses that do not use aggregation, but this is in vain. The same conditions can be used more effectively in the WHERE phase. (for example:

Select * from tablename where id > 1; and

Select * from tablename having id > 1

There is no difference between the two query results. Having is not recommended

To sum up, there are two sentences:

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.

At this point, the study of "what is the difference between where and having in sql sentences" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report