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 are the differences between where clause and having clause in SQL

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

Share

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

This article mainly introduces the differences between the where clause and the having clause in SQL. It has certain reference value. Interested friends can refer to it. I hope you can learn a lot after reading this article.

Where and Having are both a filter for query results, and the written point is the statement that sets the condition. The difference between where clause and having clause in SQL.

1.where cannot be put behind GROUP BY

2.HAVING is used in conjunction with GROUP BY and placed behind GROUP BY, which is equivalent to WHERE.

There can be no aggregate functions in the conditions after 3.WHERE, such as SUM (), AVG (), etc., while HAVING can

Where and Having are both a filter for query results, and the written point is the statement that sets the condition. The usage and similarities and differences are explained below. Note: this article uses the emp table under the default user scott in the oracle database, where sal represents employee salary and deptno represents department number.

I. aggregate function

Before we explain, let's take a look at aggregate functions: aggregate functions are sometimes called statistical functions, and their function is usually to count a group of data, such as maximum, minimum, total, and average (

MAX,MIN,COUNT, AVG), etc. The fundamental difference between these functions and other functions is that they generally act on multiple records. A simple example: SELECT SUM (sal) FROM emp, where the SUM function is to count the sum of the sal (salary) fields in the emp table. The result is that the query returns only one result, that is, the sum of wages. By using the GROUP BY clause, you can make functions such as SUM and COUNT work on data that belongs to a group.

II. Where clause

Where is used only for values returned from the from clause, and every row of data returned by the from clause is judged and filtered by the criteria in the where clause. The comparison operator (>, =, 10000) is allowed in the where clause

The screening results are as follows:

DEPTNO SUM (SAL)

-

20 10875

Of course, this result is exactly what we want.

Fourth, let's further understand them through the comparison of where clause and having clause.

The aggregate statement (sum,min,max,avg,count) takes precedence over the having clause in the query process, which is simply understood to mean that I can perform filtering only after I have the statistical results. The where clause takes precedence over the aggregate statement (sum,min,max,avg,count) during the query because it is filtered sentence by sentence. The HAVING clause allows us to filter the data of each group after being filtered into groups. While the WHERE clause filters records before aggregating For example, now we want a department whose department number is not equal to 10 and whose total salary is greater than 8000?

We analyze it this way: we screen out the departments whose department number is not 10 through the where clause, and then count the salaries of the departments, and then use the having clause to screen the statistical results.

Select deptno,sum (sal) from emp where deptnotated trees 10 'group by deptnohaving sum (sal) > 8000

The screening results are as follows:

DEPTNO SUM (SAL)

-

30 9400

20 10875

5. Similarities and differences

The similarity is that search conditions are defined, except that the where clause is filtered for a single filter, while the having clause is related to a group, not to a single row.

Finally: the best way to understand the having clause and the where clause is the processing order of those sentences in the basic select statement: the where clause can only receive data output from the from clause, while the having clause can accept input from the group by,where or from clause.

Thank you for reading this article carefully. I hope the article "what is the difference between where clause and having clause in SQL" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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