SQL Server中Group By、Having和Where子句的执行顺序是怎样的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1130062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the execution sequence of Group By, Having and Where clause in SQL Server?
提问by KM.
I am just confused with the execution sequence of an SQL query when we use GROUP BY and HAVING with a WHERE clause. Which one gets executed first? What is the sequence?
当我们将 GROUP BY 和 HAVING 与 WHERE 子句一起使用时,我只是对 SQL 查询的执行顺序感到困惑。哪个先执行?顺序是什么?
回答by KM.
in order:
为了:
FROM& JOINs determine & filter rows
WHEREmore filters on the rows
GROUP BYcombines those rows into groups
HAVINGfilters groups
ORDER BYarranges the remaining rows/groups
LIMITfilters on the remaining rows/groups
FROM& JOIN确定和过滤行
WHERE行上的更多过滤器
GROUP BY将这些行组合成组
HAVING过滤器组
ORDER BY在剩余的行/组上排列剩余的行/组
LIMIT过滤器
回答by Md. Suman Kabir
Here is the complete sequence for sql server :
这是 sql server 的完整序列:
1. FROM
2. ON
3. JOIN
4. WHERE
5. GROUP BY
6. WITH CUBE or WITH ROLLUP
7. HAVING
8. SELECT
9. DISTINCT
10. ORDER BY
11. TOP
So from the above list, you can easily understand the execution sequence of GROUP BY, HAVING and WHERE
which is :
所以从上面的列表中,你可以很容易地理解其中的执行顺序GROUP BY, HAVING and WHERE
是:
1. WHERE
2. GROUP BY
3. HAVING
回答by Matthias Meid
WHERE is first, then you GROUP the result of the query, and last but not least HAVING-clause is taken to filter the grouped result. This is the "logical" order, I don't know how this is technically implemented in the engine.
WHERE 是第一个,然后您对查询的结果进行分组,最后但并非最不重要的 HAVING 子句用于过滤分组结果。这是“逻辑”顺序,我不知道这是如何在引擎中技术实现的。
回答by AdaTheDev
I think it is implemented in the engine as Matthias said: WHERE, GROUP BY, HAVING
我认为它是在引擎中实现的,正如 Matthias 所说:WHERE、GROUP BY、HAVING
Was trying to find a reference online that lists the entire sequence (i.e. "SELECT" comes right down at the bottom), but I can't find it. It was detailed in a "Inside Microsoft SQL Server 2005" book I read not that long ago, by Solid Quality Learning
试图在网上找到一个列出整个序列的参考(即“SELECT”在底部),但我找不到它。不久前,Solid Quality Learning 所著的“Inside Microsoft SQL Server 2005”一书中对此进行了详细介绍
Edit:Found a link: http://blogs.x2line.com/al/archive/2007/06/30/3187.aspx
编辑:找到一个链接:http: //blogs.x2line.com/al/archive/2007/06/30/3187.aspx
回答by Jim Chen
In Oracle 12c, you can run code both in either sequence below:
在 Oracle 12c 中,您可以按以下任一顺序运行代码:
Where
Group By
Having
Or
或者
Where
Having
Group by
回答by GimmicksWorld
In below Order
按以下顺序
- FROM & JOIN
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
- LIMIT
- 来自 & 加入
- 在哪里
- 通过...分组
- 有
- 选择
- 订购者
- 限制
回答by FerranB
Think about what you need to do if you wish to implement:
想一想如果你想实施你需要做什么:
- WHERE: Its need to execute the JOIN operations.
- GROUP BY: You specify Group by to "group" the results on the join, then it has to after the JOIN operation, after the WHERE usage.
- HAVING: HAVING is for filtering as GROUP BY expressions says. Then, it is executed after the GROUP BY.
- WHERE:它需要执行 JOIN 操作。
- GROUP BY:您指定 Group by 对连接的结果进行“分组”,然后它必须在 JOIN 操作之后,在 WHERE 用法之后。
- HAVING:HAVING 用于按照 GROUP BY 表达式进行过滤。然后,它在 GROUP BY 之后执行。
The order is WHERE, GROUP BY and HAVING.
顺序是 WHERE、GROUP BY 和 HAVING。
回答by manoj remala
Having Clause may come prior/before the group by clause.
Have Clause 可能出现在 group by 子句之前/之前。
Example: select * FROM test_std; ROLL_NO SNAME DOB TEACH
示例: select * FROM test_std; ROLL_NO SNAME DOB TEACH
1 John 27-AUG-18 Wills
2 Knit 27-AUG-18 Prestion
3 Perl 27-AUG-18 Wills
4 Ohrm 27-AUG-18 Woods
5 Smith 27-AUG-18 Charmy
6 Jony 27-AUG-18 Wills
Warner 20-NOV-18 Wills
Marsh 12-NOV-18 Langer
FINCH 18-OCT-18 Langer
9 rows selected.
已选择 9 行。
select teach, count() count from test_std having count() > 1 group by TEACH ;
从 test_std 中选择教学、计数()计数,通过 TEACH具有计数()> 1 组;
TEACH COUNT
教数
Langer 2 Wills 4
兰格 2 遗嘱 4
回答by OcTella
SELECT
FROM
JOINs
WHERE
GROUP By
HAVING
ORDER BY
SELECT
FROM
JOIN的
WHERE
GROUP BY
HAVING
ORDER BY