The difference between where and having in oracle

1.where cannot be placed after GROUP BY
2. HAVING is used together with GROUP BY and placed after GROUP BY. The function at this time is equivalent to WHERE
3. There cannot be aggregate functions in the conditions behind WHERE, such as SUM(), AVG(), etc., while HAVING can
Where and Having are both a kind of filtering of query results, and the written point is the statement that sets the conditions. Its usage and similarities and differences are described below. Note: The field used in this article is the emp table under the default user scott in the oracle database, sal represents employee salary, and deptno represents department number.
1.
     Before the description of aggregation functions, let’s first understand aggregation functions: aggregation functions are sometimes called statistical functions, and their function is usually to perform statistics on a set of data, such as finding the maximum value, the minimum value, the total number, and the average value ( 
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 SUM is used to count the sum of the sal (salary) field in the emp table. The result is that the query only returns one result, the sum of wages. By using the GROUP BY clause, the functions SUM and COUNT can be made to work on data belonging to a group. 
2. Where clause
     where is only used for the value returned from the from clause. Each row of data returned by the from clause will be judged and filtered by the conditions in the where clause. Comparison operators (>, <, >=, <=, <>, !=|, etc.) and logical operators (and, or, not) are allowed in the where clause. Since everyone is familiar with the where clause, I won't go into details here. 
3. The having clause The
     having clause is usually used together with the order by clause. Because the role of having is to further filter the results of grouping statistics using group by. For example: Now need to find the department number whose department salary sum is greater than 10000? 
first step: 
select deptno,sum(sal) from emp group by deptno;

The filtering results are as follows:
DEPTNO SUM(SAL)
------ ----------
    30 9400
    20 10875
    10 8750
We can see the result we want. But now what if we want the sum of department wages greater than 10,000? Then I thought of having to filter the grouping statistics results to help us complete it.
Step 2:

 

select deptno,sum(sal) from emp group by deptno having sum(sal)>10000;

筛选结果如下:
DEPTNO   SUM(SAL)
------ ----------
    20      10875
当然这个结果正是我们想要的。
四、下面我们通过where子句和having子句的对比,更进一步的理解它们。
    在查询过程中聚合语句(sum,min,max,avg,count)要比having子句优先执行,简单的理解为只有有了统计结果后我才能执行筛选啊。where子句在查询过程中执行优先级别优先于聚合语句(sum,min,max,avg,count),因为它是一句一句筛选的。HAVING子句可以让我们筛选成组后的对各组数据筛选。,而WHERE子句在聚合前先筛选记录。如:现在我们想要部门号不等于10的部门并且工资总和大于8000的部门编号?
我们这样分析:通过where子句筛选出部门编号不为10的部门,然后在对部门工资进行统计,然后再使用having子句对统计结果进行筛选。

select deptno,sum(sal) from emp 
where deptno!='10' group by deptno
having sum(sal)>8000; 

筛选结果如下:
DEPTNO   SUM(SAL)
------ ----------
    30       9400
    20      10875
不做太多解释了,这个简单的小例子能够很好的说明where和having的作用。
五、异同点
     它们的相似之处就是定义搜索条件,不同之处是where子句为单个筛选而having子句与组有关,而不是与单个的行有关。
     最后:理解having子句和where子句最好的方法就是基础select语句中的那些句子的处理次序:where子句只能接收from子句输出的数据,而having子句则可以接受来自group by,where或者from子句的输入。

     

转自http://www.cnblogs.com/fuchongjundream/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325734932&siteId=291194637