简体   繁体   English

SQL-关于HAVING子句

[英]SQL - about HAVING clause

Below is a sample question from Oracle 以下是来自Oracle的示例问题

Q) Which of the following statements is true about HAVING? 问:关于HAVING,以下哪项陈述是正确的? (Choose two.) (选择两个。)

A. It can be used only in the SELECT statement. 答:它只能在SELECT语句中使用。

B. It must occur after the GROUP BY clause. B.它必须出现在GROUP BY子句之后。

C. It must occur after the WHERE clause. C.它必须在WHERE子句之后发生。

D. It cannot reference an expression unless that expression is first reference in the GROUP BY clause. D.它不能引用一个表达式,除非该表达式是GROUP BY子句中的第一个引用。

To me, the answers should be A & B. The actual answers are A & C. "HAVING cannot be used without GROUP BY, but it is not required to follow GROUP BY". 对我来说,答案应该是A&B。实际答案是A&C。“没有GROUP BY不能使用HAVING,但是并不需要遵循GROUP BY”。

Is that statement correct? 那句话正确吗? Please advice. 请指教。

No, it isn't. 不,不是。 aggregation doesn't need a GROUP BY clause, hence HAVING can be used without one. 聚合不需要GROUP BY子句,因此HAVING可以不带子句使用。

Example: Give me the maximum salary, but only if the minimum salary is greater than 1000. 示例:给我最高薪水,但前提是最低薪水大于1000。

select max(salary)
from salaries
having min(salary) > 1000;

A and C is correct, but not for the reason given. A和C是正确的,但不是出于给定的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM