[英]SQL ALL operator does nothing. What is it used for?
I don't understand the use of ALL operator in SQL Server.我不明白 SQL 服务器中 ALL 运算符的使用。 In the following example, ALL doesn't seem to do much since when I remove it, I get the same results.在下面的示例中, ALL 似乎没有做太多,因为当我删除它时,我得到了相同的结果。 Do I really have to use it?我真的必须使用它吗?
SELECT *
FROM EMPLOYEE
WHERE Salary > ALL (
SELECT AVG(Salary)
FROM EMPLOYEE);
So my question is, what does ALL do, when should I use it and is it necessary?所以我的问题是,ALL 做什么,我应该什么时候使用它,是否有必要? Thanks in advance.提前致谢。
The
ALL
operator:ALL
运算符:
- returns a boolean value as a result结果返回 boolean 值
- returns TRUE if ALL of the subquery values meet the condition如果所有子查询值都满足条件,则返回 TRUE
- is used with SELECT, WHERE and HAVING statements与 SELECT、WHERE 和 HAVING 语句一起使用
ALL means that the condition will be true only if the operation is true for all values in the range. ALL 意味着只有当操作对范围内的所有值都为真时,条件才会为真。
In your query, SEELCT AVG(Salary) FROM Employee
returns a single result, so you're comparing each individual employee's salary to "all" of the averages (for which you have one average).在您的查询中, SEELCT AVG(Salary) FROM Employee
返回一个结果,因此您将每个员工的工资与“所有”平均值(您有一个平均值)进行比较。 This is why your query doesn't need the ALL
.这就是您的查询不需要ALL
的原因。
If you had a query like this:如果您有这样的查询:
SELECT *
FROM EMPLOYEE
WHERE Salary > ALL (SELECT SomeValue FROM SomeTable);
And the SomeValue values of SomeTable were: SomeTable 的 SomeValue 值为:
5000
2000
800
Then you would only get results from Employee
where Salary > 5000 AND Salary > 2000 AND Salary > 800
(effectively Salary > 5000).那么你只会从Employee
那里得到Salary > 5000 AND Salary > 2000 AND Salary > 800
(实际上是 Salary > 5000)的结果。
Borrowing an example from sqltutorial.org , you might want to find all employees whose salaries are higher than the salary of employees working in a specific department.借用sqltutorial.org 中的一个示例,您可能希望查找其薪水高于特定部门员工薪水的所有员工。 Obviously the employees that this query finds will not be in that department:显然,此查询找到的员工将不在该部门:
SELECT
first_name, last_name, salary
FROM
employees
WHERE
salary > ALL (SELECT
salary
FROM
employees
WHERE
department_id = 2)
ORDER BY salary;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.