在本教程中,您将了解sql all运算符以及如何使用它来将值与一组值进行比较。
sql all
运算符是一个逻辑运算符,它将单个值与子查询返回的单列值集进行比较。
以下是sql all
运算符的语法:
where column_name comparison_operator all (subquery)
sql all
运算符必须以比较运算符开头,例如:>
,>=
,<
,<=
,<>
,=
,后跟子查询。 某些数据库系统(如oracle)允许使用文字值列表而不是子查询。
请注意,如果子查询不返回任何行,则where
子句中的条件始终为true
。 假设子查询返回一行或多行,下表说明了sql all
运算符的含义:
条件 | 描述 |
---|---|
c > all(…) |
c 列中的值必须大于要评估为true 的集合中的最大值。 |
c >= all(…) |
c 列中的值必须大于或等于要评估为true 的集合中的最大值。 |
c < all(…) |
c 列中的值必须小于要评估为true 的集合中的最小值。 |
c <= all(…) |
c 列中的值必须小于或等于要评估为true 的集合中的最小值。 |
c <> all(…) |
c 列中的值不得等于要评估为true 的集合中的任何值。 |
c = all(…) |
c 列中的值必须等于要评估为true 的集合中的任何值。 |
我们将使用示例数据库中的employees
表进行演示:
2.1. sql all使用大于运算符
以下查询查找column_name
列中的值大于子查询返回的最大值的行:
select
*
from
table_name
where
column_name > all (subquery);
例如,以下语句查找工资大于部门id
为2
的员工最高工资的所有员工:
select
first_name, last_name, salary
from
employees
where
salary > all (select
salary
from
employees
where
department_id = 2)
order by salary;
执行上面查询语句,得到以下结果:
下面通过查询部门id为2
中员工的最高薪水来验证它:
mysql> select
max(salary)
from
employees
where
department_id = 2;
+-------------+
| max(salary) |
+-------------+
| 13000 |
+-------------+
1 row in set
此查询返回13000
,该值小于使用上述all
运算符的查询返回的任何薪水。
2.2. sql all大于或等于运算符
下面显示了具有大于或等于运算符的sql all
运算符的语法:
select
*
from
table_name
where
column_name >= all (subquery);
该查询返回column_name
列中的值大于或等于子查询返回的所有值的所有行。
例如,以下查询查找薪水大于或等于市场营销部门(department_id=2
)员工最高薪水的所有员工:
select
first_name, last_name, salary
from
employees
where
salary >= all (select
salary
from
employees
where
department_id = 2)
order by salary;
执行上面查询语句,得到以下结果:
如上图中所示,michael
员工的薪水为13000
,等于营销部门员工的最高薪水包含在结果集中。
2.3. sql all使用小于运算符
以下说明了all
运算符结合小于运算符一起使用的查询:
select
*
from
table_name
where
column_name < all (subquery);
此查询返回column_name
列中的值小于子查询返回的最小值的所有行。
以下语句查找市场营销部门(department_id=2
)中员工的最低薪水:
select
min(salary)
from
employees
where
department_id = 2;
+-------------+
| min(salary) |
+-------------+
| 6000 |
+-------------+
1 row in set
要查找薪水低于市场营销部门(department_id=2
)员工最低薪水的所有员工,请使用all
运算符和小于运算符,如下所示:
select
first_name, last_name, salary
from
employees
where
salary < all (select
salary
from
employees
where
department_id = 2)
order by salary desc;
执行以上查询语句,得到以下结果:
2.4. sql all小于或等于运算符
以下是all
运算符与小于或等于运算符一起使用的语法:
select
*
from
table_name
where
column_name <= all (subquery);
例如,以下语句查找薪水小于或等于市场营销部门(department_id=2
)员工最低薪水的所有员工:
select
first_name, last_name, salary
from
employees
where
salary <= all (select
salary
from
employees
where
department_id = 2)
order by salary desc;
执行以上查询语句,得到以下结果:
2.5. sql all与不等于运算符
以下查询返回column_name
列中的值不等于子查询返回的任何值的所有行:
select
*
from
table_name
where
column_name <> all (subquery);
例如,要查找工资不等于每个部门平均工资的员工,请使用以下查询:
select
first_name, last_name, salary
from
employees
where
salary <> all (select
avg(salary)
from
employees
group by department_id)
order by salary desc;
执行以上查询语句,得到以下结果:
+------------+-----------+--------+
| first_name | last_name | salary |
+------------+-----------+--------+
| steven | lee | 24000 |
| neena | wong | 17000 |
| lex | liang | 17000 |
| john | liu | 14000 |
| karen | liu | 13500 |
| michael | zhou | 13000 |
| nancy | chen | 12000 |
| shelley | wu | 12000 |
| avg | su | 11000 |
| alexander | lee | 9000 |
... ...
| shelli | zhang | 2900 |
| sigal | zhang | 2800 |
| irene | liu | 2700 |
| guy | zhang | 2600 |
| karen | zhang | 2500 |
+------------+-----------+--------+
35 rows in set
请注意,子查询通过使用avg()函数和group by子句查找部门员工的平均工资。
2.6. sql all与等于运算符
当将all运算符与等于运算符一起使用时,查询将查找column_name
列中的值等于子查询返回的任何值的所有行:
select
*
from
table_name
where
column_name = all (subquery);
以下示例查找薪水等于市场营销部门(department_id=2
)员工最高薪水的所有员工:
select
first_name, last_name, salary
from
employees
where
salary = all (select
max(salary)
from
employees
where
department_id = 2);
执行上面查询语句,得到以下结果:
+------------+-----------+--------+
| first_name | last_name | salary |
+------------+-----------+--------+
| michael | zhou | 13000 |
+------------+-----------+--------+
1 row in set
在本教程中,您学习了如何使用sql all
运算符来测试值是否与子查询返回的值集匹配。